google / thread-weaver Goto Github PK
View Code? Open in Web Editor NEWA Java framework for testing multithreaded code.
License: Apache License 2.0
A Java framework for testing multithreaded code.
License: Apache License 2.0
Would be nice to see it in the list at https://github.com/google. Thanks!
Hi,
I tried getting information regarding this but could not find any. It seems I cannot instrument static methods to test them with thread weaver.
Here is a basic example of what I try to achieve:
public class DoClass {
public static void doTheThing() {
System.out.println("do");
}
}
public class DoClassTest {
@ThreadedBefore
public void before() {}
@ThreadedMain
public void mainThread() {
DoClass.doTheThing();
}
@ThreadedSecondary
public void secondThread() {
DoClass.doTheThing();
}
@ThreadedAfter
public void after() {}
@Test
public void runThreadedTests() {
new AnnotatedTestRunner().runTests(DoClassTest.class, DoClass.class);
}
}
All I end up with is this nasty exception:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.google.testing.threadtester.MethodCaller.invoke(MethodCaller.java:71)
at com.google.testing.threadtester.BaseThreadedTestRunner.runTests(BaseThreadedTestRunner.java:179)
at com.google.testing.threadtester.BaseThreadedTestRunner.runTests(BaseThreadedTestRunner.java:143)
at DoClassTest.runThreadedTests(DoClassTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.testing.threadtester.MethodCaller.invoke(MethodCaller.java:68)
... 13 more
Caused by: java.lang.IllegalArgumentException: Cannot find method public static void DoClass.doTheThing()
at com.google.testing.threadtester.ClassInstrumentationImpl.getMethod(ClassInstrumentationImpl.java:113)
at com.google.testing.threadtester.InterleavedRunner.getMainMethod(InterleavedRunner.java:116)
at com.google.testing.threadtester.InterleavedRunner.doInterleave(InterleavedRunner.java:131)
at com.google.testing.threadtester.InterleavedRunner.interleave(InterleavedRunner.java:80)
at com.google.testing.threadtester.AnnotatedTestWrapper.runTestCases(AnnotatedTestWrapper.java:258)
at com.google.testing.threadtester.AnnotatedTestWrapper.runTests(AnnotatedTestWrapper.java:242)
... 16 more
Is this a known issue ? Am I doing something wrong ?
Thanks
The old google code repo has two tags for Maven releases. Are there plans to update/move this project maven?
A test that should fail but doesn't.
System under test
public class Counter {
private int count;
public void increment() { count++; }
public int getCount() { return count; }
}
Test class
public class CounterTest {
private Counter sut;
@Test
public void testThreading() {
AnnotatedTestRunner runner = new AnnotatedTestRunner();
runner.runTests(this.getClass(), Counter.class);
}
@ThreadedBefore
public void before() {
sut = new Counter();
}
@ThreadedMain
public void main() {
sut.increment();
}
@ThreadedSecondary
public void secondary() {
sut.increment();
}
@ThreadedAfter
public void after() {
assertEquals(sut.getCount(),2);
}
}
Expected result
Test fails.
Interweaving should allow for the result to be 1 due to race condition at counter++
:
ThreadMain reads 0
ThreadSecondary reads 0
ThreadMain increments to 1
ThreadMain writes 1
ThreadSecondary increments to 1
ThreadSecondary writes 1
Final value for counter 1. Expected value being 2, that should make the test fail.
Actual result
Test passes.
Environment
<dependency>
<groupId>com.googlecode.thread-weaver</groupId>
<artifactId>threadweaver</artifactId>
<version>0.2</version>
</dependency>
$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
$ uname -a
Darwin hostname.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64
Are there any plans to make this library work with Mockito?
Verify that everything has been correctly transferred to Github. Update any registered contributors from https://code.google.com/p/thread-weaver
I'm wondering if the library is able to support Android concurrency testing?
Thank you.
I've modified the first example in a simple fashion. I'm hoping maybe you could incorporate these changes into AnnotatedTestRunner.runTests(...)
:
@Test
public void testThreading() {
final AnnotatedTestRunner runner = new AnnotatedTestRunner();
// Run all Weaver tests in this class, using MyList as the Class Under Test.
try {
runner.runTests(this.getClass(), MyList.class);
} catch (final RuntimeException e) {
final Throwable root = org.apache.commons.lang.exception.ExceptionUtils.ExceptionUtils.getRootCause(e);
if (root instanceof AssertionError)
throw (AssertionError) root;
else
throw e;
}
}
Hi,
The code in current shape implies sequential consistency (due to locking after each byte code instruction). This way the analysis is fully reliable only when there are no data races - as defined in JMM https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4.5
There is work outlining a different approach to interleavings using only locations where the happens-before relation is forced (i.e. lockin / unlocking the same monitor, acess to a volatile variable, etc.). See https://www.cs.rice.edu/~javaplt/papers/ricken-phd-thesis.pdf
This optimization in number of interleavings, enclosing whole critical blocks, could be considered in Thread Weaver - it seems that with already assumed sequential consistency there would be no loss of reliability.
Please, let me know if the above seem a fair statement. I would happily try to tackle this.
Marcin
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.