source: src/test/java/negotiator/session/SessionManagerTest.java@ 37

Last change on this file since 37 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 9.5 KB
Line 
1package negotiator.session;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5
6import java.io.IOException;
7import java.net.MalformedURLException;
8import java.net.URL;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.List;
13
14import org.junit.After;
15import org.junit.Before;
16import org.junit.Test;
17import org.junit.runner.RunWith;
18import org.junit.runners.Parameterized;
19import org.junit.runners.Parameterized.Parameters;
20
21import agents.nastyagent.BadBid;
22import agents.nastyagent.BadSuperInit;
23import agents.nastyagent.NastyAgent;
24import agents.nastyagent.NearlyOutOfMem;
25import agents.nastyagent.NonsenseActionInChoose;
26import agents.nastyagent.NullBid;
27import agents.nastyagent.OnlyBestBid;
28import agents.nastyagent.OutOfMem;
29import agents.nastyagent.SleepInChoose;
30import agents.nastyagent.SleepInNegoEnd;
31import agents.nastyagent.SleepInReceiveMessage;
32import agents.nastyagent.StoreNull;
33import agents.nastyagent.StoreUnserializableThing;
34import agents.nastyagent.ThrowInChoose;
35import agents.nastyagent.ThrowInConstructor;
36import agents.nastyagent.ThrowInNegoEnd;
37import agents.nastyagent.ThrowInReceiveMessage;
38import agents.nastyagent.UnknownValue;
39import genius.core.AgentID;
40import genius.core.Deadline;
41import genius.core.DeadlineType;
42import genius.core.events.BrokenPartyException;
43import genius.core.events.NegotiationEvent;
44import genius.core.events.SessionFailedEvent;
45import genius.core.exceptions.InstantiateException;
46import genius.core.exceptions.NegotiatorException;
47import genius.core.listener.Listener;
48import genius.core.parties.NegotiationParty;
49import genius.core.parties.NegotiationPartyInternal;
50import genius.core.parties.SessionsInfo;
51import genius.core.persistent.PersistentDataType;
52import genius.core.protocol.MultilateralProtocol;
53import genius.core.protocol.StackedAlternatingOffersProtocol;
54import genius.core.repository.DomainRepItem;
55import genius.core.repository.PartyRepItem;
56import genius.core.repository.ProfileRepItem;
57import genius.core.session.ExecutorWithTimeout;
58import genius.core.session.RepositoryException;
59import genius.core.session.Session;
60import genius.core.session.SessionConfiguration;
61import genius.core.session.SessionManager;
62
63/**
64 * Semi-end-to-end test. We use real {@link NegotiationParty} to test the
65 * {@link SessionManager}.
66 *
67 * @author W.Pasman
68 *
69 */
70@RunWith(Parameterized.class)
71public class SessionManagerTest {
72 private static final Class<? extends NegotiationParty> OPPONENT = OnlyBestBid.class;
73
74 @Parameters
75 public static Collection<Object[]> data() {
76 return Arrays.asList(new Object[][] { { OPPONENT, null }, { BadBid.class, BrokenPartyException.class },
77 { BadSuperInit.class, InstantiateException.class },
78 { NearlyOutOfMem.class, BrokenPartyException.class },
79 { NonsenseActionInChoose.class, BrokenPartyException.class },
80 { NullBid.class, BrokenPartyException.class }, { OutOfMem.class, BrokenPartyException.class },
81 { SleepInChoose.class, BrokenPartyException.class },
82 // This one seems to hang the system!
83 // { SleepInConstructor.class,
84 // NullPointerException.class },
85 // { SleepInInit.class, NullPointerException.class },
86 { SleepInReceiveMessage.class, BrokenPartyException.class },
87 { ThrowInChoose.class, BrokenPartyException.class },
88 { ThrowInConstructor.class, InstantiateException.class },
89 { ThrowInReceiveMessage.class, BrokenPartyException.class }, { UnknownValue.class, null },
90
91 { SleepInNegoEnd.class, null }, { ThrowInNegoEnd.class, null }, { StoreNull.class, null },
92 { StoreUnserializableThing.class, null },
93
94 });
95 }
96
97 final String RESOURCES = "file:src/test/resources/";
98 final String domain = RESOURCES + "partydomain/party_domain.xml";
99 final String profile = RESOURCES + "partydomain/party1_utility.xml";
100 private DomainRepItem domainRepItem;
101 private Session session;
102 private MultilateralProtocol protocol = new StackedAlternatingOffersProtocol();
103 private ExecutorWithTimeout executor = new ExecutorWithTimeout(3000);
104 private ProfileRepItem profileRepItem;
105
106 private Class<? extends NegotiationParty> partyClass;
107 private Class<? extends Exception> expectedExceptionClass;
108 private Exception actualException;
109 private SessionsInfo info;
110
111 @Before
112 public void before() throws IOException {
113 this.info = new SessionsInfo(null, PersistentDataType.DISABLED, true);
114 }
115
116 @After
117 public void after() {
118 info.close();
119 }
120
121 /**
122 * Test if running the given partyClass against {@link OnlyBestBid} gives us
123 * the correct behaviour.
124 *
125 * @param partyClass
126 * a class extending {@link NegotiationParty} that is to be used
127 * as first agent in a nego.
128 * @param eClass
129 * the class of the expected {@link Exception}
130 * @param n
131 * @throws IOException
132 */
133 public SessionManagerTest(Class<? extends NegotiationParty> partyClass, Class<? extends Exception> eClass)
134 throws IOException {
135 this.partyClass = partyClass;
136 this.expectedExceptionClass = eClass;
137 System.out.println("Running " + partyClass);
138 }
139
140 @Test
141 public void testRun() throws IOException {
142 session = new Session(new Deadline(180, DeadlineType.ROUND),
143 new SessionsInfo(protocol, PersistentDataType.DISABLED, true));
144 domainRepItem = new DomainRepItem(new URL(domain));
145 profileRepItem = new ProfileRepItem(new URL(profile), domainRepItem);
146
147 try {
148 List<NegotiationPartyInternal> theparties = generateParties(partyClass);
149 SessionManager sessionMgr = new SessionManager(mock(SessionConfiguration.class), theparties, session,
150 executor);
151 sessionMgr.addListener(new Listener<NegotiationEvent>() {
152
153 @Override
154 public void notifyChange(NegotiationEvent evt) {
155 if (evt instanceof SessionFailedEvent) {
156 actualException = ((SessionFailedEvent) evt).getException();
157 }
158 }
159 });
160 sessionMgr.runAndWait();
161
162 } catch (Exception e) {
163 actualException = e;
164 }
165 checkOutcome();
166 }
167
168 /**
169 * Check if always {@link NegotiationParty#negotiationEnded(genius.core.Bid)}
170 * is called after the nego (if the party was generated ok to start with).
171 *
172 * @throws IOException
173 *
174 * @throws InstantiateException
175 * @throws MalformedURLException
176 *
177 */
178 @Test
179 public void testEndingProperly() throws IOException {
180 List<NegotiationPartyInternal> theparties = new ArrayList<>();
181 try {
182 session = new Session(new Deadline(180, DeadlineType.ROUND),
183 new SessionsInfo(protocol, PersistentDataType.DISABLED, true));
184 domainRepItem = new DomainRepItem(new URL(domain));
185 profileRepItem = new ProfileRepItem(new URL(profile), domainRepItem);
186 theparties = generateParties(partyClass);
187 } catch (MalformedURLException | InstantiateException e) {
188 return; // crashes here are checked in #testRun
189 }
190
191 // if we get here, session starts. It should then always END properly
192 SessionManager sessionMgr = new SessionManager(mock(SessionConfiguration.class), theparties, session, executor);
193 try {
194 sessionMgr.run();
195 } catch (RuntimeException e) {
196 // if it errs is not relevant in this test. What matters is if
197 // negotioationEnded is called.
198 }
199 checkEndedProperly(theparties);
200 }
201
202 /**
203 * Check if all parties received the negotiationEnded call
204 *
205 * @param theparties
206 */
207 private void checkEndedProperly(List<NegotiationPartyInternal> theparties) {
208 for (NegotiationPartyInternal party : theparties) {
209 if (!((NastyAgent) (party.getParty())).isEnded()) {
210 throw new IllegalStateException("Agent " + party + " did not receive final negotiationEnded call");
211 }
212 }
213 }
214
215 private void checkOutcome() {
216 boolean ok;
217 Class<? extends Exception> actualExClass = actualException != null ? actualException.getClass() : null;
218 if (expectedExceptionClass != null) {
219 ok = expectedExceptionClass.equals(actualExClass);
220 } else {
221 ok = null == actualException;
222 }
223 if (!ok) {
224 System.err.println("Failure running " + partyClass + ": expected " + string(expectedExceptionClass)
225 + " but got " + string(actualExClass));
226 if (actualException != null) {
227 actualException.printStackTrace();
228 }
229 assertEquals(expectedExceptionClass, actualExClass);
230 }
231 }
232
233 String string(Class<? extends Exception> eClass) {
234 return eClass == null ? "No exception" : eClass.toString();
235 }
236
237 private List<NegotiationPartyInternal> generateParties(Class<? extends NegotiationParty> partyClass)
238 throws InstantiateException {
239 ArrayList<NegotiationPartyInternal> parties = new ArrayList<NegotiationPartyInternal>();
240 try {
241 parties.add(createParty(partyClass));
242 parties.add(createParty(OPPONENT));
243 } catch (MalformedURLException | IllegalAccessException | ClassNotFoundException | RepositoryException
244 | NegotiatorException | InstantiationException e) {
245 throw new InstantiateException("Failed to create party " + partyClass, e);
246 }
247 return parties;
248 }
249
250 /**
251 * Create a real party based on the class
252 *
253 * @param partyClass
254 * @return {@link NegotiationPartyInternal}
255 * @throws InstantiateException
256 * if party can't be instantiated
257 */
258 private NegotiationPartyInternal createParty(Class<? extends NegotiationParty> partyClass)
259 throws MalformedURLException, InstantiationException, IllegalAccessException, ClassNotFoundException,
260 RepositoryException, NegotiatorException, InstantiateException {
261 PartyRepItem partyRepItem = new PartyRepItem(partyClass.getCanonicalName());
262
263 return new NegotiationPartyInternal(partyRepItem, profileRepItem, session, info, getAgentID(partyClass), null);
264 }
265
266 private AgentID getAgentID(Class<? extends NegotiationParty> partyClass) {
267 return new AgentID(partyClass.getName());
268 }
269}
Note: See TracBrowser for help on using the repository browser.