def test_reconnect_noConnectionMethod(self): """ Reconnection is not possible is the setConnect method hasn't been called. """ exc = Exception('foo') class BadConn(object): def __init__(self): self.called = [] def runQuery(self, *args): self.called.append(args) return defer.fail(exc) c1 = BadConn() pool = ConnectionPool() pool.add(c1) result = pool.runQuery('something', 'ran') self.assertEqual(self.failureResultOf(result).value, exc) self.assertEqual(len(c1.called), 1) self.assertIn(('something', 'ran'), c1.called)
def _makeBlockingPostgres(parsed, connections=1): import psycopg2 from psycopg2.extras import DictCursor connstr = mkConnStr(parsed) pool = ConnectionPool() pool.db_scheme = 'postgres' for i in xrange(connections): db = psycopg2.connect(connstr, cursor_factory=DictCursor) runner = PostgresRunner(db) pool.add(runner) return defer.succeed(pool)
def test_add(self): """ You can add connections to a pool """ mock = MagicMock() balancer = MagicMock() pool = ConnectionPool(pool=balancer) pool.add(mock) balancer.add.assert_called_once_with(mock)
def test_runOperation(self): """ You can run a query """ mock = MagicMock() mock.runOperation = MagicMock(return_value=defer.succeed('success')) pool = ConnectionPool() pool.add(mock) d = pool.runOperation('my query') self.assertEqual(self.successResultOf(d), 'success') mock.runOperation.assert_called_once_with('my query')
def test_setConnect(self): """ You can tell the connection pool how to make connections. """ called = [] def mkConnection(arg): called.append(arg) return 'connection' pool = ConnectionPool() pool.setConnect(mkConnection, 'foo') d = pool.makeConnection() self.assertEqual(self.successResultOf(d), 'connection') self.assertEqual(called, ['foo'])
def _makeTxPostgres(parsed, connections=1): from norm.tx_postgres import DictConnection pool = ConnectionPool() pool.db_scheme = 'postgres' connstr = mkConnStr(parsed) dlist = [] for i in xrange(connections): conn = DictConnection() d = conn.connect(connstr) d.addCallback(lambda _: pool.add(conn)) dlist.append(d) ret = defer.gatherResults(dlist) return ret.addCallback(lambda _: pool)
def test_close(self): """ You can close all the connections """ c1 = MagicMock() c2 = MagicMock() pool = ConnectionPool() pool.add(c1) pool.add(c2) d = pool.close() d.addCallback(lambda _: c1.close.assert_called_once_with()) d.addCallback(lambda _: c2.close.assert_called_once_with()) return d
def test_reconnect(self): """ If a query fails, make sure it wasn't just closed, reconnect and retry if it was. """ class BadConn(object): def __init__(self): self.called = [] def runQuery(self, *args): self.called.append(args) return defer.fail(Exception('foo')) c1 = BadConn() c2 = MagicMock() c2.runQuery.return_value = defer.succeed('success') pool = ConnectionPool() pool.add(c1) pool.setConnect(lambda:c2) result = yield pool.runQuery('something', 'ran') self.assertEqual(len(c1.called), 2) self.assertIn(('something', 'ran'), c1.called) c2.runQuery.assert_any_call('something', 'ran') self.assertEqual(pool.pool.list(), [c2], "Should have the new conn " "in the pool") self.assertEqual(result, 'success', "Should have eventually succeeded")
def test_returnToPool(self): """ After a successful query, interaction or operation, the connection should be returned to the pool """ mock = MagicMock() mock.runInteraction = MagicMock(return_value=defer.fail(Exception('foo'))) mock.runQuery = MagicMock(return_value=defer.succeed('something')) mock.runOperation = MagicMock(return_value=defer.succeed('success')) pool = ConnectionPool() pool.add(mock) d = pool.runQuery('query') self.assertEqual(self.successResultOf(d), 'something') d = pool.runOperation('operation') self.assertEqual(self.successResultOf(d), 'success') d = pool.runInteraction('interaction') self.assertIsInstance(self.failureResultOf(d).value, Exception)