Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions libraries-testing-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@
<artifactId>javalite-common</artifactId>
<version>${javalite.version}</version>
</dependency>
<dependency>
<groupId>com.vmlens</groupId>
<artifactId>api</artifactId>
<version>${vmlens.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -218,6 +224,24 @@
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>com.vmlens</groupId>
<artifactId>vmlens-maven-plugin</artifactId>
<version>${vmlens.version}</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/BankAccountTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -238,5 +262,6 @@
<truth.version>0.32</truth.version>
<jgotesting.version>0.12</jgotesting.version>
<javalite.version>1.4.13</javalite.version>
<vmlens.version>1.2.10</vmlens.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.vmlens;

public class AtomicBankAccount {

private final Object LOCK = new Object();
private volatile int amount;

public int getAmount() {
return amount;
}

public void update(int delta) {
synchronized (LOCK) {
amount += delta;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.vmlens;

public class RegularFieldBankAccount {

private int amount;

public int getAmount() {
return amount;
}

public void update(int delta) {
amount += delta;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.vmlens;

public class VolatileFieldBankAccount {

private volatile int amount;

public int getAmount() {
return amount;
}

public void update(int delta) {
amount += delta;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.baeldung.vmlens;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;

import org.junit.jupiter.api.Test;

import com.vmlens.api.AllInterleavings;

/**
* This test tests the different implementations of a concurrent bank account class.+
* To see what is wrong with RegularFieldBankAccount and VolatileFieldBankAccount replace
* AtomicBankAccount with one of those classes.
*
*/

class BankAccountTest {

@Test
public void whenParallelUpdate_thenAmountSumOfBothUpdates() throws InterruptedException {
try (AllInterleavings allInterleavings = new AllInterleavings("bankAccount.updateUpdate")) {
while (allInterleavings.hasNext()) {
AtomicBankAccount bankAccount = new AtomicBankAccount();

Thread first = new Thread() {
@Override
public void run() {
bankAccount.update(5);
}
};
first.start();
bankAccount.update(10);
first.join();

int amount = bankAccount.getAmount();
assertThat(amount, is(15));
}
}
}

@Test
public void whenParallelUpdateAndGet_thenResultEitherAmountBeforeOrAfterUpdate() throws InterruptedException {
try (AllInterleavings allInterleavings = new AllInterleavings("bankAccount.updateGetAmount")) {
while (allInterleavings.hasNext()) {
AtomicBankAccount bankAccount = new AtomicBankAccount();

Thread first = new Thread() {
@Override
public void run() {
bankAccount.update(5);
}
};
first.start();

int amount = bankAccount.getAmount();

assertThat(amount, anyOf(is(0), is(5)));
first.join();
}
}
}
}