source: protocol/src/test/java/geniusweb/protocol/session/mopac/MOPACTest.java@ 32

Last change on this file since 32 was 32, checked in by bart, 3 years ago

Multiple learns with repeated tournament, maven use https.

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