[1] | 1 | package negotiator.session;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.mockito.Mockito.mock;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.net.MalformedURLException;
|
---|
| 8 | import java.net.URL;
|
---|
| 9 | import java.util.ArrayList;
|
---|
| 10 | import java.util.Arrays;
|
---|
| 11 | import java.util.Collection;
|
---|
| 12 | import java.util.List;
|
---|
| 13 |
|
---|
| 14 | import org.junit.After;
|
---|
| 15 | import org.junit.Before;
|
---|
| 16 | import org.junit.Test;
|
---|
| 17 | import org.junit.runner.RunWith;
|
---|
| 18 | import org.junit.runners.Parameterized;
|
---|
| 19 | import org.junit.runners.Parameterized.Parameters;
|
---|
| 20 |
|
---|
| 21 | import agents.nastyagent.BadBid;
|
---|
| 22 | import agents.nastyagent.BadSuperInit;
|
---|
| 23 | import agents.nastyagent.NastyAgent;
|
---|
| 24 | import agents.nastyagent.NearlyOutOfMem;
|
---|
| 25 | import agents.nastyagent.NonsenseActionInChoose;
|
---|
| 26 | import agents.nastyagent.NullBid;
|
---|
| 27 | import agents.nastyagent.OnlyBestBid;
|
---|
| 28 | import agents.nastyagent.OutOfMem;
|
---|
| 29 | import agents.nastyagent.SleepInChoose;
|
---|
| 30 | import agents.nastyagent.SleepInNegoEnd;
|
---|
| 31 | import agents.nastyagent.SleepInReceiveMessage;
|
---|
| 32 | import agents.nastyagent.StoreNull;
|
---|
| 33 | import agents.nastyagent.StoreUnserializableThing;
|
---|
| 34 | import agents.nastyagent.ThrowInChoose;
|
---|
| 35 | import agents.nastyagent.ThrowInConstructor;
|
---|
| 36 | import agents.nastyagent.ThrowInNegoEnd;
|
---|
| 37 | import agents.nastyagent.ThrowInReceiveMessage;
|
---|
| 38 | import agents.nastyagent.UnknownValue;
|
---|
| 39 | import genius.core.AgentID;
|
---|
| 40 | import genius.core.Deadline;
|
---|
| 41 | import genius.core.DeadlineType;
|
---|
| 42 | import genius.core.events.BrokenPartyException;
|
---|
| 43 | import genius.core.events.NegotiationEvent;
|
---|
| 44 | import genius.core.events.SessionFailedEvent;
|
---|
| 45 | import genius.core.exceptions.InstantiateException;
|
---|
| 46 | import genius.core.exceptions.NegotiatorException;
|
---|
| 47 | import genius.core.listener.Listener;
|
---|
| 48 | import genius.core.parties.NegotiationParty;
|
---|
| 49 | import genius.core.parties.NegotiationPartyInternal;
|
---|
| 50 | import genius.core.parties.SessionsInfo;
|
---|
| 51 | import genius.core.persistent.PersistentDataType;
|
---|
| 52 | import genius.core.protocol.MultilateralProtocol;
|
---|
| 53 | import genius.core.protocol.StackedAlternatingOffersProtocol;
|
---|
| 54 | import genius.core.repository.DomainRepItem;
|
---|
| 55 | import genius.core.repository.PartyRepItem;
|
---|
| 56 | import genius.core.repository.ProfileRepItem;
|
---|
| 57 | import genius.core.session.ExecutorWithTimeout;
|
---|
| 58 | import genius.core.session.RepositoryException;
|
---|
| 59 | import genius.core.session.Session;
|
---|
| 60 | import genius.core.session.SessionConfiguration;
|
---|
| 61 | import 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)
|
---|
| 71 | public 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 | }
|
---|