def test_connect(self): client = self.client._get_server() self.client._get_server = lambda: client # Already connected client.transport = _MockTransport() client.transport.isOpen = lambda: True self.assert_(self.client._connect()) # Not connected, no error nopens = client.transport.calls['open'] with save(client.transport, ('isOpen',)): client.transport.isOpen = lambda: False self.assert_(self.client._connect()) self.assert_(client.transport.calls['open'] == nopens + 1) # Thrift Exception on connect - trapped ncloses = client.transport.calls['close'] with save(client.transport, ('isOpen', 'open')): client.transport.isOpen = lambda: False client.transport.open = raises(TTransportException) self.assertRaises(ErrorThriftMessage, self.client._connect) self.assert_(client.transport.calls['close'] == ncloses +1) # Other exception on connect should be ignored with save(client.transport, ('isOpen', 'open')): client.transport.isOpen = lambda: False client.transport.open = raises(Exception) ncloses = client.transport.calls['close'] self.assertRaises(Exception, self.client._connect) # Connection recycling - reuse same connection nopens = client.transport.calls['open'] ncloses = client.transport.calls['close'] conn = self.client._connect() self.client._recycle = 60 client.connect_time = time.time() self.assert_(conn is self.client._connect()) self.assert_(client.transport.calls['open'] == nopens) self.assert_(client.transport.calls['open'] == ncloses) # Recycling - reconnect nopens = client.transport.calls['open'] ncloses = client.transport.calls['close'] conn = self.client._connect() self.client._recycle = 60 client.connect_time = 0 self.assert_(conn is self.client._connect()) self.assert_(client.transport.calls['open'] == nopens + 1) self.assert_(client.transport.calls['close'] == ncloses + 1)
def test_connect(self): client = self.client._get_server() self.client._get_server = lambda: client # Already connected client.transport = _MockTransport() client.transport.isOpen = lambda: True self.assert_(self.client._connect()) # Not connected, no error nopens = client.transport.calls['open'] with save(client.transport, ('isOpen', )): client.transport.isOpen = lambda: False self.assert_(self.client._connect()) self.assert_(client.transport.calls['open'] == nopens + 1) # Thrift Exception on connect - trapped ncloses = client.transport.calls['close'] with save(client.transport, ('isOpen', 'open')): client.transport.isOpen = lambda: False client.transport.open = raises(TTransportException) self.assertRaises(ErrorThriftMessage, self.client._connect) self.assert_(client.transport.calls['close'] == ncloses + 1) # Other exception on connect should be ignored with save(client.transport, ('isOpen', 'open')): client.transport.isOpen = lambda: False client.transport.open = raises(Exception) ncloses = client.transport.calls['close'] self.assertRaises(Exception, self.client._connect) # Connection recycling - reuse same connection nopens = client.transport.calls['open'] ncloses = client.transport.calls['close'] conn = self.client._connect() self.client._recycle = 60 client.connect_time = time.time() self.assert_(conn is self.client._connect()) self.assert_(client.transport.calls['open'] == nopens) self.assert_(client.transport.calls['open'] == ncloses) # Recycling - reconnect nopens = client.transport.calls['open'] ncloses = client.transport.calls['close'] conn = self.client._connect() self.client._recycle = 60 client.connect_time = 0 self.assert_(conn is self.client._connect()) self.assert_(client.transport.calls['open'] == nopens + 1) self.assert_(client.transport.calls['close'] == ncloses + 1)
def test_build_server(self): exc_classes = (InvalidRequestException, UnavailableException, Thrift.TException) cls = Cassandra.Client srv = self.client._build_server(cls, "localhost", 1234) self.assert_(isinstance(srv, Cassandra.Client)) self.client._timeout = 250 srv = self.client._build_server(cls, "localhost", 1234) self.assert_(srv._iprot.trans._TBufferedTransport__trans._timeout == self.client._timeout * 0.001) self.assert_(isinstance(srv, Cassandra.Client)) with save(conn.TSocket, ("TSocket",)): for exc_class in exc_classes: conn.TSocket.TSocket = raises(exc_class) self.assert_(self.client._build_server(cls, "localhost", 1234) is None)
def test_build_server(self): exc_classes = (InvalidRequestException, UnavailableException, Thrift.TException) cls = Cassandra.Client srv = self.client._build_server(cls, 'localhost', 1234) self.assert_(isinstance(srv, Cassandra.Client)) self.client._timeout = 250 srv = self.client._build_server(cls, 'localhost', 1234) self.assert_(srv._iprot.trans._TFramedTransport__trans._timeout == self.client._timeout * .001) self.assert_(isinstance(srv, Cassandra.Client)) with save(conn.TSocket, ('TSocket', )): for exc_class in exc_classes: conn.TSocket.TSocket = raises(exc_class) self.assert_( self.client._build_server(cls, 'localhost', 1234) is None)
def setUp(self): Cassandra.Iface.raises = raises(Exception)