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):
return self, ('', 0)
DoNothingConnector.getNewConnection = getNewConnection
addr = ("127.0.0.7", 93413)
try:
bc = self._makeListeningConnection(addr=addr)
self.assertEqual(bc.getAddress(), addr)
self._checkRegistered()
......@@ -174,6 +175,8 @@ class ConnectionTests(NeoUnitTestBase):
bc.readable()
self._checkGetNewConnection()
self._checkConnectionAccepted()
finally:
del DoNothingConnector.getNewConnection
def test_02_ListeningConnection2(self):
# test with exception raise when getting new connection
......@@ -181,6 +184,7 @@ class ConnectionTests(NeoUnitTestBase):
raise ConnectorTryAgainException
DoNothingConnector.getNewConnection = getNewConnection
addr = ("127.0.0.7", 93413)
try:
bc = self._makeListeningConnection(addr=addr)
self.assertEqual(bc.getAddress(), addr)
self._checkRegistered()
......@@ -190,6 +194,8 @@ class ConnectionTests(NeoUnitTestBase):
bc.readable()
self._checkGetNewConnection(1)
self._checkConnectionAccepted(0)
finally:
del DoNothingConnector.getNewConnection
def test_03_Connection(self):
bc = self._makeConnection()
......@@ -230,28 +236,35 @@ class ConnectionTests(NeoUnitTestBase):
def receive(self):
return "testdata"
DoNothingConnector.receive = receive
try:
bc = self._makeConnection()
self._checkReadBuf(bc, '')
bc._recv()
self._checkReadBuf(bc, 'testdata')
finally:
del DoNothingConnector.receive
def test_Connection_recv2(self):
# patch receive method to raise try again
def receive(self):
raise ConnectorTryAgainException
DoNothingConnector.receive = receive
try:
bc = self._makeConnection()
self._checkReadBuf(bc, '')
bc._recv()
self._checkReadBuf(bc, '')
self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.receive
def test_Connection_recv3(self):
# patch receive method to raise ConnectorConnectionRefusedException
def receive(self):
raise ConnectorConnectionRefusedException
DoNothingConnector.receive = receive
try:
bc = self._makeConnection()
self._checkReadBuf(bc, '')
# fake client connection instance with connecting attribute
......@@ -260,18 +273,23 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReadBuf(bc, '')
self._checkConnectionFailed(1)
self._checkUnregistered(1)
finally:
del DoNothingConnector.receive
def test_Connection_recv4(self):
# patch receive method to raise any other connector error
def receive(self):
raise ConnectorException
DoNothingConnector.receive = receive
try:
bc = self._makeConnection()
self._checkReadBuf(bc, '')
self.assertRaises(ConnectorException, bc._recv)
self._checkReadBuf(bc, '')
self._checkConnectionClosed(1)
self._checkUnregistered(1)
finally:
del DoNothingConnector.receive
def test_Connection_send1(self):
# no data, nothing done
......@@ -288,6 +306,7 @@ class ConnectionTests(NeoUnitTestBase):
def send(self, data):
return len(data)
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
......@@ -296,12 +315,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, '')
self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send3(self):
# send part of the data
def send(self, data):
return len(data)/2
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
......@@ -310,12 +332,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, 'data')
self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send4(self):
# send multiple packet
def send(self, data):
return len(data)
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"]
......@@ -324,12 +349,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, '')
self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send5(self):
# send part of multiple packet
def send(self, data):
return len(data)/2
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"]
......@@ -338,12 +366,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, 'econdthird')
self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send6(self):
# raise try again
def send(self, data):
raise ConnectorTryAgainException
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"]
......@@ -352,12 +383,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkWriteBuf(bc, 'testdatasecondthird')
self._checkConnectionClosed(0)
self._checkUnregistered(0)
finally:
del DoNothingConnector.send
def test_Connection_send7(self):
# raise other error
def send(self, data):
raise ConnectorException
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata", "second", "third"]
......@@ -368,6 +402,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(1)
self._checkConnectionClosed(1)
self._checkUnregistered(1)
finally:
del DoNothingConnector.send
def test_07_Connection_addPacket(self):
# new packet
......@@ -511,6 +547,7 @@ class ConnectionTests(NeoUnitTestBase):
def send(self, data):
return len(data)/2
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
......@@ -529,12 +566,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0)
self._checkShutdown(0)
self._checkClose(0)
finally:
del DoNothingConnector.send
def test_Connection_writable2(self):
# without pending operation after send
def send(self, data):
return len(data)
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
......@@ -553,12 +593,15 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0)
self._checkShutdown(0)
self._checkClose(0)
finally:
del DoNothingConnector.send
def test_Connection_writable3(self):
# without pending operation after send and aborted set to true
def send(self, data):
return len(data)
DoNothingConnector.send = send
try:
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
......@@ -578,6 +621,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(1)
self._checkShutdown(1)
self._checkClose(1)
finally:
del DoNothingConnector.send
def test_Connection_readable(self):
# With aborted set to false
......@@ -596,6 +641,7 @@ class ConnectionTests(NeoUnitTestBase):
p.setId(1)
return ''.join(p.encode())
DoNothingConnector.receive = receive
try:
bc = self._makeConnection()
bc._queue = Mock()
self._checkReadBuf(bc, '')
......@@ -616,6 +662,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0)
self._checkShutdown(0)
self._checkClose(0)
finally:
del DoNothingConnector.receive
def test_ClientConnection_init1(self):
# create a good client connection
......@@ -686,6 +734,7 @@ class ConnectionTests(NeoUnitTestBase):
makeClientConnection_org = DoNothingConnector.makeClientConnection
DoNothingConnector.send = send
DoNothingConnector.makeClientConnection = makeClientConnection
try:
try:
bc = self._makeClientConnection()
finally:
......@@ -713,6 +762,8 @@ class ConnectionTests(NeoUnitTestBase):
self._checkReaderRemoved(0)
self._checkShutdown(0)
self._checkClose(0)
finally:
del DoNothingConnector.send
def test_ClientConnection_writable2(self):
# with a connecting connection, must not call parent's method
......@@ -720,9 +771,11 @@ class ConnectionTests(NeoUnitTestBase):
def getError(self):
return True
DoNothingConnector.getError = getError
try:
bc = self._makeClientConnection()
finally:
del DoNothingConnector.getError
# check connector created and connection initialize
bc.connecting = True
self._checkWriteBuf(bc, '')
bc.write_buf = ["testdata"]
self.assertTrue(bc.pending())
......@@ -735,9 +788,9 @@ class ConnectionTests(NeoUnitTestBase):
self.assertFalse(bc.pending())
self.assertFalse(bc.aborted)
self._checkWriteBuf(bc, '')
self._checkConnectionClosed(0)
self._checkConnectionClosed(1)
self._checkConnectionCompleted(1)
self._checkConnectionFailed(1)
self._checkConnectionFailed(0)
self._checkUnregistered(1)
self._checkReaderAdded(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