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

Non-thread counter doesn't fail #10

@parml

Description

@parml

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions