001/* For testing the workspace synchronization features.
002
003 Copyright (c) 1997-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027
028 */
029package ptolemy.kernel.util.test;
030
031import ptolemy.kernel.util.Workspace;
032
033//////////////////////////////////////////////////////////////////////////
034//// TestWorkspace
035
036/**
037 This object implements a thread that obtains read permission to
038 a workspace three times sequentially, then obtains write permission.
039 To use it, create an instance and then call its start() method.
040 To obtain a profile of what it did, call its profile() method.
041 That will return only after the thread completes.
042 NOTE: This is a very primitive test.  It does not check very much.
043
044 @author Edward A. Lee
045 @version $Id$
046 @since Ptolemy II 0.2
047 @Pt.ProposedRating Green (eal)
048 @Pt.AcceptedRating Red (cxh)
049
050 */
051public class TestWorkspace extends Thread {
052    public static void main(String[] args) {
053        Workspace w = new Workspace("test");
054        TestWorkspace tw = new TestWorkspace("test", w);
055        tw.start();
056        System.out.println(tw._profile);
057    }
058
059    public TestWorkspace(String name, Workspace workspace) {
060        _name = name;
061        _workspace = workspace;
062    }
063
064    @Override
065    public synchronized void run() {
066        for (int i = 0; i < 3; i++) {
067            try {
068                _workspace.getReadAccess();
069                _profile += _name + ".getReadAccess()\n";
070
071                try {
072                    // FindBugs:
073                    // [M M SWL] Method calls Thread.sleep() with a lock held [SWL_SLEEP_WITH_LOCK_HELD]
074                    // In this test program however this is not a problem.
075                    Thread.sleep(100);
076                } catch (InterruptedException ex) {
077                }
078            } finally {
079                _workspace.doneReading();
080                _profile += _name + ".doneReading()\n";
081            }
082        }
083
084        try {
085            _workspace.getWriteAccess();
086            _profile += _name + ".getWriteAccess()\n";
087
088            try {
089                // FindBugs:
090                // [M M SWL] Method calls Thread.sleep() with a lock held [SWL_SLEEP_WITH_LOCK_HELD]
091                // In this test program however this is not a problem.
092                Thread.sleep(100);
093            } catch (InterruptedException ex) {
094            }
095        } finally {
096            _workspace.doneWriting();
097            _profile += _name + ".doneWriting()\n";
098        }
099    }
100
101    public synchronized String profile() {
102        return _profile;
103    }
104
105    private String _name;
106
107    private Workspace _workspace;
108
109    private String _profile = "";
110}