001/* For testing the workspace synchronization features. 002 003 Copyright (c) 2003-2013 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.Collections; 032import java.util.Iterator; 033import java.util.LinkedList; 034import java.util.List; 035 036////////////////////////////////////////////////////////////////////////// 037//// TestWorkspaceBase 038 039/** 040 A base class for creating tests on the workspace synchronization features. 041 A derived test creates a list of threads that access the workspace in 042 various ways. 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 abstract class TestWorkspaceBase { 052 /** Create threads that access a workspace in various ways. 053 * List the threads in _accessThreads. 054 */ 055 public abstract void initializeTest(); 056 057 /** Initialize and run the test. 058 */ 059 public void runTest() { 060 initializeTest(); 061 062 Iterator threads = _accessThreads.iterator(); 063 064 while (threads.hasNext()) { 065 Thread thread = (Thread) threads.next(); 066 thread.start(); 067 } 068 069 try { 070 Thread.sleep(_testTime); 071 } catch (InterruptedException ex) { 072 _profile = "Test interrupted\n"; 073 } 074 075 threads = _accessThreads.iterator(); 076 077 while (threads.hasNext()) { 078 Thread thread = (Thread) threads.next(); 079 thread.interrupt(); 080 081 try { 082 thread.join(); 083 } catch (InterruptedException ex) { 084 _profile += "Test thread " + thread.getName() 085 + " interrupted\n"; 086 } 087 } 088 089 if (_profile == null) { 090 StringBuffer buf = new StringBuffer(); 091 Iterator records = _record.iterator(); 092 093 while (records.hasNext()) { 094 buf.append((String) records.next()); 095 buf.append("\n"); 096 } 097 098 _profile = buf.toString(); 099 } 100 101 //System.out.println(_profile); 102 } 103 104 public String profile() { 105 return _profile; 106 } 107 108 private String _profile; 109 110 protected List _accessThreads = new LinkedList(); 111 112 protected List _record = Collections.synchronizedList(new LinkedList()); 113 114 protected long _testTime = 0; 115}