我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用redis.ResponseError()。
def assertResponseError(self, msg=None): """ Assert that a context block with a redis command triggers a redis error response. For Example: with self.assertResponseError(): r.execute_command('non_existing_command') """ try: yield except ResponseError: pass else: self.fail("Expected redis ResponseError " + (msg or ''))
def testInvalidTaskTableAdd(self): # Check that Redis returns an error when RAY.TASK_TABLE_ADD is called # with the wrong arguments. with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.TASK_TABLE_ADD") with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.TASK_TABLE_ADD", "hello") with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.TASK_TABLE_ADD", "task_id", 3, "node_id") with self.assertRaises(redis.ResponseError): # Non-integer scheduling states should not be added. self.redis.execute_command("RAY.TASK_TABLE_ADD", "task_id", "invalid_state", "node_id", "task_spec") with self.assertRaises(redis.ResponseError): # Should not be able to update a non-existent task. self.redis.execute_command("RAY.TASK_TABLE_UPDATE", "task_id", 10, "node_id", b"")
def post(self): # set CORS for cross-origin request # CORS Beginning self.set_header('Access-Control-Allow-Origin', '*') self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') self.set_header('Access-Control-Max-Age', 1000) self.set_header('Access-Control-Allow-Headers', '*') # CORS Ending type = self.get_argument('type').strip() # TODO ????????redis???? # ResponseError: NOAUTH Authentication required. try: return_code = self.redis.publish(options.redis_channel, type) if return_code > 0: self.set_status(200) else: self.set_status(500) except redis.ResponseError as e: # self.logger.error('===== error: ' + e + ' =====') self.logger.error(e)
def post(self): # set CORS for cross-origin request # CORS Beginning self.set_header('Access-Control-Allow-Origin', '*') self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') self.set_header('Access-Control-Max-Age', 1000) self.set_header('Access-Control-Allow-Headers', '*') # CORS Ending type = self.get_argument('type').strip() # TODO ????????redis???? # ResponseError: NOAUTH Authentication required. try: return_code = self.redis.publish(options.redis_channel, type) if return_code > 0: self.set_status(200) else: self.set_status(500) except redis.ResponseError as e: # self.logger.error('===== error: ' + e + ' =====') self.logger.error(e) #logger.error(e)
def _start_process(self): if self._is_external: return if REDIS_DEBUGGER: debugger = REDIS_DEBUGGER.split() args = debugger + self.args else: args = self.args stdout = None if REDIS_SHOW_OUTPUT else subprocess.PIPE if REDIS_SHOW_OUTPUT: sys.stderr.write("Executing: {}".format(repr(args))) self.process = subprocess.Popen( args, stdin=sys.stdin, stdout=stdout, stderr=sys.stderr, ) begin = time.time() while True: try: self.client().ping() break except (redis.ConnectionError, redis.ResponseError): self.process.poll() if self.process.returncode is not None: raise RuntimeError( "Process has exited with code {}\n. Redis output: {}" .format(self.process.returncode, self._get_output())) if time.time() - begin > 300: raise RuntimeError('Cannot initialize client (waited 5mins)') time.sleep(0.1)
def createIndex(self, client, num_docs = 100): assert isinstance(client, Client) #conn.flushdb() #client = Client('test', port=conn.port) try: client.create_index((TextField('play', weight=5.0), TextField('txt'), NumericField('chapter'))) except redis.ResponseError: client.drop_index() return self.createIndex(client, num_docs=num_docs) chapters = {} with bz2.BZ2File(WILL_PLAY_TEXT) as fp: r = csv.reader(fp, delimiter=';') for n, line in enumerate(r): #['62816', 'Merchant of Venice', '9', '3.2.74', 'PORTIA', "I'll begin it,--Ding, dong, bell."] play, chapter, character, text = line[1], line[2], line[4], line[5] key = '{}:{}'.format(play, chapter).lower() d = chapters.setdefault(key, {}) d['play'] = play d['txt'] = d.get('txt', '') + ' ' + text d['chapter'] = int(chapter or 0) if len(chapters) == num_docs: break indexer = client.batch_indexer(chunk_size=50) self.assertIsInstance(indexer, Client.BatchIndexer) self.assertEqual(50, indexer.chunk_size) for key, doc in chapters.iteritems(): indexer.add_document(key, **doc) indexer.commit()
def replicate(ctx, args): master = Node.from_uri(args.master) if not master.is_master(): ctx.abort('Node {} is not a master.'.format(args.master)) cluster = Cluster.from_node(master) cluster.add_node(args.slave) slave = Node.from_uri(args.slave) try: slave.replicate(master.name) except redis.ResponseError as e: ctx.abort(str(e)) cluster.wait()
def testObjectTableAddAndLookup(self): # Try calling RAY.OBJECT_TABLE_LOOKUP with an object ID that has not # been added yet. response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(response, None) # Add some managers and try again. self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash1", "manager_id1") self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash1", "manager_id2") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id1", b"manager_id2"}) # Add a manager that already exists again and try again. self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash1", "manager_id2") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id1", b"manager_id2"}) # Check that we properly handle NULL characters. In the past, NULL # characters were handled improperly causing a "hash mismatch" error if # two object IDs that agreed up to the NULL character were inserted # with different hashes. self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "\x00object_id3", 1, "hash1", "manager_id1") self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "\x00object_id4", 1, "hash2", "manager_id1") # Check that NULL characters in the hash are handled properly. self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id3", 1, "\x00hash1", "manager_id1") with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id3", 1, "\x00hash2", "manager_id1")
def incr(self, key, delta=1, version=None): """ Add delta to value in the cache. If the key does not exist, raise a ValueError exception. """ key = self.make_key(key, version=version) exists = self._client.exists(key) if not exists: raise ValueError("Key '%s' not found" % key) try: value = self._client.incr(key, delta) except redis.ResponseError: value = self.get(key) + delta self.set(key, value) return value
def testInvalidObjectTableAdd(self): # Check that Redis returns an error when RAY.OBJECT_TABLE_ADD is called # with the wrong arguments. with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.OBJECT_TABLE_ADD") with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "hello") with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id2", "one", "hash2", "manager_id1") with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id2", 1, "hash2", "manager_id1", "extra argument") # Check that Redis returns an error when RAY.OBJECT_TABLE_ADD adds an # object ID that is already present with a different hash. self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash1", "manager_id1") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id1"}) with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash2", "manager_id2") # Check that the second manager was added, even though the hash was # mismatched. response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id1", b"manager_id2"}) # Check that it is fine if we add the same object ID multiple times # with the most recent hash. self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash2", "manager_id1") self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash2", "manager_id1") self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash2", "manager_id2") self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 2, "hash2", "manager_id2") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id1", b"manager_id2"})
def testObjectTableAddAndRemove(self): # Try removing a manager from an object ID that has not been added yet. with self.assertRaises(redis.ResponseError): self.redis.execute_command("RAY.OBJECT_TABLE_REMOVE", "object_id1", "manager_id1") # Try calling RAY.OBJECT_TABLE_LOOKUP with an object ID that has not # been added yet. response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(response, None) # Add some managers and try again. self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash1", "manager_id1") self.redis.execute_command("RAY.OBJECT_TABLE_ADD", "object_id1", 1, "hash1", "manager_id2") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id1", b"manager_id2"}) # Remove a manager that doesn't exist, and make sure we still have the # same set. self.redis.execute_command("RAY.OBJECT_TABLE_REMOVE", "object_id1", "manager_id3") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id1", b"manager_id2"}) # Remove a manager that does exist. Make sure it gets removed the first # time and does nothing the second time. self.redis.execute_command("RAY.OBJECT_TABLE_REMOVE", "object_id1", "manager_id1") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id2"}) self.redis.execute_command("RAY.OBJECT_TABLE_REMOVE", "object_id1", "manager_id1") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), {b"manager_id2"}) # Remove the last manager, and make sure we have an empty set. self.redis.execute_command("RAY.OBJECT_TABLE_REMOVE", "object_id1", "manager_id2") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), set()) # Remove a manager from an empty set, and make sure we now have an # empty set. self.redis.execute_command("RAY.OBJECT_TABLE_REMOVE", "object_id1", "manager_id3") response = self.redis.execute_command("RAY.OBJECT_TABLE_LOOKUP", "object_id1") self.assertEqual(set(response), set())