def test_watch(uuid): uuid.uuid4.return_value = "abc123" redis = mock.Mock(pipeline=mock.Mock(spec=("watch",)), spec=("pipeline",)) pipe = redis.pipeline.return_value keys = ["foo", "bar"] cache = global_cache.RedisCache(redis) cache.watch(keys) pipe.watch.assert_called_once_with("foo", "bar") assert cache.pipes == { "foo": global_cache._Pipeline(pipe, "abc123"), "bar": global_cache._Pipeline(pipe, "abc123"), }
def test_compare_and_swap(): redis = mock.Mock(spec=()) cache = global_cache.RedisCache(redis) pipe1 = mock.Mock(spec=("multi", "mset", "execute", "reset")) pipe2 = mock.Mock(spec=("multi", "mset", "execute", "reset")) cache.pipes = { "ay": global_cache._Pipeline(pipe1, "abc123"), "be": global_cache._Pipeline(pipe1, "abc123"), "see": global_cache._Pipeline(pipe2, "def456"), "dee": global_cache._Pipeline(pipe2, "def456"), "whatevs": global_cache._Pipeline(None, "himom!"), } pipe2.execute.side_effect = redis_module.exceptions.WatchError items = {"ay": "foo", "be": "bar", "see": "baz", "wut": "huh?"} cache.compare_and_swap(items) pipe1.multi.assert_called_once_with() pipe2.multi.assert_called_once_with() pipe1.mset.assert_called_once_with({"ay": "foo", "be": "bar"}) pipe2.mset.assert_called_once_with({"see": "baz"}) pipe1.execute.assert_called_once_with() pipe2.execute.assert_called_once_with() pipe1.reset.assert_called_once_with() pipe2.reset.assert_called_once_with() assert cache.pipes == { "whatevs": global_cache._Pipeline(None, "himom!") }
def test_unwatch(): redis = mock.Mock(spec=()) cache = global_cache.RedisCache(redis) pipe1 = mock.Mock(spec=("reset", )) pipe2 = mock.Mock(spec=("reset", )) cache._pipes.pipes = { "ay": global_cache._Pipeline(pipe1, "abc123"), "be": global_cache._Pipeline(pipe1, "abc123"), "see": global_cache._Pipeline(pipe2, "def456"), "dee": global_cache._Pipeline(pipe2, "def456"), "whatevs": global_cache._Pipeline(None, "himom!"), } cache.unwatch(["ay", "be", "see", "dee", "nuffin"]) assert cache.pipes == { "whatevs": global_cache._Pipeline(None, "himom!") }