def testImplicitPipelining(self): """ Calling query method several times in a row without waiting should do implicit pipelining, so all requests are send immediately and all replies are received in one chunk with high probability """ db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) cnt_LineReceiver = _CallCounter(redis.LineReceiver.dataReceived) self.patch(redis.LineReceiver, 'dataReceived', cnt_LineReceiver.get_callee()) cnt_HiredisProtocol = _CallCounter(redis.HiredisProtocol.dataReceived) self.patch(redis.HiredisProtocol, 'dataReceived', cnt_HiredisProtocol.get_callee()) for i in range(5): db.set(self.KEY, self.VALUE) yield db.get(self.KEY) total_data_chunks = cnt_LineReceiver.call_count + \ cnt_HiredisProtocol.call_count self.assertEqual(total_data_chunks, 1) yield db.disconnect()
def setUp(self): self.db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) self.db1 = None self.redis_2_6 = yield self.is_redis_2_6() yield self.db.script_flush()
def connect(self): self.servers = [] serverDeferreds = [] for connection_info in self.connection_list: try: if type(connection_info) == dict: def addServer(res): self.servers.append(res) return res d = redis.Connection(**connection_info) d.addCallback(addServer) serverDeferreds.append(d) else: server = connection_info self.servers.append(server) except Exception as e: raise Warning(str(e)) def checkQuorun(res): self.quorum = (len(self.connection_list) // 2) + 1 if len(self.servers) < self.quorum: raise CannotObtainLock( "Failed to connect to the majority of redis servers") return res dl = DeferredList(serverDeferreds) dl.addCallback(checkQuorun) return dl
def testRedisLPUSHListOfValues(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) yield db.delete("txredisapi:LPUSH") yield db.lpush("txredisapi:LPUSH", [1, 2, 3]) result = yield db.lrange("txredisapi:LPUSH", 0, -1) self.assertEqual(result, [3, 2, 1]) yield db.disconnect()
def test_set_var_task(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) with inject_services(configure): ts = TaskService() r = yield ts.task_list_vars(123123) assert r == {} r = yield ts.task_set_var(123123, 'boo', '123') assert r == {'boo': 123} r = yield ts.task_list_vars(123123) assert r == {'boo': 123} r = yield ts.task_set_var(123123, 'boo2', '1234') assert r == {'boo': 123, 'boo2': 1234} r = yield ts.task_rm_var(123123, 'boo') assert r == {'boo2': 1234}
def test_ConnectionDB1(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, dbid=1, reconnect=False) self.assertEqual(isinstance(db, redis.ConnectionHandler), True) yield db.disconnect()
def test_ConnectionDB1(self): db = yield redis.Connection(redis_host, redis_port, dbid=1, reconnect=False) self.assertEqual(isinstance(db, redis.ConnectionHandler), True) yield db.disconnect()
def test_script_kill(self): self._skipCheck() try: result = yield self.db.script_kill() except redis.NoScriptRunning: pass except: raise self.failureException('%s raised instead of %s:\n %s' % (sys.exc_info()[0], 'txredisapi.NoScriptRunning', failure.Failure().getTraceback())) else: raise self.failureException('%s not raised (%r returned)' % ('txredisapi.ResponseError', result)) # Run an infinite loop script from one connection # and kill it from another. inf_loop = "while 1 do end" self.db1 = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) eval_deferred = self.db1.eval(inf_loop) reactor.iterate() r = yield self.db.script_kill() self.assertEqual(r, 'OK') try: result = yield eval_deferred except redis.ResponseError: pass except: raise self.failureException('%s raised instead of %s:\n %s' % (sys.exc_info()[0], 'txredisapi.ResponseError', failure.Failure().getTraceback())) else: raise self.failureException('%s not raised (%r returned)' % ('txredisapi.ResponseError', result))
def testRedisRPUSHSingleValue(self): db = yield redis.Connection(redis_host, redis_port, reconnect=False) yield db.delete("txredisapi:RPUSH") yield db.lpush("txredisapi:RPUSH", "singlevalue") result = yield db.lpop("txredisapi:RPUSH") self.assertEqual(result, "singlevalue") yield db.disconnect()
def test_cancel(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT) prefix = 'txredisapi:cancel' # Set + Get key = prefix + '1' value = 'first' res = yield db.set(key, value) self.assertEquals('OK', res) val = yield db.get(key) self.assertEquals(val, value) # Cancel a method d = db.time() d.addErrback(lambda _: True) d.cancel() # And Set + Get key = prefix + '2' value = 'second' res = yield db.set(key, value) #self.assertEquals('OK', res) val = yield db.get(key) self.assertEquals(val, value) yield db.disconnect()
def test_Connection(self): db = yield txredisapi.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) yield self._assert_simple_sets_on_pipeline(db=db) yield db.disconnect()
def testRedisError(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) yield db.set('txredisapi:a', 'test') try: yield db.sort('txredisapi:a', end='a') except redis.RedisError: pass else: yield db.disconnect() self.fail('RedisError not raised') try: yield db.incr('txredisapi:a') except redis.ResponseError: pass else: yield db.disconnect() self.fail('ResponseError not raised on redis error') yield db.disconnect() try: yield db.get('txredisapi:a') except redis.ConnectionError: pass else: self.fail('ConnectionError not raised')
def testRedisPublish(self): db = yield redis.Connection(redis_host, redis_port, reconnect=False) for value in ("foo", "bar"): yield db.publish("test_publish", value) yield db.disconnect()
def testRedisLPUSHSingleValue(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) yield db.delete("txredisapi:LPUSH") yield db.lpush("txredisapi:LPUSH", "singlevalue") result = yield db.lpop("txredisapi:LPUSH") self.assertEqual(result, "singlevalue") yield db.disconnect()
def test_deployment_controller_publish_app(): redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() eb = flexmock() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) with inject_services(configure): controller = DeploymentController() eb.should_receive('fire_event').times(3) yield controller.create('foo', None) r = yield controller.get('foo') assert r.public_app is None yield controller.publish_app('foo', 'bar') r = yield controller.get('foo') assert r.public_app == 'bar.foo' yield controller.unpublish_app('foo') r = yield controller.get('foo') assert r.public_app is None
def testRedisRPUSHListOfValues(self): db = yield redis.Connection(redis_host, redis_port, reconnect=False) yield db.delete("txredisapi:RPUSH") yield db.lpush("txredisapi:RPUSH", [1,2,3]) result = yield db.lrange("txredisapi:RPUSH", 0, -1) self.assertEqual(result, [3,2,1]) yield db.disconnect()
def onJoin(self, details): try: self.rc = yield redis.Connection() yield self.register(self.authenticate, 'com.example.authenticate') print("WAMP-Ticket dynamic authenticator registered!") except Exception as e: print("Failed to register dynamic authenticator: {0}".format(e))
def test_setget(): key = 'test' conn = yield redis.Connection(HOST, PORT) start = time.time() for i in xrange(N): yield conn.set(key, 'test_data') yield conn.get(key) print("done set-get: %.4fs." % ((time.time() - start) / N))
def testRedisHSetHGet(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) for hk in ("foo", "bar"): yield db.hset("txredisapi:HSetHGet", hk, 1) result = yield db.hget("txredisapi:HSetHGet", hk) self.assertEqual(result, 1) yield db.disconnect()
def test_deployment_controller(): redis = yield txredisapi.Connection(dbid=2) yield redis.flushdb() eb = flexmock() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) with inject_services(configure): controller = DeploymentController() with pytest.raises(DeploymentDoesNotExist): yield controller.get('foo') eb.should_receive('fire_event').with_args( 'new-deployment', public_app=None, apps=[], name='foo', public_domain='foo.bar').once() eb.should_receive('fire_event').with_args( 'new-deployment', public_app=None, apps=[], name='boo', public_domain='other.path').once() r = yield controller.create('foo', 'foo.bar') assert isinstance(r, Deployment) assert r.public_domain == 'foo.bar' assert r.name == 'foo' r = yield controller.get('foo') assert r.public_domain == 'foo.bar' assert r.name == 'foo' r = yield controller.create('boo', 'other.path') assert isinstance(r, Deployment) assert r.public_domain == 'other.path' assert r.name == 'boo' r = yield controller.list() assert isinstance(r, list) assert len(r) == 2 for app in r: assert isinstance(app, Deployment) eb.should_receive('fire_event').with_args('remove-deployment', name='foo').once() yield controller.remove('foo') with pytest.raises(DeploymentDoesNotExist): yield controller.get('foo')
def test_dbid(self): db1 = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False, dbid=1) db2 = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False, dbid=5) self.addCleanup(db1.disconnect) self.addCleanup(db2.disconnect) yield db1.set('x', 42) try: value = yield db2.get('x') self.assertIs(value, None) finally: yield db1.delete('x')
def init_database(self): self.mongo = yield txmongo.MongoConnection( host=self.setting.DATA_MONGO_HOST, port=self.setting.DATA_MONGO_PORT) self.redis = yield txredisapi.Connection( host=self.setting.QUEUE_REDIS_HOST, port=self.setting.QUEUE_REDIS_PORT) #yield self.mongo.admin.authenticate("root", "chenfuzhi") defer.returnValue(True)
def testRedisHGetAll(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) d = {u"key1": u"uno", u"key2": u"dos"} yield db.hmset("txredisapi:HGetAll", d) s = yield db.hgetall("txredisapi:HGetAll") self.assertEqual(d, s) yield db.disconnect()
def test_charset_default(self): db = yield redis.Connection() yield db.set(self.TEST_KEY, self.TEST_VALUE_UNICODE) result = yield db.get(self.TEST_KEY) self.assertTrue(type(result) == unicode) self.assertEqual(result, self.TEST_VALUE_UNICODE) yield db.delete(self.TEST_KEY) yield db.disconnect()
def testWatchEdgeCase_1(self): rapi = yield txredisapi.Connection(redis_host, redis_port) tx = yield rapi.multi("foobar") yield tx.unwatch() self.assertTrue(tx.inTransaction) yield tx.discard() self.assertFalse(tx.inTransaction) yield rapi.disconnect()
def test_lrange(): key = 'test_list' list_length = 1000 conn = yield redis.Connection(HOST, PORT) yield defer.DeferredList( [conn.lpush(key, str(i)) for i in xrange(list_length)]) start = time.time() for i in xrange(N): yield conn.lrange(key, 0, 999) print("done lrange: %.4fs." % ((time.time() - start) / N))
def test_charset_None(self): db = yield redis.Connection(charset=None) yield db.set(self.TEST_KEY, self.TEST_VALUE_BINARY) result = yield db.get(self.TEST_KEY) self.assertTrue(type(result) == str) self.assertEqual(result, self.TEST_VALUE_BINARY) yield db.delete(self.TEST_KEY) yield db.disconnect()
def testRedisOperationTime(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, reconnect=False) time = yield db.time() self.assertIsInstance(time, list) self.assertEqual(len(time), 2) self.assertIsInstance(time[0], int) self.assertIsInstance(time[1], int) yield db.disconnect()
def test_charset_None(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT, charset=None) yield db.set(self.TEST_KEY, self.TEST_VALUE_BINARY) result = yield db.get(self.TEST_KEY) self.assertTrue(type(result) == six.binary_type) self.assertEqual(result, self.TEST_VALUE_BINARY) yield db.delete(self.TEST_KEY) yield db.disconnect()
def test_charset_default(self): db = yield redis.Connection(REDIS_HOST, REDIS_PORT) yield db.set(self.TEST_KEY, self.TEST_VALUE_UNICODE) result = yield db.get(self.TEST_KEY) self.assertEqual(result, self.TEST_VALUE_UNICODE) self.assertTrue(type(result) == six.text_type) yield db.delete(self.TEST_KEY) yield db.disconnect()