def test_disconnect_notconnected(self): """ Attempting to disconnect when not connected should raise exception. """ conn = Connection('1.2.3.4', 61613) self.assertRaises(NotConnectedError, conn.disconnect) conn.connect() conn.disconnect() self.assertRaises(NotConnectedError, conn.disconnect)
def test_connect(self): """ Test basic connection functionality. """ conn = Connection('1.2.3.4', 61613) conn.connect() print self.mocksocket.method_calls self.assertTrue(self.mocksocket.connect.called) self.assertEquals((('1.2.3.4', 61613),), self.mocksocket.connect.call_args[0]) self.mocksocket.reset_mock() conn.connect() self.assertFalse(self.mocksocket.connect.called)
def test_connected(self): """ Test that 'connected' property reflects connection status. """ conn = Connection('1.2.3.4', 61613) conn.connect() self.assertTrue(conn.connected) conn.disconnect() self.assertFalse(conn.connected) conn.connect() self.assertTrue(conn.connected)
def test_disconnect_reconnect(self): """ Test disconnect and reconnect behavior. """ conn = Connection('1.2.3.4', 61613) conn.connect() conn.disconnect() self.assertFalse(conn.connected) conn.send("MESSAGE") self.assertTrue(self.mocksocket.connect.called) self.assertTrue(self.mocksocket.sendall.called)
def test_auto_connect(self): """ Test the fact that send and read automatically attempt to connect. """ conn = Connection('1.2.3.4', 61613) conn.send("MESSAGE") self.assertTrue(self.mocksocket.connect.called) conn.disconnect() f = frame.ConnectedFrame('my-session-id') self.mocksocket.reset_mock() self.mocksocket.recv.side_effect = lambda len: str(f) result = conn.read() self.assertEquals(str(f), str(result)) self.assertTrue(self.mocksocket.connect.called)