def test_add_redis_note_failure(self, get_redis_connection, start_watch): """Verifies that notes attempt re-add on failure""" redis_notes = RedisNotes() get_redis_connection.return_value = self.redis start_watch.side_effect = [ redis.WatchError(), True, ] redis_notes.add_redis_note(1373500800, "today: this is a simple test #yolo") self.assertEqual(2, len(start_watch.mock_calls)) self.assertEqual(b"today: this is a simple test #yolo", self.redis.get('note_1373500800'))
def transaction(self, func, *keys): # We use a for loop instead of while # because if the test this is being used in # goes wrong we don't want an infinite loop! with self.pipeline() as p: for _ in range(5): try: p.watch(*keys) func(p) return p.execute() except redis.WatchError: continue raise redis.WatchError('Could not run transaction after 5 tries')
def execute(self): """Run all the commands in the pipeline and return the results.""" if self.watching: mismatches = [(k, v, u) for (k, v, u) in [(k, v, self.owner._db[k]) for (k, v) in self.watching.items()] if v != u] if mismatches: self.commands = [] raise redis.WatchError( 'Watched key%s %s changed' % ('' if len(mismatches) == 1 else 's', ', '.join( k for (k, _, _) in mismatches))) return [ getattr(self.owner, name)(*args, **kwargs) for name, args, kwargs in self.commands ]
def node_get_all(self): r = self._get_redis() nodes_key = self.prefix + 'nodes' while True: with r.pipeline() as pipe: try: pipe.watch(nodes_key) node_ids = pipe.smembers(nodes_key) nodes = dict() for node_id in node_ids: node_key = self.prefix + 'node_' + node_id pipe.watch(node_key) info_raw = pipe.get(node_key) if not info_raw: raise redis.WatchError() nodes[node_id] = json.loads(info_raw) pipe.multi() pipe.execute() return nodes except redis.WatchError: continue