001/* For testing the workspace synchronization features. 002 003 Copyright (c) 2003-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 java.util.LinkedList; 032import java.util.List; 033 034import ptolemy.kernel.util.Workspace; 035 036////////////////////////////////////////////////////////////////////////// 037//// TestWorkspace6 038 039/** 040 Test the following scenario: thread T1 gets read access twice, then waits 041 on a lock object (and in doing so, releases the read access); T2 gets/releases 042 write access; T1 is interrupted, and reacquires read access. 043 044 @author Xiaojun Liu 045 @version $Id$ 046 @since Ptolemy II 3.0 047 @Pt.ProposedRating Green (eal) 048 @Pt.AcceptedRating Red (cxh) 049 050 */ 051public class TestWorkspace6 extends TestWorkspaceBase { 052 public static void main(String[] args) { 053 TestWorkspace6 tw = new TestWorkspace6(); 054 tw.runTest(); 055 } 056 057 @Override 058 public void initializeTest() { 059 Workspace workspace = new Workspace(); 060 List actions = new LinkedList(); 061 AccessAction action = new AccessAction(workspace, 0, 'R', 0, null, 062 _record, "A0"); 063 actions.add(action); 064 action = new AccessAction(workspace, 0, 'R', 0, null, _record, "A1"); 065 actions.add(action); 066 action = new AccessAction(workspace, 0, 'U', 0, new Object(), _record, 067 "A2"); 068 actions.add(action); 069 _thread = new AccessThread("T1", actions, this); 070 _accessThreads.add(_thread); 071 actions = new LinkedList(); 072 action = new AccessAction(workspace, 500, 'W', 500, null, _record, 073 "A3"); 074 actions.add(action); 075 _accessThreads.add(new AccessThread("T2", actions, this)); 076 _testTime = 5000; // ms 077 } 078 079 @Override 080 public void runTest() { 081 new Thread() { 082 @Override 083 public void run() { 084 try { 085 sleep(1000); 086 } catch (InterruptedException e) { 087 // ignore 088 } 089 090 // interrupt T2 while it is waiting for read access 091 _thread.interrupt(); 092 } 093 }.start(); 094 super.runTest(); 095 } 096 097 private Thread _thread; 098}