def test_transmit_closedConnection(self):
        "Test transmit (closed connection)"
        network = DummyNetwork()
        network.interfaceExistsReturnValue = False

        self.connection.messages = \
        [
        persistentconnection.PersistentConnectionMessage(
         index=1,
         message=messages.OutboundMessage(localID="localID")
         ),
        persistentconnection.PersistentConnectionMessage(
         index=2,
         message=messages.OutboundMessage(localID="localID")
         ),
        persistentconnection.PersistentConnectionMessage(
         index=3,
         message=messages.OutboundMessage(localID="localID")
         )
        ]

        self.connection.closing = False
        self.connection.notYetTransmitted = 1

        self.assertTrue(self.connection.transmit(network))

        self.assertEqual(network.trace,
                         [('interfaceExists', ('localID', ), {})])
        self.assertEqual(len(self.connection.messages), 3)
        self.assertEqual(self.connection.notYetTransmitted, 3)

        network.trace = []

        self.assertFalse(self.connection.transmit(network))

        self.assertEqual(network.trace,
                         [('interfaceExists', ('localID', ), {})])
        self.assertEqual(len(self.connection.messages), 3)
        self.assertEqual(self.connection.notYetTransmitted, 3)

        network.trace = []
        self.connection.closing = True

        self.assertTrue(self.connection.transmit(network))

        self.assertEqual(network.trace,
                         [('interfaceExists', ('localID', ), {})])
        self.assertEqual(len(self.connection.messages), 0)
        self.assertEqual(self.connection.notYetTransmitted, 0)
    def test_addMessage_wrapAround(self):
        "Test addMessage (wrap-around)"

        msgIn = messages.OutboundMessage(localID="localID",
                                         message=messages.Cancel())
        msgInState = msgIn.getState()

        for i in range(65536):
            self.connection.handleMessage(msgIn)

            self.assertEqual(self.connection.lastIndex, i)
            self.assertEqual(self.connection.notYetTransmitted, 1)
            self.assertEqual(len(self.connection.messages), 1)
            msg = self.connection.messages[-1]
            self.assertEqual(msg.index, i)
            self.assertEqual(msg.message.getState(), msgInState)

            self.connection.messages = []
            self.connection.notYetTransmitted = 0

        self.connection.handleMessage(msgIn)

        self.assertEqual(self.connection.lastIndex, 0)
        self.assertEqual(self.connection.notYetTransmitted, 1)
        self.assertEqual(len(self.connection.messages), 1)
        msg = self.connection.messages[-1]
        self.assertEqual(msg.index, 0)
        self.assertEqual(msg.message.getState(), msgInState)
    def test_transmit_openConnection(self):
        "Test transmit (open connection)"
        network = DummyNetwork()
        network.interfaceExistsReturnValue = True

        self.connection.messages = \
        [
        persistentconnection.PersistentConnectionMessage(
         index=1,
         message=messages.OutboundMessage(localID="localID")
         ),
        persistentconnection.PersistentConnectionMessage(
         index=2,
         message=messages.OutboundMessage(localID="localID")
         ),
        persistentconnection.PersistentConnectionMessage(
         index=3,
         message=messages.OutboundMessage(localID="localID")
         )
        ]

        self.connection.notYetTransmitted = 0

        self.assertFalse(self.connection.transmit(network))

        self.assertEqual(network.trace,
                         [('interfaceExists', ('localID', ), {})])
        self.assertEqual(len(self.connection.messages), 3)
        self.assertEqual(self.connection.notYetTransmitted, 0)

        network.trace = []
        self.connection.notYetTransmitted = 2

        self.assertTrue(self.connection.transmit(network))

        self.assertEqual(network.trace,
                         [('interfaceExists', ('localID', ), {}),
                          ('sendOutboundMessage',
                           (2, self.connection.messages[1].message), {}),
                          ('sendOutboundMessage',
                           (3, self.connection.messages[2].message), {})])
        self.assertEqual(len(self.connection.messages), 3)
        self.assertEqual(self.connection.notYetTransmitted, 0)
    def test_addMessage_fullOutbox(self):
        "Test addMessage (full outbox)"

        msgIn = messages.OutboundMessage(localID="localID",
                                         message=messages.Cancel())
        msgInState = msgIn.getState()

        for i in range(32768):
            self.connection.handleMessage(msgIn)

            self.assertEqual(self.connection.lastIndex, i)
            self.assertEqual(self.connection.notYetTransmitted, i + 1)
            self.assertEqual(len(self.connection.messages), i + 1)
            msg = self.connection.messages[-1]
            self.assertEqual(msg.index, i)
            self.assertEqual(msg.message.getState(), msgInState)

        self.assertRaises(Exception, self.connection.handleMessage, msgIn)

        self.assertEqual(self.connection.lastIndex, 32767)
        self.assertEqual(self.connection.notYetTransmitted, 32768)
        self.assertEqual(len(self.connection.messages), 32768)
