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//// TestWorkspace4 038 039/** 040 Test the following scenario: thread T1 gets write access; T2 waits 041 for read access and is interrupted, then waits for read access again; 042 T1 releases write access; T2 gets/releases read access; another thread 043 T3 gets write access; T2 handles failure (being interrupted) in getting 044 read access. 045 046 @author Xiaojun Liu 047 @version $Id$ 048 @since Ptolemy II 3.0 049 @Pt.ProposedRating Green (eal) 050 @Pt.AcceptedRating Red (cxh) 051 052 */ 053public class TestWorkspace4 extends TestWorkspaceBase { 054 @Override 055 public void initializeTest() { 056 Workspace workspace = new Workspace(); 057 List actions = new LinkedList(); 058 AccessAction action = new AccessAction(workspace, 0, 'W', 1000, null, 059 _record, "A0"); 060 actions.add(action); 061 _accessThreads.add(new AccessThread("T1", actions, this)); 062 actions = new LinkedList(); 063 action = new AccessAction(workspace, 500, 'R', 500, null, _record, 064 "A2"); 065 actions.add(action); 066 action = new AccessAction(workspace, 0, 'R', 0, null, _record, "A3"); 067 actions.add(action); 068 _thread = new AccessThread("T2", actions, this); 069 _accessThreads.add(_thread); 070 actions = new LinkedList(); 071 action = new AccessAction(workspace, 2200, 'W', 100, null, _record, 072 "A1"); 073 actions.add(action); 074 _accessThreads.add(new AccessThread("T3", actions, this)); 075 _testTime = 5000; // ms 076 } 077 078 @Override 079 public void runTest() { 080 new Thread() { 081 @Override 082 public void run() { 083 try { 084 sleep(1000); 085 } catch (InterruptedException e) { 086 // ignore 087 } 088 089 // interrupt T2 while it is waiting for read access 090 _thread.interrupt(); 091 } 092 }.start(); 093 super.runTest(); 094 } 095 096 private Thread _thread; 097}