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,6 +165,7 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -165,6 +165,7 @@ 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)
try:
bc = self._makeListeningConnection(addr=addr) bc = self._makeListeningConnection(addr=addr)
self.assertEqual(bc.getAddress(), addr) self.assertEqual(bc.getAddress(), addr)
self._checkRegistered() self._checkRegistered()
...@@ -174,6 +175,8 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -174,6 +175,8 @@ class ConnectionTests(NeoUnitTestBase):
bc.readable() bc.readable()
self._checkGetNewConnection() self._checkGetNewConnection()
self._checkConnectionAccepted() 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,6 +184,7 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -181,6 +184,7 @@ 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)
try:
bc = self._makeListeningConnection(addr=addr) bc = self._makeListeningConnection(addr=addr)
self.assertEqual(bc.getAddress(), addr) self.assertEqual(bc.getAddress(), addr)
self._checkRegistered() self._checkRegistered()
...@@ -190,6 +194,8 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -190,6 +194,8 @@ class ConnectionTests(NeoUnitTestBase):
bc.readable() bc.readable()
self._checkGetNewConnection(1) self._checkGetNewConnection(1)
self._checkConnectionAccepted(0) self._checkConnectionAccepted(0)
finally:
del DoNothingConnector.getNewConnection
def test_03_Connection(self): def test_03_Connection(self):
bc = self._makeConnection() bc = self._makeConnection()
...@@ -230,28 +236,35 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -230,28 +236,35 @@ class ConnectionTests(NeoUnitTestBase):
def receive(self): def receive(self):
return "testdata" return "testdata"
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
bc._recv() bc._recv()
self._checkReadBuf(bc, 'testdata') 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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
bc._recv() bc._recv()
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
self._checkConnectionClosed(0) self._checkConnectionClosed(0)
self._checkUnregistered(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
# fake client connection instance with connecting attribute # fake client connection instance with connecting attribute
...@@ -260,18 +273,23 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -260,18 +273,23 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
self._checkConnectionFailed(1) self._checkConnectionFailed(1)
self._checkUnregistered(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
self.assertRaises(ConnectorException, bc._recv) self.assertRaises(ConnectorException, bc._recv)
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
self._checkConnectionClosed(1) self._checkConnectionClosed(1)
self._checkUnregistered(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,6 +306,7 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -288,6 +306,7 @@ class ConnectionTests(NeoUnitTestBase):
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
...@@ -296,12 +315,15 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -296,12 +315,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
self._checkConnectionClosed(0) self._checkConnectionClosed(0)
self._checkUnregistered(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
...@@ -310,12 +332,15 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -310,12 +332,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, 'data') self._checkWriteBuf(bc, 'data')
self._checkConnectionClosed(0) self._checkConnectionClosed(0)
self._checkUnregistered(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
...@@ -324,12 +349,15 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -324,12 +349,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
self._checkConnectionClosed(0) self._checkConnectionClosed(0)
self._checkUnregistered(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
...@@ -338,12 +366,15 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -338,12 +366,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, 'econdthird') self._checkWriteBuf(bc, 'econdthird')
self._checkConnectionClosed(0) self._checkConnectionClosed(0)
self._checkUnregistered(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
...@@ -352,12 +383,15 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -352,12 +383,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, 'testdatasecondthird') self._checkWriteBuf(bc, 'testdatasecondthird')
self._checkConnectionClosed(0) self._checkConnectionClosed(0)
self._checkUnregistered(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
...@@ -368,6 +402,8 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -368,6 +402,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(1) self._checkReaderRemoved(1)
self._checkConnectionClosed(1) self._checkConnectionClosed(1)
self._checkUnregistered(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,6 +547,7 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -511,6 +547,7 @@ 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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
...@@ -529,12 +566,15 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -529,12 +566,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0) self._checkReaderRemoved(0)
self._checkShutdown(0) self._checkShutdown(0)
self._checkClose(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
...@@ -553,12 +593,15 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -553,12 +593,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0) self._checkReaderRemoved(0)
self._checkShutdown(0) self._checkShutdown(0)
self._checkClose(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
try:
bc = self._makeConnection() bc = self._makeConnection()
self._checkWriteBuf(bc, '') self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
...@@ -578,6 +621,8 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -578,6 +621,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(1) self._checkReaderRemoved(1)
self._checkShutdown(1) self._checkShutdown(1)
self._checkClose(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,6 +641,7 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -596,6 +641,7 @@ class ConnectionTests(NeoUnitTestBase):
p.setId(1) p.setId(1)
return ''.join(p.encode()) return ''.join(p.encode())
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
try:
bc = self._makeConnection() bc = self._makeConnection()
bc._queue = Mock() bc._queue = Mock()
self._checkReadBuf(bc, '') self._checkReadBuf(bc, '')
...@@ -616,6 +662,8 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -616,6 +662,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0) self._checkReaderRemoved(0)
self._checkShutdown(0) self._checkShutdown(0)
self._checkClose(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
...@@ -686,6 +734,7 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -686,6 +734,7 @@ class ConnectionTests(NeoUnitTestBase):
makeClientConnection_org = DoNothingConnector.makeClientConnection makeClientConnection_org = DoNothingConnector.makeClientConnection
DoNothingConnector.send = send DoNothingConnector.send = send
DoNothingConnector.makeClientConnection = makeClientConnection DoNothingConnector.makeClientConnection = makeClientConnection
try:
try: try:
bc = self._makeClientConnection() bc = self._makeClientConnection()
finally: finally:
...@@ -713,6 +762,8 @@ class ConnectionTests(NeoUnitTestBase): ...@@ -713,6 +762,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0) self._checkReaderRemoved(0)
self._checkShutdown(0) self._checkShutdown(0)
self._checkClose(0) self._checkClose(0)
finally:
del DoNothingConnector.send
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
try:
bc = self._makeClientConnection() 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