Example #5
0
	def test_connectionSession(self):
		"Test connection session"

		#Opening the connection:
		c1 = self.network.makeConnection(
			('localhost', 4321), 'localID', messages.Pay(ID='remoteID'))

		self.assertEqual(c1.localID, 'localID')
		self.assertEqual(len(self.network.connections), 1)
		self.assertEqual(self.network.connections[0], c1)
		self.assertEqual(self.network.getInterface('localID'), c1)
		self.assertTrue(self.network.interfaceExists('localID'))

		self.network.processNetworkEvents(timeout=0.01)

		self.assertEqual(len(self.network.connections), 2)
		c2 = self.network.connections[1]
		self.assertEqual(c2.localID, None)

		self.assertEqual(len(self.messages), 0)

		self.network.processNetworkEvents(timeout=0.01)

		self.assertEqual(c2.localID, 'remoteID')
		self.assertTrue(self.network.interfaceExists('remoteID'))
		self.assertEqual(len(self.messages), 1)
		self.assertEqual(self.messages[0].__class__, messages.Pay)

		#Sending another connect message (illegal):
		self.messages = []
		self.network.sendOutboundMessage(0, messages.OutboundMessage(
			localID='localID',
			message=messages.Pay(ID='remoteID2')))

		self.network.processNetworkEvents(timeout=0.01)

		self.assertEqual(c2.localID, 'remoteID') #unchanged
		self.assertEqual(len(self.messages), 0) #nothing received

		#Sending another connect message (illegal):
		self.messages = []
		self.network.sendOutboundMessage(0, messages.OutboundMessage(
			localID='localID',
			message=messages.ConnectLink(ID='remoteID2')))

		self.network.processNetworkEvents(timeout=0.01)

		self.assertEqual(c2.localID, 'remoteID') #unchanged
		self.assertEqual(len(self.messages), 0) #nothing received

		#Sending another message back:
		self.network.sendOutboundMessage(42, messages.OutboundMessage(
			localID='remoteID',
			message=messages.Cancel(ID=None)))

		self.network.processNetworkEvents(timeout=0.01) #sends the confirmation

		self.assertEqual(len(self.messages), 1)
		self.assertEqual(self.messages[0].__class__, messages.Cancel)

		self.messages = []
		self.network.processNetworkEvents(timeout=0.01) #receives the confirmation

		self.assertEqual(len(self.messages), 1)
		self.assertEqual(self.messages[0].__class__, messages.Confirmation)
		self.assertEqual(self.messages[0].localID, 'remoteID')
		self.assertEqual(self.messages[0].index, 42)

		#Sending an invalid message:
		self.messages = []
		c2.send('{"data": "Invalid"}\n')

		self.network.processNetworkEvents(timeout=0.01)

		self.assertEqual(len(self.messages), 0)

		#Try to make an already open connection (should be ignored):
		self.network.makeConnection(
			('localhost', 4321), 'localID', messages.Pay(ID='remoteID'))
		self.assertEqual(len(self.network.connections), 2)

		#Closing the connection:
		self.network.closeInterface('remoteID')

		self.assertEqual(len(self.network.connections), 1)
		self.assertEqual(self.network.connections[0], c1)
		self.assertFalse(self.network.interfaceExists('remoteID'))
		self.assertTrue(self.network.interfaceExists('localID'))

		self.network.processNetworkEvents(timeout=0.01)

		self.assertFalse(self.network.interfaceExists('localID'))
		self.assertEqual(len(self.network.connections), 0)

		#Closing a no-longer-existing connection (should be fine):
		self.network.closeInterface('localID')

		self.assertEqual(len(self.messages), 0)