Commit afd77e60 authored by Rajko Albrecht's avatar Rajko Albrecht

single cas finished, including tests

parent b4c71b80
......@@ -5,13 +5,27 @@ class Redis
module Cas
def cas key, ttl=nil
res = watch(key) do
value = get(key)
break false if value.nil?
value = yield value
ires = multi do |multi|
multi.set(key,value,{:expire_in => ttl})
end
return_value ires
end
res
end
def cas_multi keys, ttl=nil
end
private
def return_value result
result.is_a?(Array) && result[0] == 'OK'
end
end
end
......
require 'test_helper'
describe Redis::Store::StoreWithCas do
def setup
@store = Redis::Store::StoreWithCas.new
@client = @store.instance_variable_get(:@client)
end
def teardown
@store.flushdb
@store.quit
end
def test_cas
@store.set('foo', 'baz')
assert(@store.cas('foo') do |value|
assert_equal 'baz', value
'bar'
end)
assert_equal 'bar', @store.get('foo')
end
def test_cas_with_cache_miss
refute @store.cas('not_exist') { |_value| flunk }
end
def test_cas_with_conflict
@store.set('foo', 'bar')
refute @store.cas('foo') { |_value|
@store.set('foo', 'baz')
'biz'
}
assert_equal 'baz', @store.get('foo')
end
end
require 'bundler/setup'
require 'minitest/autorun'
require 'mocha/setup'
require 'redis-store-with-cas'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment