Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Non-thread counter doesn't fail #10

Open
parml opened this issue Mar 20, 2018 · 1 comment
Open

Non-thread counter doesn't fail #10

parml opened this issue Mar 20, 2018 · 1 comment

Comments

@parml
Copy link

parml commented Mar 20, 2018

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
@parml
Copy link
Author

parml commented Mar 20, 2018

Attaching sample maven project.
sample-project.zip

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant