1 | package geniusweb.protocol.session.mopac;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.mockito.ArgumentMatchers.any;
|
---|
8 | import static org.mockito.ArgumentMatchers.anyLong;
|
---|
9 | import static org.mockito.Mockito.mock;
|
---|
10 | import static org.mockito.Mockito.times;
|
---|
11 | import static org.mockito.Mockito.verify;
|
---|
12 | import static org.mockito.Mockito.when;
|
---|
13 |
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.Collections;
|
---|
17 | import java.util.Date;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | import org.junit.Before;
|
---|
21 | import org.junit.Test;
|
---|
22 |
|
---|
23 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
24 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
25 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
26 |
|
---|
27 | import geniusweb.actions.Action;
|
---|
28 | import geniusweb.actions.Offer;
|
---|
29 | import geniusweb.actions.PartyId;
|
---|
30 | import geniusweb.inform.Inform;
|
---|
31 | import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
|
---|
32 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
|
---|
33 | import geniusweb.protocol.session.mopac.phase.OfferPhase;
|
---|
34 | import geniusweb.protocol.session.mopac.phase.Phase;
|
---|
35 | import geniusweb.protocol.session.mopac.phase.VotingPhase;
|
---|
36 | import tudelft.utilities.logging.Reporter;
|
---|
37 | import tudelft.utilities.repository.NoResourcesNowException;
|
---|
38 |
|
---|
39 | public class MOPACTest {
|
---|
40 |
|
---|
41 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
42 |
|
---|
43 | private static final PartyId PARTY1ID = new PartyId("party1");
|
---|
44 | private static final PartyId PARTY2ID = new PartyId("party2");
|
---|
45 | private static final PartyId PARTY3ID = new PartyId("party3");
|
---|
46 |
|
---|
47 | // don't try to mock settings, all parts are needed and mocking
|
---|
48 | // makes the test unreadable.
|
---|
49 | private final String setstr = "{\"MOPACSettings\":{\"participants\":["
|
---|
50 | + "{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"}]}},"
|
---|
51 | + "{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}},"
|
---|
52 | + "{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party3\",\"parameters\":{}},\"profile\":\"http://profile3\"}]}}],"
|
---|
53 | + "\"deadline\":{\"DeadlineTime\":{\"durationms\":100}},\"votingevaluator\":{\"LargestAgreement\":{}}}}";
|
---|
54 |
|
---|
55 | private MOPACSettings settings;
|
---|
56 | private Reporter logger = mock(Reporter.class);
|
---|
57 | private MOPAC mopac;
|
---|
58 |
|
---|
59 | private final ProtocolToPartyConnFactory factory = mock(
|
---|
60 | ProtocolToPartyConnFactory.class);
|
---|
61 | private final ProtocolToPartyConn conn1 = mock(ProtocolToPartyConn.class),
|
---|
62 | conn2 = mock(ProtocolToPartyConn.class),
|
---|
63 | conn3 = mock(ProtocolToPartyConn.class);
|
---|
64 |
|
---|
65 | private List<ProtocolToPartyConn> connections;
|
---|
66 |
|
---|
67 | @Before
|
---|
68 | public void before() throws JsonParseException, JsonMappingException,
|
---|
69 | IOException, NoResourcesNowException {
|
---|
70 | settings = jackson.readValue(setstr, MOPACSettings.class);
|
---|
71 | mopac = settings.getProtocol(logger);
|
---|
72 |
|
---|
73 | connections = Arrays.asList(conn1, conn2, conn3);
|
---|
74 | when(factory.connect(any(List.class))).thenReturn(connections);
|
---|
75 |
|
---|
76 | // hack the state.with(connection) stuff simplistically
|
---|
77 |
|
---|
78 | when(conn1.getParty()).thenReturn(PARTY1ID);
|
---|
79 | when(conn2.getParty()).thenReturn(PARTY2ID);
|
---|
80 | when(conn3.getParty()).thenReturn(PARTY3ID);
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test
|
---|
84 | public void testSmoke() {
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Test
|
---|
88 | public void testGetDescr() {
|
---|
89 | assertNotNull(mopac.getDescription());
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Test
|
---|
93 | public void testInitialState() {
|
---|
94 | MOPACState state = mopac.getState();
|
---|
95 | assertEquals(settings, state.getSettings());
|
---|
96 | assertTrue(state.getActions().isEmpty());
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void testProtocol() {
|
---|
101 | assertEquals("MOPAC", mopac.getRef().getURI().getPath());
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Test
|
---|
105 | public void testWorkingCheckDeadline()
|
---|
106 | throws IOException, NoResourcesNowException, InterruptedException {
|
---|
107 |
|
---|
108 | mopac.start(factory);
|
---|
109 |
|
---|
110 | assertFalse(mopac.getState().isFinal(System.currentTimeMillis()));
|
---|
111 | // we have deadline in 100ms so check
|
---|
112 | Thread.sleep(200);
|
---|
113 | assertTrue(mopac.getState().isFinal(System.currentTimeMillis()));
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Test
|
---|
117 | public void testSubscribedToConnections() {
|
---|
118 | mopac.start(factory);
|
---|
119 | verify(conn1, times(1)).addListener(any());
|
---|
120 | verify(conn2, times(1)).addListener(any());
|
---|
121 | verify(conn3, times(1)).addListener(any());
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Test
|
---|
125 | public void testActionIsBroadcast()
|
---|
126 | throws IOException, NoResourcesNowException, InterruptedException {
|
---|
127 |
|
---|
128 | mopac.start(factory);
|
---|
129 | Action offer = mock(Offer.class);
|
---|
130 | mopac.actionRequest(conn1, offer, 1l);
|
---|
131 |
|
---|
132 | // 1 time for YourTurn, one time for ActionDone.
|
---|
133 | // but filtering ActionDone here (instead of Action) does not work,
|
---|
134 | // maybe bug in Mockito?
|
---|
135 | verify(conn1, times(2)).send(any(Inform.class));
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Fatal error occurs, run should fail
|
---|
140 | */
|
---|
141 | @Test(expected = RuntimeException.class)
|
---|
142 | public void testPartyFailsConnect()
|
---|
143 | throws IOException, NoResourcesNowException {
|
---|
144 | ProtocolToPartyConnFactory factory1 = mock(
|
---|
145 | ProtocolToPartyConnFactory.class);
|
---|
146 | // irrecoverable fail test
|
---|
147 | when(factory1.connect(any(List.class)))
|
---|
148 | .thenThrow(new IOException("Failed to connect to parties"));
|
---|
149 |
|
---|
150 | mopac.start(factory1);
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Nonfatal error occurs, protocol should retry till success
|
---|
156 | */
|
---|
157 | @Test
|
---|
158 | public void testPartyFailsConnect2ndtimeOK()
|
---|
159 | throws IOException, NoResourcesNowException {
|
---|
160 | long now = System.currentTimeMillis();
|
---|
161 | ProtocolToPartyConnFactory factory1 = mock(
|
---|
162 | ProtocolToPartyConnFactory.class);
|
---|
163 | // recoverable fail, and then success.
|
---|
164 | when(factory1.connect(any(List.class)))
|
---|
165 | .thenThrow(new NoResourcesNowException("try again later",
|
---|
166 | new Date(now + 100)))
|
---|
167 | .thenReturn(connections);
|
---|
168 |
|
---|
169 | mopac.start(factory1);
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Test that in Offer phase, and party does action, the next phase is
|
---|
175 | * entered.
|
---|
176 | */
|
---|
177 | @Test
|
---|
178 | public void testProceedsNextPhase() {
|
---|
179 | // the state before the last party makes the offer
|
---|
180 | MOPACState offerstate = mock(MOPACState.class);
|
---|
181 | when(offerstate.getSettings()).thenReturn(settings);
|
---|
182 | OfferPhase offerphase = mock(OfferPhase.class);
|
---|
183 | when(offerphase.isFinal(anyLong())).thenReturn(false);
|
---|
184 | when(offerstate.getPhase()).thenReturn(offerphase);
|
---|
185 |
|
---|
186 | // the phase where all parties placed an offer
|
---|
187 | OfferPhase finalofferphase = mock(OfferPhase.class);
|
---|
188 | when(finalofferphase.isFinal(anyLong())).thenReturn(true);
|
---|
189 | MOPACState finalofferstate = mock(MOPACState.class);
|
---|
190 | when(finalofferstate.getPhase()).thenReturn(finalofferphase);
|
---|
191 | when(finalofferstate.finishPhase()).thenReturn(finalofferstate);
|
---|
192 | when(finalofferstate.isFinal(anyLong())).thenReturn(false);
|
---|
193 |
|
---|
194 | when(offerstate.with(any(PartyId.class), any(Action.class), anyLong()))
|
---|
195 | .thenReturn(finalofferstate);
|
---|
196 |
|
---|
197 | // the phase where all parties can start voting.
|
---|
198 | // this is the state to be reached.
|
---|
199 | long now = System.currentTimeMillis();
|
---|
200 | MOPACState votingstate = mock(MOPACState.class);
|
---|
201 | Phase votingphase = mock(VotingPhase.class);
|
---|
202 |
|
---|
203 | PartyStates votingpartystates = mock(PartyStates.class);
|
---|
204 | when(votingpartystates.getNotYetActed())
|
---|
205 | .thenReturn(Collections.emptySet());
|
---|
206 | when(votingphase.getDeadline()).thenReturn(now + 100);
|
---|
207 | when(votingphase.getPartyStates()).thenReturn(votingpartystates);
|
---|
208 | when(votingstate.getPhase()).thenReturn(votingphase);
|
---|
209 |
|
---|
210 | when(finalofferstate.nextPhase(anyLong())).thenReturn(votingstate);
|
---|
211 |
|
---|
212 | mopac = new MOPAC(offerstate, logger);
|
---|
213 | Offer action = mock(Offer.class);
|
---|
214 | mopac.actionRequest(conn3, action, now);
|
---|
215 |
|
---|
216 | // check that the action result in reaching the voting phase
|
---|
217 | assertEquals(votingstate, mopac.getState());
|
---|
218 | }
|
---|
219 |
|
---|
220 | }
|
---|