Skip to content

Commit ce43d80

Browse files
tomekl007pedja4
authored andcommitted
BAEL-855 stm (eugenp#1855)
* BAEL-855 code for the STM article * BAEL-855 method ordering * BAEL-855 Better test case * BAEL-855 formatting * BAEL-855 rename * BAEL-855 change to expected * Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-855_stm # Conflicts: # libraries/pom.xml
1 parent ee79ac3 commit ce43d80

3 files changed

Lines changed: 213 additions & 15 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<parent>
55
<artifactId>parent-modules</artifactId>
66
<groupId>com.baeldung</groupId>
@@ -236,7 +236,6 @@
236236
<artifactId>datanucleus-xml</artifactId>
237237
<version>5.0.0-release</version>
238238
</dependency>
239-
240239
<dependency>
241240
<groupId>org.springframework</groupId>
242241
<artifactId>spring-web</artifactId>
@@ -254,21 +253,25 @@
254253
<version>3.0.3</version>
255254
<scope>test</scope>
256255
</dependency>
257-
<dependency>
258-
<groupId>com.zaxxer</groupId>
259-
<artifactId>HikariCP</artifactId>
260-
<version>2.6.1</version>
261-
<scope>compile</scope>
262-
</dependency>
263-
<dependency>
264-
<groupId>org.postgresql</groupId>
265-
<artifactId>postgresql</artifactId>
266-
<version>42.0.0</version>
267-
</dependency>
268-
256+
<dependency>
257+
<groupId>org.multiverse</groupId>
258+
<artifactId>multiverse-core</artifactId>
259+
<version>${multiverse.version}</version>
260+
</dependency>
261+
<dependency>
262+
<groupId>com.zaxxer</groupId>
263+
<artifactId>HikariCP</artifactId>
264+
<version>2.6.1</version>
265+
<scope>compile</scope>
266+
</dependency>
267+
<dependency>
268+
<groupId>org.postgresql</groupId>
269+
<artifactId>postgresql</artifactId>
270+
<version>42.0.0</version>
271+
</dependency>
269272
</dependencies>
270-
271273
<properties>
274+
<multiverse.version>0.7.0</multiverse.version>
272275
<cglib.version>3.2.4</cglib.version>
273276
<commons-lang.version>3.5</commons-lang.version>
274277
<jasypt.version>1.9.2</jasypt.version>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.stm;
2+
3+
import org.multiverse.api.StmUtils;
4+
import org.multiverse.api.callables.TxnCallable;
5+
import org.multiverse.api.references.TxnInteger;
6+
import org.multiverse.api.references.TxnLong;
7+
8+
public class Account {
9+
10+
private final TxnLong lastUpdate;
11+
private final TxnInteger balance;
12+
13+
public Account(final int balance) {
14+
this.lastUpdate = StmUtils.newTxnLong(System.currentTimeMillis());
15+
this.balance = StmUtils.newTxnInteger(balance);
16+
}
17+
18+
public Integer getBalance() {
19+
return balance.atomicGet();
20+
}
21+
22+
public void adjustBy(final int amount) {
23+
adjustBy(amount, System.currentTimeMillis());
24+
}
25+
26+
public void adjustBy(final int amount, final long date) {
27+
StmUtils.atomic(() -> {
28+
balance.increment(amount);
29+
lastUpdate.set(date);
30+
31+
if (balance.get() < 0) {
32+
throw new IllegalArgumentException("Not enough money");
33+
}
34+
});
35+
}
36+
37+
public void transferTo(final Account other, final int amount) {
38+
StmUtils.atomic(() -> {
39+
final long date = System.currentTimeMillis();
40+
adjustBy(-amount, date);
41+
other.adjustBy(amount, date);
42+
});
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return StmUtils.atomic((TxnCallable<String>)
48+
txn -> "Balance: " + balance.get(txn) + " lastUpdateDate: " + lastUpdate.get(txn));
49+
}
50+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.baeldung.stm;
2+
3+
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
8+
import java.util.concurrent.CountDownLatch;
9+
import java.util.concurrent.ExecutorService;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.atomic.AtomicBoolean;
13+
14+
import static org.assertj.core.api.Java6Assertions.assertThat;
15+
import static org.junit.Assert.assertTrue;
16+
17+
public class AccountTest {
18+
19+
@Test
20+
public void givenAccount_whenDecrement_thenShouldReturnProperValue() {
21+
//given
22+
Account a = new Account(10);
23+
24+
//when
25+
a.adjustBy(-5);
26+
27+
//then
28+
assertThat(a.getBalance()).isEqualTo(5);
29+
}
30+
31+
@Test(expected = IllegalArgumentException.class)
32+
public void givenAccount_whenDecrementTooMuch_thenShouldThrow() {
33+
//given
34+
Account a = new Account(10);
35+
36+
//when
37+
a.adjustBy(-11);
38+
}
39+
40+
@Test
41+
public void givenTwoThreads_whenBothApplyOperation_thenShouldThrow() throws InterruptedException {
42+
//given
43+
ExecutorService ex = Executors.newFixedThreadPool(2);
44+
Account a = new Account(10);
45+
CountDownLatch countDownLatch = new CountDownLatch(1);
46+
AtomicBoolean exceptionThrown = new AtomicBoolean(false);
47+
48+
//when
49+
ex.submit(() -> {
50+
try {
51+
countDownLatch.await();
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
56+
try {
57+
a.adjustBy(-6);
58+
} catch (IllegalArgumentException e) {
59+
exceptionThrown.set(true);
60+
}
61+
});
62+
ex.submit(() -> {
63+
try {
64+
countDownLatch.await();
65+
} catch (InterruptedException e) {
66+
e.printStackTrace();
67+
}
68+
try {
69+
a.adjustBy(-5);
70+
} catch (IllegalArgumentException e) {
71+
exceptionThrown.set(true);
72+
}
73+
});
74+
75+
countDownLatch.countDown();
76+
ex.awaitTermination(1, TimeUnit.SECONDS);
77+
ex.shutdown();
78+
79+
//then
80+
assertTrue(exceptionThrown.get());
81+
}
82+
83+
@Test
84+
public void givenTwoAccounts_whenFailedWhileTransferring_thenShouldRollbackTransaction() {
85+
//given
86+
final Account a = new Account(10);
87+
final Account b = new Account(10);
88+
89+
//when
90+
a.transferTo(b, 5);
91+
92+
//then
93+
assertThat(a.getBalance()).isEqualTo(5);
94+
assertThat(b.getBalance()).isEqualTo(15);
95+
96+
//and
97+
try {
98+
a.transferTo(b, 20);
99+
} catch (final IllegalArgumentException e) {
100+
System.out.println("failed to transfer money");
101+
}
102+
103+
//then
104+
assertThat(a.getBalance()).isEqualTo(5);
105+
assertThat(b.getBalance()).isEqualTo(15);
106+
}
107+
108+
@Test
109+
public void givenTwoThreads_whenBothTryToTransfer_thenShouldNotDeadlock() throws InterruptedException {
110+
//given
111+
ExecutorService ex = Executors.newFixedThreadPool(2);
112+
final Account a = new Account(10);
113+
final Account b = new Account(10);
114+
CountDownLatch countDownLatch = new CountDownLatch(1);
115+
116+
//when
117+
ex.submit(() -> {
118+
try {
119+
countDownLatch.await();
120+
} catch (InterruptedException e) {
121+
e.printStackTrace();
122+
}
123+
a.transferTo(b, 10);
124+
});
125+
ex.submit(() -> {
126+
try {
127+
countDownLatch.await();
128+
} catch (InterruptedException e) {
129+
e.printStackTrace();
130+
}
131+
b.transferTo(a, 1);
132+
133+
});
134+
135+
countDownLatch.countDown();
136+
ex.awaitTermination(1, TimeUnit.SECONDS);
137+
ex.shutdown();
138+
139+
//then
140+
assertThat(a.getBalance()).isEqualTo(1);
141+
assertThat(b.getBalance()).isEqualTo(19);
142+
}
143+
144+
145+
}

0 commit comments

Comments
 (0)