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.PtolemyThread;
032import ptolemy.kernel.util.Workspace;
033
034//////////////////////////////////////////////////////////////////////////
035//// PtestWorkspace
036
037/**
038
039 This object implements a ptolemy thread that obtains read permission to
040 a workspace three times sequentially, then obtains write permission.
041 To use it, create an instance and then call its start() method.
042 To obtain a profile of what it did, call its profile() method.
043 That will return only after the thread completes.
044 NOTE: This is a very primitive test.  It does not check very much.
045
046 @author Edward A. Lee, Lukito Muliadi
047 @version $Id$
048 @since Ptolemy II 0.2
049 @Pt.ProposedRating Green (eal)
050 @Pt.AcceptedRating Red (cxh)
051
052 */
053public class PtestWorkspace extends PtolemyThread {
054    public PtestWorkspace(String name, Workspace workspace) {
055        _name = name;
056        _workspace = workspace;
057    }
058
059    @Override
060    public synchronized void run() {
061        for (int i = 0; i < 3; i++) {
062            try {
063                _workspace.getReadAccess();
064                _profile += _name + ".getReadAccess()\n";
065
066                try {
067                    // FindBugs:
068                    // [M M SWL] Method calls Thread.sleep() with a lock held [SWL_SLEEP_WITH_LOCK_HELD]
069                    // In this test program however this is not a problem.
070                    Thread.sleep(100);
071                } catch (InterruptedException ex) {
072                }
073            } finally {
074                _workspace.doneReading();
075                _profile += _name + ".doneReading()\n";
076            }
077        }
078
079        try {
080            _workspace.getWriteAccess();
081            _profile += _name + ".getWriteAccess()\n";
082
083            try {
084                // FindBugs:
085                // [M M SWL] Method calls Thread.sleep() with a lock held [SWL_SLEEP_WITH_LOCK_HELD]
086                // In this test program however this is not a problem.
087                Thread.sleep(100);
088            } catch (InterruptedException ex) {
089            }
090        } finally {
091            _workspace.doneWriting();
092            _profile += _name + ".doneWriting()\n";
093        }
094    }
095
096    public synchronized String profile() {
097        return _profile;
098    }
099
100    private String _name;
101
102    private Workspace _workspace;
103
104    private String _profile = "";
105}