diff options
author | tomc797 <34632752+tomc797@users.noreply.github.com> | 2018-10-15 20:47:43 -0700 |
---|---|---|
committer | Asif Saif Uddin <auvipy@gmail.com> | 2018-10-16 09:47:43 +0600 |
commit | 77be4f202ad03a0b9292d8743633e3390f87f9a7 (patch) | |
tree | f19755ac53be1820451dca1550258aa8e1bfb70e | |
parent | f812b5695950c713f3f455b8df92336124aa8683 (diff) | |
download | py-amqp-77be4f202ad03a0b9292d8743633e3390f87f9a7.tar.gz |
Improved tests of Connection.connect, Connection.close behaviors (#212)
* If there is an exception raised on Connection.connect or Connection.close,
ensure that the underlying transport socket is closed
* Added more informative message for exception
* Fix code style to satisfy travis checks
* Fix code style to satisfy travis checks
* Provided more informative exception message when remote unexpectedly hangs up
* Test the setting of SO_RCVTIMEO, SO_SNDTIMEO
* callback is soley invoked when connected
From the unit tests, I gleened that the callback should only be invoked
when Connection.connected is true
* Code style fixes
* Code style fixes
* Code style fixes
* Code fix, wrap line continuation differently
* Added three tests to improve coverage
Test to ensure connect is idempotent. Tests to confirm EOF behavior.
* Rigorous checks of context manager behavior
Checks of __enter__, __exit__ behaviors when socket.error exceptions
are encountered.
* Rigorous checks of context manager behavior
Checks of __enter__, __exit__ behaviors when socket.error exceptions
are encountered.
* Fix AssertionError with uncalled transport.connect
-rw-r--r-- | t/unit/test_connection.py | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/t/unit/test_connection.py b/t/unit/test_connection.py index b34ba7b..86656b0 100644 --- a/t/unit/test_connection.py +++ b/t/unit/test_connection.py @@ -100,6 +100,33 @@ class test_Connection: self.conn.connect.assert_called_with() self.conn.close.assert_called_with() + def test__enter__socket_error(self): + # test when entering + self.conn = Connection() + self.conn.close = Mock(name='close') + reached = False + with patch('socket.socket', side_effect=socket.error): + with pytest.raises(socket.error): + with self.conn: + reached = True + assert not reached and not self.conn.close.called + assert self.conn._transport is None and not self.conn.connected + + def test__exit__socket_error(self): + # test when exiting + connection = self.conn + transport = connection._transport + transport.connected = True + connection.send_method = Mock(name='send_method', + side_effect=socket.error) + reached = False + with pytest.raises(socket.error): + with connection: + reached = True + assert reached + assert connection.send_method.called and transport.close.called + assert self.conn._transport is None and not self.conn.connected + def test_then(self): self.conn.on_open = Mock(name='on_open') on_success = Mock(name='on_success') @@ -128,12 +155,18 @@ class test_Connection: callback.assert_called_with() def test_connect__socket_error(self): + # check Transport.Connect error + # socket.error derives from IOError + # ssl.SSLError derives from socket.error self.conn = Connection() - self.conn.collect = Mock(name='collect') - with patch('socket.socket', side_effect=socket.error): - with pytest.raises(socket.error): - self.conn.connect() - self.conn.collect.assert_called_with() + self.conn.Transport = Mock(name='Transport') + transport = self.conn.Transport.return_value + transport.connect.side_effect = IOError + assert self.conn._transport is None and not self.conn.connected + with pytest.raises(IOError): + self.conn.connect() + transport.connect.assert_called + assert self.conn._transport is None and not self.conn.connected def test_on_start(self): self.conn._on_start(3, 4, {'foo': 'bar'}, b'x y z AMQPLAIN PLAIN', @@ -400,11 +433,10 @@ class test_Connection: def test_close__socket_error(self): self.conn.send_method = Mock(name='send_method', side_effect=socket.error) - self.conn.collect = Mock(name='collect') with pytest.raises(socket.error): self.conn.close() self.conn.send_method.assert_called() - self.conn.collect.assert_called_with() + assert self.conn._transport is None and not self.conn.connected def test_on_close(self): self.conn._x_close_ok = Mock(name='_x_close_ok') |