1 | package geniusweb.partiesserver.repository;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertNull;
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 | import static org.mockito.Mockito.mock;
|
---|
6 | import static org.mockito.Mockito.when;
|
---|
7 |
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.junit.Test;
|
---|
12 |
|
---|
13 | import geniusweb.actions.PartyId;
|
---|
14 | import tudelft.utilities.logging.Reporter;
|
---|
15 | import tudelft.utilities.repository.NoResourcesNowException;
|
---|
16 |
|
---|
17 | public class RunningPartiesRepoTest {
|
---|
18 | @Test
|
---|
19 | public void smokeTest() {
|
---|
20 | new RunningPartiesRepo(mock(Reporter.class));
|
---|
21 | }
|
---|
22 |
|
---|
23 | @Test
|
---|
24 | public void testSimpleParallelPuts() throws InterruptedException {
|
---|
25 | RunningPartiesRepo repo = new RunningPartiesRepo(mock(Reporter.class));
|
---|
26 |
|
---|
27 | List<SimpleThread> threads = new LinkedList<SimpleThread>();
|
---|
28 | for (int n = 0; n < RunningPartiesRepo.MAX_SLOTS; n++) {
|
---|
29 | RunningParty newParty = mock(RunningParty.class);
|
---|
30 | when(newParty.getID()).thenReturn(new PartyId("party" + n));
|
---|
31 |
|
---|
32 | SimpleThread thread = new SimpleThread() {
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public void run1() throws NoResourcesNowException {
|
---|
36 | try {
|
---|
37 | Thread.sleep(30);
|
---|
38 | } catch (InterruptedException e) {
|
---|
39 | // TODO Auto-generated catch block
|
---|
40 | e.printStackTrace();
|
---|
41 | }
|
---|
42 | repo.put(newParty);
|
---|
43 | }
|
---|
44 | };
|
---|
45 | threads.add(thread);
|
---|
46 | thread.start();
|
---|
47 | }
|
---|
48 |
|
---|
49 | for (SimpleThread thread : threads) {
|
---|
50 | thread.join();
|
---|
51 | assertNull(thread.getException());
|
---|
52 | }
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Test
|
---|
57 | public void testTooManyParallelPuts() throws InterruptedException {
|
---|
58 | RunningPartiesRepo repo = new RunningPartiesRepo(mock(Reporter.class));
|
---|
59 |
|
---|
60 | List<SimpleThread> threads = new LinkedList<SimpleThread>();
|
---|
61 | for (int n = 0; n < RunningPartiesRepo.MAX_SLOTS + 1; n++) {
|
---|
62 | RunningParty newParty = mock(RunningParty.class);
|
---|
63 | when(newParty.getID()).thenReturn(new PartyId("party" + n));
|
---|
64 |
|
---|
65 | SimpleThread thread = new SimpleThread() {
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void run1()
|
---|
69 | throws InterruptedException, NoResourcesNowException {
|
---|
70 | Thread.sleep(30);
|
---|
71 | repo.put(newParty);
|
---|
72 | }
|
---|
73 | };
|
---|
74 | threads.add(thread);
|
---|
75 | thread.start();
|
---|
76 | }
|
---|
77 |
|
---|
78 | for (SimpleThread thread : threads) {
|
---|
79 | thread.join();
|
---|
80 | }
|
---|
81 |
|
---|
82 | assertTrue("One of the threads should have bounced", threads.stream()
|
---|
83 | .anyMatch(thread -> thread.getException() != null));
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Thread that stores exceptions so that we can later check what happened.
|
---|
91 | * Override {@link #run1()} instead of {@link #run()}
|
---|
92 | */
|
---|
93 | abstract class SimpleThread extends Thread {
|
---|
94 | private Exception exception = null;
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void run() {
|
---|
98 | try {
|
---|
99 | run1();
|
---|
100 | } catch (Exception e) {
|
---|
101 | this.exception = e;
|
---|
102 | e.printStackTrace();
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public Exception getException() {
|
---|
107 | return exception;
|
---|
108 | }
|
---|
109 |
|
---|
110 | abstract void run1() throws Exception;
|
---|
111 | }
|
---|