[88] | 1 | from datetime import datetime
|
---|
| 2 | import json
|
---|
| 3 | import time
|
---|
| 4 | import unittest
|
---|
| 5 | from unittest.mock import Mock
|
---|
| 6 |
|
---|
| 7 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 8 | from tudelft.utilities.repository.NoResourcesNowException import NoResourcesNowException
|
---|
| 9 | from tudelft_utilities_logging.Reporter import Reporter
|
---|
| 10 |
|
---|
| 11 | from geniusweb.actions.Offer import Offer
|
---|
| 12 | from geniusweb.actions.PartyId import PartyId
|
---|
| 13 | from geniusweb.inform.Inform import Inform
|
---|
| 14 | from geniusweb.protocol.partyconnection.ProtocolToPartyConn import ProtocolToPartyConn
|
---|
| 15 | from geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory import ProtocolToPartyConnFactory
|
---|
| 16 | from geniusweb.protocol.session.mopac.MOPAC import MOPAC
|
---|
| 17 | from geniusweb.protocol.session.mopac.MOPACSettings import MOPACSettings
|
---|
| 18 | from geniusweb.protocol.session.mopac.MOPACState import MOPACState
|
---|
| 19 | from geniusweb.protocol.session.mopac.PartyStates import PartyStates
|
---|
| 20 | from geniusweb.protocol.session.mopac.phase.OfferPhase import OfferPhase
|
---|
| 21 | from geniusweb.protocol.session.mopac.phase.VotingPhase import VotingPhase
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | class MOPACTest(unittest.TestCase):
|
---|
| 25 |
|
---|
| 26 | jackson = ObjectMapper()
|
---|
| 27 |
|
---|
| 28 | PARTY1ID = PartyId("party1")
|
---|
| 29 | PARTY2ID = PartyId("party2")
|
---|
| 30 | PARTY3ID = PartyId("party3")
|
---|
| 31 |
|
---|
| 32 | # don't try to mock settings, all parts are needed and mocking
|
---|
| 33 | # makes the test unreadable.
|
---|
| 34 | setstr = "{\"MOPACSettings\":{\"participants\":[" +\
|
---|
| 35 | "{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"}]}}," +\
|
---|
| 36 | "{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}}," +\
|
---|
| 37 | "{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party3\",\"parameters\":{}},\"profile\":\"http://profile3\"}]}}]," +\
|
---|
| 38 | "\"deadline\":{\"DeadlineTime\":{\"durationms\":100}},\"votingevaluator\":{\"LargestAgreement\":{}}}}"
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | logger = Mock(Reporter)
|
---|
| 42 |
|
---|
| 43 | factory = Mock( ProtocolToPartyConnFactory)
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | def setUp(self):
|
---|
| 47 | self.conn1 = Mock(ProtocolToPartyConn)
|
---|
| 48 | self.conn2 = Mock(ProtocolToPartyConn)
|
---|
| 49 | self.conn3 = Mock(ProtocolToPartyConn)
|
---|
| 50 |
|
---|
| 51 | self.settings = self.jackson.parse(json.loads(self.setstr), MOPACSettings)
|
---|
| 52 | self.mopac = self.settings.getProtocol(self.logger)
|
---|
| 53 |
|
---|
| 54 | self.connections = [self.conn1, self.conn2, self.conn3]
|
---|
| 55 | self.factory.connectAll=Mock(return_value=self.connections)
|
---|
| 56 |
|
---|
| 57 | # hack the state.with(connection) stuff simplistically
|
---|
| 58 |
|
---|
| 59 | self.conn1.getParty=Mock(return_value=self.PARTY1ID)
|
---|
| 60 | self.conn2.getParty=Mock(return_value=self.PARTY2ID)
|
---|
| 61 | self.conn3.getParty=Mock(return_value=self.PARTY3ID)
|
---|
| 62 |
|
---|
| 63 | def testSmoke(self):
|
---|
| 64 | pass
|
---|
| 65 |
|
---|
| 66 | def testGetDescr(self):
|
---|
| 67 | self.assertNotEqual(None, self.mopac.getDescription())
|
---|
| 68 |
|
---|
| 69 | def testInitialState(self):
|
---|
| 70 | state = self.mopac.getState()
|
---|
| 71 | self.assertEqual(self.settings, state.getSettings())
|
---|
| 72 | self.assertEqual(0, len(state.getActions()))
|
---|
| 73 |
|
---|
| 74 | def testProtocol(self):
|
---|
| 75 | self.assertEquals("MOPAC", str(self.mopac.getRef().getURI().getPath()))
|
---|
| 76 |
|
---|
| 77 | def testWorkingCheckDeadline(self):
|
---|
| 78 | self.mopac.start(self.factory)
|
---|
| 79 |
|
---|
| 80 | self.assertFalse(self.mopac.getState().isFinal(time.time()*1000))
|
---|
| 81 | # we have deadline in 100ms so check
|
---|
| 82 | time.sleep(200/1000)
|
---|
| 83 | self.assertTrue(self.mopac.getState().isFinal(time.time()*1000))
|
---|
| 84 |
|
---|
| 85 | def testSubscribedToConnections(self):
|
---|
| 86 | self.mopac.start(self.factory)
|
---|
| 87 |
|
---|
| 88 | self.assertEquals(1, self.conn1.addListener.call_count)
|
---|
| 89 | self.assertEquals(1, self.conn2.addListener.call_count)
|
---|
| 90 | self.assertEquals(1, self.conn3.addListener.call_count)
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | def testActionIsBroadcast(self):
|
---|
| 94 | self.mopac.start(self.factory)
|
---|
| 95 | offer = Mock(Offer)
|
---|
| 96 | self.mopac._actionRequest(self.conn1, offer, 1)
|
---|
| 97 |
|
---|
| 98 | # 1 time for YourTurn, one time for ActionDone.
|
---|
| 99 | # but filtering ActionDone here (instead of Action) does not work,
|
---|
| 100 | # maybe bug in Mockito?
|
---|
| 101 |
|
---|
| 102 | self.assertEqual( 2, len([call for call in self.conn1.send.call_args_list
|
---|
| 103 | if isinstance(call[0][0], Inform)]))
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | #@Test(expected = RuntimeException.class)
|
---|
| 107 | def testPartyFailsConnect(self):
|
---|
| 108 | '''
|
---|
| 109 | Fatal error occurs, run should fail
|
---|
| 110 | '''
|
---|
| 111 | factory1 = Mock(ProtocolToPartyConnFactory)
|
---|
| 112 | # irrecoverable fail test
|
---|
| 113 | def doExc(conn):
|
---|
| 114 | raise ConnectionError("Failed to connect to parties")
|
---|
| 115 |
|
---|
| 116 | factory1.connectAll.side_effect=doExc
|
---|
| 117 |
|
---|
| 118 | self.assertRaises(ConnectionError, lambda:self.mopac.start(factory1))
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | callnr=0
|
---|
| 122 |
|
---|
| 123 | def testPartyFailsConnect2ndtimeOK(self):
|
---|
| 124 | '''
|
---|
| 125 | Nonfatal error occurs, protocol should retry till success
|
---|
| 126 | '''
|
---|
| 127 | now = time.time()*1000
|
---|
| 128 | factory1 = Mock(ProtocolToPartyConnFactory)
|
---|
| 129 | # recoverable fail, and then success.
|
---|
| 130 |
|
---|
| 131 | this=self
|
---|
| 132 | def doExcThenOk(con):
|
---|
| 133 | if this.callnr==0:
|
---|
| 134 | this.callnr=1
|
---|
| 135 | raise NoResourcesNowException("try again later",
|
---|
| 136 | datetime.fromtimestamp((now + 100)/1000.))
|
---|
| 137 | return this.connections
|
---|
| 138 |
|
---|
| 139 | factory1.connectAll.side_effect=doExcThenOk
|
---|
| 140 |
|
---|
| 141 | self.mopac.start(factory1)
|
---|
| 142 |
|
---|
| 143 | def testProceedsNextPhase(self):
|
---|
| 144 | '''
|
---|
| 145 | Test that in Offer phase, and party does action, the next phase is
|
---|
| 146 | entered.
|
---|
| 147 | '''
|
---|
| 148 | # the state before the last party makes the offer
|
---|
| 149 | offerstate = Mock(MOPACState)
|
---|
| 150 | offerstate.getSettings=Mock(return_value=self.settings)
|
---|
| 151 | offerphase = Mock(OfferPhase)
|
---|
| 152 | offerphase.isFinal=Mock(return_value=False)
|
---|
| 153 | offerstate.getPhase=Mock(return_value=offerphase)
|
---|
| 154 | offerstate.__repr__=Mock(return_value="offerstate")
|
---|
| 155 |
|
---|
| 156 | # the phase where all parties placed an offer
|
---|
| 157 | finalofferphase = Mock(OfferPhase)
|
---|
| 158 | finalofferphase.isFinal=Mock(return_value=True)
|
---|
| 159 | finalofferstate = Mock(MOPACState)
|
---|
| 160 | finalofferstate.getPhase=Mock(return_value=finalofferphase)
|
---|
| 161 | finalofferstate.finishPhase=Mock(return_value=finalofferstate)
|
---|
| 162 | finalofferstate.__repr__=Mock(return_value="finalofferstate")
|
---|
| 163 |
|
---|
| 164 | #ADD
|
---|
| 165 | finalofferstate.isFinal = Mock(return_value=False)
|
---|
| 166 |
|
---|
| 167 | offerstate.WithAction=Mock(return_value=finalofferstate)
|
---|
| 168 |
|
---|
| 169 | # the phase where all parties can start voting.
|
---|
| 170 | # this is the state to be reached.
|
---|
| 171 | now = time.time()*1000
|
---|
| 172 | votingstate = Mock(MOPACState)
|
---|
| 173 | votingphase = Mock(VotingPhase)
|
---|
| 174 | votingpartystates = Mock(PartyStates)
|
---|
| 175 | votingpartystates.getNotYetActed=Mock(return_value=set())
|
---|
| 176 | votingphase.getDeadline=Mock(return_value=now + 100)
|
---|
| 177 | votingphase.getPartyStates=Mock(return_value=votingpartystates)
|
---|
| 178 | votingstate.getPhase=Mock(return_value=votingphase)
|
---|
| 179 | votingstate.__repr__=Mock(return_value="voringstate")
|
---|
| 180 |
|
---|
| 181 | finalofferstate.nextPhase=Mock(return_value=votingstate)
|
---|
| 182 |
|
---|
| 183 | mopac = MOPAC(offerstate, self.logger)
|
---|
| 184 | action = Mock(Offer)
|
---|
| 185 | mopac._actionRequest(self.conn3, action, now)
|
---|
| 186 |
|
---|
| 187 | # check that the action result in reaching the voting phase
|
---|
| 188 | self.assertEqual(votingstate, mopac.getState())
|
---|
| 189 |
|
---|