Commit bcac3c8a authored by Vincent Pelletier's avatar Vincent Pelletier

Properly undo monkeypatching so tests become independent.

Also, make test_ClientConnection_writable2 test what it says it tests.
parent 94f7a7ff
...@@ -165,15 +165,18 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -165,15 +165,18 @@ class ConnectionTests(NeoUnitTestBase):
return self, ('', 0) return self, ('', 0)
DoNothingConnector.getNewConnection = getNewConnection DoNothingConnector.getNewConnection = getNewConnection
addr = ("127.0.0.7", 93413) addr = ("127.0.0.7", 93413)
bc = self._makeListeningConnection(addr=addr) try:
self.assertEqual(bc.getAddress(), addr) bc = self._makeListeningConnection(addr=addr)
self._checkRegistered() self.assertEqual(bc.getAddress(), addr)
self._checkReaderAdded() self._checkRegistered()
self._checkMakeListeningConnection() self._checkReaderAdded()
# test readable self._checkMakeListeningConnection()
bc.readable() # test readable
self._checkGetNewConnection() bc.readable()
self._checkConnectionAccepted() self._checkGetNewConnection()
self._checkConnectionAccepted()
finally:
del DoNothingConnector.getNewConnection
def test_02_ListeningConnection2(self): def test_02_ListeningConnection2(self):
# test with exception raise when getting new connection # test with exception raise when getting new connection
...@@ -181,15 +184,18 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -181,15 +184,18 @@ class ConnectionTests(NeoUnitTestBase):
raise ConnectorTryAgainException raise ConnectorTryAgainException
DoNothingConnector.getNewConnection = getNewConnection DoNothingConnector.getNewConnection = getNewConnection
addr = ("127.0.0.7", 93413) addr = ("127.0.0.7", 93413)
bc = self._makeListeningConnection(addr=addr) try:
self.assertEqual(bc.getAddress(), addr) bc = self._makeListeningConnection(addr=addr)
self._checkRegistered() self.assertEqual(bc.getAddress(), addr)
self._checkReaderAdded() self._checkRegistered()
self._checkMakeListeningConnection() self._checkReaderAdded()
# test readable self._checkMakeListeningConnection()
bc.readable() # test readable
self._checkGetNewConnection(1) bc.readable()
self._checkConnectionAccepted(0) self._checkGetNewConnection(1)
self._checkConnectionAccepted(0)
finally:
del DoNothingConnector.getNewConnection
def test_03_Connection(self): def test_03_Connection(self):
bc = self._makeConnection() bc = self._makeConnection()
...@@ -230,48 +236,60 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -230,48 +236,60 @@ class ConnectionTests(NeoUnitTestBase):
def receive(self): def receive(self):
return "testdata" return "testdata"
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
bc = self._makeConnection() try:
self._checkReadBuf(bc, '') bc = self._makeConnection()
bc._recv() self._checkReadBuf(bc, '')
self._checkReadBuf(bc, 'testdata') bc._recv()
self._checkReadBuf(bc, 'testdata')
finally:
del DoNothingConnector.receive
def test_Connection_recv2(self): def test_Connection_recv2(self):
# patch receive method to raise try again # patch receive method to raise try again
def receive(self): def receive(self):
raise ConnectorTryAgainException raise ConnectorTryAgainException
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
bc = self._makeConnection() try:
self._checkReadBuf(bc, '') bc = self._makeConnection()
bc._recv() self._checkReadBuf(bc, '')
self._checkReadBuf(bc, '') bc._recv()
self._checkConnectionClosed(0) self._checkReadBuf(bc, '')
self._checkUnregistered(0) self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.receive
def test_Connection_recv3(self): def test_Connection_recv3(self):
# patch receive method to raise ConnectorConnectionRefusedException # patch receive method to raise ConnectorConnectionRefusedException
def receive(self): def receive(self):
raise ConnectorConnectionRefusedException raise ConnectorConnectionRefusedException
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
bc = self._makeConnection() try:
self._checkReadBuf(bc, '') bc = self._makeConnection()
# fake client connection instance with connecting attribute self._checkReadBuf(bc, '')
bc.connecting = True # fake client connection instance with connecting attribute
bc._recv() bc.connecting = True
self._checkReadBuf(bc, '') bc._recv()
self._checkConnectionFailed(1) self._checkReadBuf(bc, '')
self._checkUnregistered(1) self._checkConnectionFailed(1)
self._checkUnregistered(1)
finally:
del DoNothingConnector.receive
def test_Connection_recv4(self): def test_Connection_recv4(self):
# patch receive method to raise any other connector error # patch receive method to raise any other connector error
def receive(self): def receive(self):
raise ConnectorException raise ConnectorException
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
bc = self._makeConnection() try:
self._checkReadBuf(bc, '') bc = self._makeConnection()
self.assertRaises(ConnectorException, bc._recv) self._checkReadBuf(bc, '')
self._checkReadBuf(bc, '') self.assertRaises(ConnectorException, bc._recv)
self._checkConnectionClosed(1) self._checkReadBuf(bc, '')
self._checkUnregistered(1) self._checkConnectionClosed(1)
self._checkUnregistered(1)
finally:
del DoNothingConnector.receive
def test_Connection_send1(self): def test_Connection_send1(self):
# no data, nothing done # no data, nothing done
...@@ -288,86 +306,104 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -288,86 +306,104 @@ class ConnectionTests(NeoUnitTestBase):
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata"] self._checkWriteBuf(bc, '')
bc._send() bc.write_buf = ["testdata"]
self._checkSend(1, "testdata") bc._send()
self._checkWriteBuf(bc, '') self._checkSend(1, "testdata")
self._checkConnectionClosed(0) self._checkWriteBuf(bc, '')
self._checkUnregistered(0) self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send3(self): def test_Connection_send3(self):
# send part of the data # send part of the data
def send(self, data): def send(self, data):
return len(data)/2 return len(data)/2
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata"] self._checkWriteBuf(bc, '')
bc._send() bc.write_buf = ["testdata"]
self._checkSend(1, "testdata") bc._send()
self._checkWriteBuf(bc, 'data') self._checkSend(1, "testdata")
self._checkConnectionClosed(0) self._checkWriteBuf(bc, 'data')
self._checkUnregistered(0) self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send4(self): def test_Connection_send4(self):
# send multiple packet # send multiple packet
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata", "second", "third"] self._checkWriteBuf(bc, '')
bc._send() bc.write_buf = ["testdata", "second", "third"]
self._checkSend(1, "testdatasecondthird") bc._send()
self._checkWriteBuf(bc, '') self._checkSend(1, "testdatasecondthird")
self._checkConnectionClosed(0) self._checkWriteBuf(bc, '')
self._checkUnregistered(0) self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send5(self): def test_Connection_send5(self):
# send part of multiple packet # send part of multiple packet
def send(self, data): def send(self, data):
return len(data)/2 return len(data)/2
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata", "second", "third"] self._checkWriteBuf(bc, '')
bc._send() bc.write_buf = ["testdata", "second", "third"]
self._checkSend(1, "testdatasecondthird") bc._send()
self._checkWriteBuf(bc, 'econdthird') self._checkSend(1, "testdatasecondthird")
self._checkConnectionClosed(0) self._checkWriteBuf(bc, 'econdthird')
self._checkUnregistered(0) self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send6(self): def test_Connection_send6(self):
# raise try again # raise try again
def send(self, data): def send(self, data):
raise ConnectorTryAgainException raise ConnectorTryAgainException
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata", "second", "third"] self._checkWriteBuf(bc, '')
bc._send() bc.write_buf = ["testdata", "second", "third"]
self._checkSend(1, "testdatasecondthird") bc._send()
self._checkWriteBuf(bc, 'testdatasecondthird') self._checkSend(1, "testdatasecondthird")
self._checkConnectionClosed(0) self._checkWriteBuf(bc, 'testdatasecondthird')
self._checkUnregistered(0) self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send7(self): def test_Connection_send7(self):
# raise other error # raise other error
def send(self, data): def send(self, data):
raise ConnectorException raise ConnectorException
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata", "second", "third"] self._checkWriteBuf(bc, '')
self.assertRaises(ConnectorException, bc._send) bc.write_buf = ["testdata", "second", "third"]
self._checkSend(1, "testdatasecondthird") self.assertRaises(ConnectorException, bc._send)
# connection closed -> buffers flushed self._checkSend(1, "testdatasecondthird")
self._checkWriteBuf(bc, '') # connection closed -> buffers flushed
self._checkReaderRemoved(1) self._checkWriteBuf(bc, '')
self._checkConnectionClosed(1) self._checkReaderRemoved(1)
self._checkUnregistered(1) self._checkConnectionClosed(1)
self._checkUnregistered(1)
finally:
del DoNothingConnector.send
def test_07_Connection_addPacket(self): def test_07_Connection_addPacket(self):
# new packet # new packet
...@@ -511,73 +547,82 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -511,73 +547,82 @@ class ConnectionTests(NeoUnitTestBase):
def send(self, data): def send(self, data):
return len(data)/2 return len(data)/2
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata"] self._checkWriteBuf(bc, '')
self.assertTrue(bc.pending()) bc.write_buf = ["testdata"]
self.assertFalse(bc.aborted) self.assertTrue(bc.pending())
bc.writable() self.assertFalse(bc.aborted)
# test send was called bc.writable()
self._checkSend(1, "testdata") # test send was called
self._checkWriteBuf(bc, "data") self._checkSend(1, "testdata")
self._checkConnectionClosed(0) self._checkWriteBuf(bc, "data")
self._checkClose(0) self._checkConnectionClosed(0)
self._checkUnregistered(0) self._checkClose(0)
# pending, so nothing called self._checkUnregistered(0)
self.assertTrue(bc.pending()) # pending, so nothing called
self._checkWriterRemoved(0) self.assertTrue(bc.pending())
self._checkReaderRemoved(0) self._checkWriterRemoved(0)
self._checkShutdown(0) self._checkReaderRemoved(0)
self._checkClose(0) self._checkShutdown(0)
self._checkClose(0)
finally:
del DoNothingConnector.send
def test_Connection_writable2(self): def test_Connection_writable2(self):
# without pending operation after send # without pending operation after send
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata"] self._checkWriteBuf(bc, '')
self.assertTrue(bc.pending()) bc.write_buf = ["testdata"]
self.assertFalse(bc.aborted) self.assertTrue(bc.pending())
bc.writable() self.assertFalse(bc.aborted)
# test send was called bc.writable()
self._checkSend(1, "testdata") # test send was called
self._checkWriteBuf(bc, '') self._checkSend(1, "testdata")
self._checkConnectionClosed(0) self._checkWriteBuf(bc, '')
self._checkClose(0) self._checkConnectionClosed(0)
self._checkUnregistered(0) self._checkClose(0)
# nothing else pending, so writer has been removed self._checkUnregistered(0)
self.assertFalse(bc.pending()) # nothing else pending, so writer has been removed
self._checkWriterRemoved(1) self.assertFalse(bc.pending())
self._checkReaderRemoved(0) self._checkWriterRemoved(1)
self._checkShutdown(0) self._checkReaderRemoved(0)
self._checkClose(0) self._checkShutdown(0)
self._checkClose(0)
finally:
del DoNothingConnector.send
def test_Connection_writable3(self): def test_Connection_writable3(self):
# without pending operation after send and aborted set to true # without pending operation after send and aborted set to true
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
bc = self._makeConnection() try:
self._checkWriteBuf(bc, '') bc = self._makeConnection()
bc.write_buf = ["testdata"] self._checkWriteBuf(bc, '')
self.assertTrue(bc.pending()) bc.write_buf = ["testdata"]
bc.abort() self.assertTrue(bc.pending())
self.assertTrue(bc.aborted) bc.abort()
bc.writable() self.assertTrue(bc.aborted)
# test send was called bc.writable()
self._checkSend(1, "testdata") # test send was called
self._checkWriteBuf(bc, '') self._checkSend(1, "testdata")
self._checkConnectionClosed(1) self._checkWriteBuf(bc, '')
self._checkClose(1) self._checkConnectionClosed(1)
self._checkUnregistered(1) self._checkClose(1)
# nothing else pending, so writer has been removed self._checkUnregistered(1)
self.assertFalse(bc.pending()) # nothing else pending, so writer has been removed
self._checkWriterRemoved(1) self.assertFalse(bc.pending())
self._checkReaderRemoved(1) self._checkWriterRemoved(1)
self._checkShutdown(1) self._checkReaderRemoved(1)
self._checkClose(1) self._checkShutdown(1)
self._checkClose(1)
finally:
del DoNothingConnector.send
def test_Connection_readable(self): def test_Connection_readable(self):
# With aborted set to false # With aborted set to false
...@@ -596,26 +641,29 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -596,26 +641,29 @@ class ConnectionTests(NeoUnitTestBase):
p.setId(1) p.setId(1)
return ''.join(p.encode()) return ''.join(p.encode())
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
bc = self._makeConnection() try:
bc._queue = Mock() bc = self._makeConnection()
self._checkReadBuf(bc, '') bc._queue = Mock()
self.assertFalse(bc.aborted) self._checkReadBuf(bc, '')
bc.readable() self.assertFalse(bc.aborted)
# check packet decoded bc.readable()
self._checkReadBuf(bc, '') # check packet decoded
self.assertEqual(len(bc._queue.mockGetNamedCalls("append")), 1) self._checkReadBuf(bc, '')
call = bc._queue.mockGetNamedCalls("append")[0] self.assertEqual(len(bc._queue.mockGetNamedCalls("append")), 1)
data = call.getParam(0) call = bc._queue.mockGetNamedCalls("append")[0]
self.assertEqual(type(data), Packets.AnswerPrimary) data = call.getParam(0)
self.assertEqual(data.getId(), 1) self.assertEqual(type(data), Packets.AnswerPrimary)
self._checkReadBuf(bc, '') self.assertEqual(data.getId(), 1)
# check not aborted self._checkReadBuf(bc, '')
self.assertFalse(bc.aborted) # check not aborted
self._checkUnregistered(0) self.assertFalse(bc.aborted)
self._checkWriterRemoved(0) self._checkUnregistered(0)
self._checkReaderRemoved(0) self._checkWriterRemoved(0)
self._checkShutdown(0) self._checkReaderRemoved(0)
self._checkClose(0) self._checkShutdown(0)
self._checkClose(0)
finally:
del DoNothingConnector.receive
def test_ClientConnection_init1(self): def test_ClientConnection_init1(self):
# create a good client connection # create a good client connection
...@@ -687,32 +735,35 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -687,32 +735,35 @@ class ConnectionTests(NeoUnitTestBase):
DoNothingConnector.send = send DoNothingConnector.send = send
DoNothingConnector.makeClientConnection = makeClientConnection DoNothingConnector.makeClientConnection = makeClientConnection
try: try:
bc = self._makeClientConnection() try:
bc = self._makeClientConnection()
finally:
DoNothingConnector.makeClientConnection = makeClientConnection_org
# check connector created and connection initialize
self.assertFalse(bc.connecting)
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
self.assertTrue(bc.pending())
self.assertFalse(bc.aborted)
# call
self._checkConnectionCompleted(1)
self._checkReaderAdded(1)
bc.writable()
self.assertFalse(bc.pending())
self.assertFalse(bc.aborted)
self.assertFalse(bc.connecting)
self._checkSend(1, "testdata")
self._checkConnectionClosed(0)
self._checkConnectionCompleted(1)
self._checkConnectionFailed(0)
self._checkUnregistered(0)
self._checkReaderAdded(1)
self._checkWriterRemoved(1)
self._checkReaderRemoved(0)
self._checkShutdown(0)
self._checkClose(0)
finally: finally:
DoNothingConnector.makeClientConnection = makeClientConnection_org del DoNothingConnector.send
# check connector created and connection initialize
self.assertFalse(bc.connecting)
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
self.assertTrue(bc.pending())
self.assertFalse(bc.aborted)
# call
self._checkConnectionCompleted(1)
self._checkReaderAdded(1)
bc.writable()
self.assertFalse(bc.pending())
self.assertFalse(bc.aborted)
self.assertFalse(bc.connecting)
self._checkSend(1, "testdata")
self._checkConnectionClosed(0)
self._checkConnectionCompleted(1)
self._checkConnectionFailed(0)
self._checkUnregistered(0)
self._checkReaderAdded(1)
self._checkWriterRemoved(1)
self._checkReaderRemoved(0)
self._checkShutdown(0)
self._checkClose(0)
def test_ClientConnection_writable2(self): def test_ClientConnection_writable2(self):
# with a connecting connection, must not call parent's method # with a connecting connection, must not call parent's method
...@@ -720,9 +771,11 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -720,9 +771,11 @@ class ConnectionTests(NeoUnitTestBase):
def getError(self): def getError(self):
return True return True
DoNothingConnector.getError = getError DoNothingConnector.getError = getError
bc = self._makeClientConnection() try:
bc = self._makeClientConnection()
finally:
del DoNothingConnector.getError
# check connector created and connection initialize # check connector created and connection initialize
bc.connecting = True
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
...@@ -735,9 +788,9 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -735,9 +788,9 @@ class ConnectionTests(NeoUnitTestBase):
self.assertFalse(bc.pending()) self.assertFalse(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
self._checkConnectionClosed(0) self._checkConnectionClosed(1)
self._checkConnectionCompleted(1) self._checkConnectionCompleted(1)
self._checkConnectionFailed(1) self._checkConnectionFailed(0)
self._checkUnregistered(1) self._checkUnregistered(1)
self._checkReaderAdded(1) self._checkReaderAdded(1)
self._checkWriterRemoved(1) self._checkWriterRemoved(1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment