google / thread-weaver

A Java framework for testing multithreaded code.
Apache License 2.0
305 stars 68 forks source link

Cannot instrument static methods #5

Open CappCorp opened 9 years ago

CappCorp commented 9 years ago

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

CappCorp commented 9 years ago

No update on this ticket, has the project been move somewhere else or is it still supported from here ?

Thanks.

alasdairmackintosh commented 9 years ago

Sorry - I must have missed this the first time around.

I'm afraid there's currently no support for static methods. The logic that handles breakpoints is built around the assumption that we are invoking non-static methods on a specific object being tested. (And the logic for interleaving threads of execution is built on breakpoints, which allow us to pause one thread and run another.)

I'm not sure what would break if this assumption was removed. I will try and investigate, but it might not be feasible.

Alasdair

CappCorp commented 9 years ago

OK, thanks for the answer.

I hope it is feasible, I tried having a look at the instrumentation but I'm really not familiar enough with that kind of procedure and could not figure out the limitations of the mechanism.

Please keep me posted on your investigation, thanks in advance :)

CappCorp commented 9 years ago

Hello, Any update so far ?