-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyFactoryTest.java
More file actions
79 lines (68 loc) · 2.45 KB
/
LazyFactoryTest.java
File metadata and controls
79 lines (68 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package ru.spbau.mit.alyokhina;
import org.junit.Test;
import java.util.function.Supplier;
import static org.junit.Assert.*;
public class LazyFactoryTest {
@Test
public void testCreateLazySingleThreadMode() {
Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(() -> 5);
assertEquals((Integer) 5, test.get());
assertEquals((Integer) 5, test.get());
}
@Test
public void testCreateLazySingleThreadModeIfSupplierGetNull() {
Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(() -> null);
assertEquals(null, test.get());
assertEquals(null, test.get());
}
@Test
public void testCreateLazySingleThreadModeIfSupplierChangeValue() {
Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(new Supplier<Integer>() {
private boolean flag = false;
@Override
public Integer get() {
Integer ans = flag ? 5 : 6;
flag = true;
return ans;
}
});
assertEquals((Integer) 6, test.get());
assertEquals((Integer) 6, test.get());
}
@Test
public void testCreateLazyMultiThreadedModeForOneThread() {
Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(() -> 5);
assertEquals((Integer) 5, test.get());
assertEquals((Integer) 5, test.get());
}
@Test
public void testCreateLazyMultiThreadedModeForThreads() {
Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(() -> 5);
Thread[] threads = new Thread[1000];
for (int i = 0; i < 1000; i++) {
threads[i] = new Thread(() -> assertEquals((Integer) 5, test.get()));
}
for (Thread thread : threads) {
thread.run();
}
}
@Test
public void testCreateLazyMultiThreadedModeForThreadsIfSupplierChange() throws Exception {
Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(new Supplier<Integer>() {
private boolean flag = false;
@Override
public Integer get() {
Integer ans = flag ? 5 : 6;
flag = true;
return ans;
}
});
Thread[] threads = new Thread[1000];
for (int i = 0; i < 1000; i++) {
threads[i] = new Thread(() -> assertEquals((Integer) 6, test.get()));
}
for (Thread thread : threads) {
thread.run();
}
}
}