Skip to content

Commit 2a8d5a4

Browse files
author
janvanbesien
committed
initial revision
git-svn-id: http://multithreadedtc-junit4.googlecode.com/svn/trunk/MultithreadedTC@2 7aeca032-9163-11de-bb70-8db92513eca4
0 parents  commit 2a8d5a4

File tree

94 files changed

+10945
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+10945
-0
lines changed

CHANGELOG.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
3+
1.01 August 13, 2007
4+
- - Update to better support translation into JDK 1.4 compatible bytecode using Retrotranslator
5+
6+
1.0 May 10, 2007
7+
- - Initial Release

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright (c) 2007, University of Maryland
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9+
* Neither the name of the University of Maryland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
14+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
15+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README-JDK14.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The jar file MultithreadedTC-XXX-jdk14.jar is compiled for JDK 1.4 and requires all the libraries
2+
distributed with Retrotranslator (http://retrotranslator.sourceforge.net/)

README.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
For more information about MultithreadedTC, visit
2+
http://www.cs.umd.edu/projects/PL/multithreadedtc/
3+
4+
To use this library, you need JUnit (3.8 or 4.*). Get it at
5+
http://www.junit.org
6+
7+
MultithreadedTC is released under the BSD License,
8+
http://www.opensource.org/licenses/bsd-license.php

build.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project name="MultithreadedTC" default="distjar" basedir=".">
2+
3+
<property name="release.id" value="1.01" />
4+
<property name="jarfile" value="MultithreadedTC-${release.id}.jar" />
5+
<property name="jarfile14" value="MultithreadedTC-${release.id}-jdk14.jar" />
6+
7+
<property name="sourcename" value="MultithreadedTC-${release.id}-source" />
8+
9+
<property name="info.files" value="README.txt,LICENSE.txt,CHANGELOG.txt" />
10+
<property name="all.info.files" value="${info.files},README-JDK14.txt" />
11+
12+
<!-- Create distribution jar -->
13+
<target name="distjar">
14+
<jar destfile="${jarfile}" update="true">
15+
<fileset dir="bin" includes="edu/**/*class" />
16+
<fileset dir="src" includes="edu/**/*.java" />
17+
<fileset dir="." includes="${info.files}" />
18+
</jar>
19+
</target>
20+
21+
<!-- Create Source Distribution zip -->
22+
<target name="sourcezip" depends="distjar,distjar-jdk14">
23+
<zip destfile="${sourcename}.zip" update="true">
24+
<zipfileset dir="." includes="src/**" excludes="**.svn**" prefix="${sourcename}"/>
25+
<zipfileset dir="." includes="examples/**" excludes="**.svn**" prefix="${sourcename}"/>
26+
<zipfileset dir="." includes="web/**" excludes="**.svn**,sanity/**" prefix="${sourcename}"/>
27+
<zipfileset dir="." includes="${all.info.files}" prefix="${sourcename}"/>
28+
<zipfileset dir="." includes=".project,.classpath,build.xml" prefix="${sourcename}"/>
29+
<zipfileset dir="." includes="${jarfile}" prefix="${sourcename}"/>
30+
<zipfileset dir="." includes="${jarfile14}" prefix="${sourcename}"/>
31+
</zip>
32+
</target>
33+
34+
<!-- Create Website distribution zip -->
35+
<target name="webzip">
36+
<zip destfile="MultithreadedTCWebsite.zip" update="true">
37+
<fileset dir="web" includes="**" excludes="**.svn**" />
38+
</zip>
39+
</target>
40+
41+
<!-- Retro Tasks: Create a Jar for 1.4 -->
42+
<!-- The 'retrotranslator' task creates a jar file with JDK 1.4 compatible class files.
43+
This task requires all the jar files in the retrotranslator distribution to be in
44+
the lib folder. Find these at http://retrotranslator.sourceforge.net/ -->
45+
<target name="distjar-jdk14">
46+
<path id="retroclasspath">
47+
<fileset dir="lib" includes="**/*.jar"/>
48+
</path>
49+
50+
<taskdef name="retrotranslator" classpathref="retroclasspath"
51+
classname="net.sf.retrotranslator.transformer.RetrotranslatorTask" />
52+
<retrotranslator destjar="${jarfile14}" verify="false">
53+
<fileset dir="bin" includes="edu/**/*.class"/>
54+
<fileset dir="." includes="${all.info.files}" />
55+
</retrotranslator>
56+
</target>
57+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package plain_vs_mtc;
2+
3+
import edu.umd.cs.mtc.MultithreadedTestCase;
4+
import edu.umd.cs.mtc.Threaded;
5+
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import static org.junit.Assert.assertEquals;
11+
import static junit.framework.Assert.assertTrue;
12+
13+
/**
14+
* @author <a href="mailto:[email protected]">Jan Van Besien</a>
15+
*/
16+
public class CompareAndSetTestsMTC extends MultithreadedTestCase
17+
{
18+
19+
AtomicInteger ai;
20+
21+
@Before
22+
public void initialize()
23+
{
24+
ai = new AtomicInteger(1);
25+
}
26+
27+
@Threaded
28+
public void thread1()
29+
{
30+
while (!ai.compareAndSet(2, 3)) Thread.yield();
31+
}
32+
33+
@Threaded
34+
public void thread2()
35+
{
36+
assertTrue(ai.compareAndSet(1, 2));
37+
}
38+
39+
@Test
40+
public void finish()
41+
{
42+
assertEquals(ai.get(), 3);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package plain_vs_mtc;
2+
3+
import edu.umd.cs.mtc.MultithreadedTestCase;
4+
import edu.umd.cs.mtc.Threaded;
5+
import static junit.framework.Assert.assertTrue;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertFalse;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.util.concurrent.atomic.AtomicInteger;
12+
13+
/**
14+
* compareAndSet in one thread enables another waiting for value to succeed
15+
*/
16+
public class CompareAndSetTestsPlain
17+
{
18+
19+
/* NOTES
20+
* - Plain version requires a join before final asserts
21+
* - MTC version does not need to check if thread is alive
22+
*/
23+
24+
@Test
25+
public void testCompareAndSet() throws InterruptedException
26+
{
27+
final AtomicInteger ai = new AtomicInteger(1);
28+
Thread t = new Thread(new Runnable()
29+
{
30+
public void run()
31+
{
32+
while (!ai.compareAndSet(2, 3)) Thread.yield();
33+
}
34+
});
35+
36+
t.start();
37+
assertTrue(ai.compareAndSet(1, 2));
38+
t.join(2500);
39+
assertFalse(t.isAlive());
40+
assertEquals(ai.get(), 3);
41+
}
42+
43+
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package plain_vs_mtc;
2+
3+
import edu.umd.cs.mtc.MultithreadedTestCase;
4+
import edu.umd.cs.mtc.Threaded;
5+
6+
import java.util.concurrent.Semaphore;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import static org.junit.Assert.fail;
11+
12+
/**
13+
* @author <a href="mailto:[email protected]">Jan Van Besien</a>
14+
*/
15+
public class InterruptBlockedTestsMTC extends MultithreadedTestCase
16+
{
17+
Semaphore s;
18+
19+
@Before
20+
public void initialize()
21+
{
22+
s = new Semaphore(0);
23+
}
24+
25+
@Threaded("1")
26+
public void thread1()
27+
{
28+
try
29+
{
30+
s.acquire();
31+
fail("should throw exception");
32+
} catch (InterruptedException success)
33+
{
34+
assertTick(1);
35+
}
36+
}
37+
38+
@Threaded
39+
public void thread2()
40+
{
41+
waitForTick(1);
42+
getThread(1).interrupt();
43+
}
44+
45+
@Test
46+
public void test()
47+
{
48+
}
49+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package plain_vs_mtc;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
import static junit.framework.Assert.assertFalse;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.After;
9+
import static org.junit.Assert.fail;
10+
11+
/**
12+
* Test that a waiting acquire blocks interruptibly
13+
*/
14+
public class InterruptBlockedTestsPlain
15+
{
16+
17+
/* NOTES
18+
* - Failures in threads require additional work in setup and teardown
19+
* - Relies on Threaded.sleep to ensure acquire has blocked
20+
* - Does not ensure that exceptions are definitely caused by interrupt
21+
* - More verbose
22+
* - Requires a join at the end
23+
* - In MTC version, get reference to a thread using getThread(1)
24+
*/
25+
26+
volatile boolean threadFailed;
27+
28+
public void threadShouldThrow()
29+
{
30+
threadFailed = true;
31+
fail("should throw exception");
32+
}
33+
34+
@Before
35+
public void setUp() throws Exception
36+
{
37+
threadFailed = false;
38+
}
39+
40+
@Test
41+
public void testInterruptedAcquire()
42+
{
43+
final Semaphore s = new Semaphore(0);
44+
Thread t = new Thread(new Runnable()
45+
{
46+
public void run()
47+
{
48+
try
49+
{
50+
s.acquire();
51+
threadShouldThrow();
52+
} catch (InterruptedException success)
53+
{
54+
}
55+
}
56+
});
57+
t.start();
58+
try
59+
{
60+
Thread.sleep(50);
61+
t.interrupt();
62+
t.join();
63+
} catch (InterruptedException e)
64+
{
65+
fail("Unexpected exception");
66+
}
67+
}
68+
69+
@After
70+
public void tearDown() throws Exception
71+
{
72+
assertFalse(threadFailed);
73+
}
74+
75+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package plain_vs_mtc;
2+
3+
import edu.umd.cs.mtc.MultithreadedTestCase;
4+
import edu.umd.cs.mtc.Threaded;
5+
import static junit.framework.Assert.assertEquals;
6+
import static junit.framework.Assert.assertTrue;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
12+
/**
13+
* @author <a href="mailto:[email protected]">Jan Van Besien</a>
14+
*/
15+
public class ThreadControlTestsMTC extends MultithreadedTestCase
16+
{
17+
18+
AtomicInteger ai;
19+
20+
@Before
21+
public void initialize()
22+
{
23+
ai = new AtomicInteger(0);
24+
}
25+
26+
@Threaded
27+
public void thread1()
28+
{
29+
assertTrue(ai.compareAndSet(0, 1)); // S1
30+
waitForTick(3);
31+
assertEquals(ai.get(), 3); // S4
32+
}
33+
34+
@Threaded
35+
public void thread2()
36+
{
37+
waitForTick(1);
38+
assertTrue(ai.compareAndSet(1, 2)); // S2
39+
waitForTick(3);
40+
assertEquals(ai.get(), 3); // S4
41+
}
42+
43+
@Threaded
44+
public void thread3()
45+
{
46+
waitForTick(2);
47+
assertTrue(ai.compareAndSet(2, 3)); // S3
48+
}
49+
50+
@Test
51+
public void test()
52+
{
53+
}
54+
}

0 commit comments

Comments
 (0)