Skip to content

Commit 4bcb056

Browse files
committed
transfering money tests
1 parent 770548d commit 4bcb056

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

src/main/java/LockExecutor.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.concurrent.locks.ReadWriteLock;
2+
import java.util.function.Supplier;
3+
4+
/**
5+
* Created by mtumilowicz on 2019-01-07.
6+
*/
7+
public class LockExecutor {
8+
private final ReadWriteLock lock;
9+
10+
LockExecutor(ReadWriteLock lock) {
11+
this.lock = lock;
12+
}
13+
14+
public void write(Runnable action) {
15+
lock.writeLock().lock();
16+
try {
17+
action.run();
18+
} finally {
19+
lock.writeLock().unlock();
20+
}
21+
}
22+
23+
public <T> T read(Supplier<T> action) {
24+
lock.readLock().lock();
25+
try {
26+
return action.get();
27+
} finally {
28+
lock.readLock().unlock();
29+
}
30+
}
31+
}

src/main/java/User.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import lombok.Value;
2+
3+
/**
4+
* Created by mtumilowicz on 2019-01-26.
5+
*/
6+
@Value
7+
class User {
8+
int id;
9+
int balance;
10+
11+
User income(int value) {
12+
return new User(id, balance + value);
13+
}
14+
15+
User outcome(int value) {
16+
return new User(id, balance - value);
17+
}
18+
}

src/main/java/XXX.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/test/java/XXXTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import org.junit.Test;
2+
3+
import java.util.concurrent.locks.ReadWriteLock;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
import java.util.function.Function;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
import static org.hamcrest.CoreMatchers.is;
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
12+
/**
13+
* Created by mtumilowicz on 2019-01-25.
14+
*/
15+
public class XXXTest {
16+
17+
@Test
18+
public void transfer() {
19+
var userMap = Stream.of(new User(1, 40), new User(2, 33))
20+
.collect(Collectors.toMap(User::getId, Function.identity()));
21+
22+
ReadWriteLock lock = new ReentrantReadWriteLock();
23+
LockExecutor executor = new LockExecutor(lock);
24+
25+
executor.write(() -> {
26+
int transfer = 15;
27+
userMap.replace(1, userMap.get(1).outcome(transfer));
28+
userMap.replace(2, userMap.get(2).income(transfer));
29+
});
30+
31+
assertThat(userMap.get(1).getBalance(), is(25));
32+
assertThat(userMap.get(2).getBalance(), is(48));
33+
}
34+
35+
@Test
36+
public void read() {
37+
var userMap = Stream.of(new User(1, 40), new User(2, 33))
38+
.collect(Collectors.toMap(User::getId, Function.identity()));
39+
40+
ReadWriteLock lock = new ReentrantReadWriteLock();
41+
LockExecutor executor = new LockExecutor(lock);
42+
43+
var balanceAll = executor.read(() -> userMap.values().stream().map(User::getBalance).mapToInt(x -> x).sum());
44+
45+
assertThat(balanceAll, is(73));
46+
}
47+
}

0 commit comments

Comments
 (0)