Skip to content

Commit

Permalink
Add 'cas' command test
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Jul 25, 2017
1 parent 7ea7316 commit 5952dc0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
45 changes: 45 additions & 0 deletions test/cas_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative "test_helper"

describe "cas" do
it "stores a value if cas_unique is correct" do
with_bashcached_and_client do |client|
expect_set client, value: "test1"
expect_cas client, value: "test2", cas_unique: 1, result: "STORED"
expect_gets client, value: "test2", cas_unique: 2
end
end

it "does not store a value if cas_unique is wrong" do
with_bashcached_and_client do |client|
expect_set client, value: "test1"
expect_set client, value: "test2"
expect_cas client, value: "test3", cas_unique: 1, result: "EXISTS"
expect_gets client, value: "test2", cas_unique: 2
end
end

it "does not store a value if the key is not found" do
with_bashcached_and_client do |client|
expect_cas client, value: "test1", cas_unique: 1, result: "NOT_FOUND"
expect_not_get client
end
end

it "can set a flag" do
with_bashcached_and_client do |client|
expect_set client, value: "test1"
expect_cas client, value: "test2", flags: 42, cas_unique: 1, result: "STORED"
expect_gets client, value: "test2", flags: 42, cas_unique: 2
end
end

it "can set an exptime" do
with_bashcached_and_client do |client|
expect_set client, value: "test1"
expect_cas client, value: "test2", exptime: 2, cas_unique: 1, result: "STORED"
expect_gets client, value: "test2", cas_unique: 2
sleep 2.5
expect_not_get client
end
end
end
11 changes: 9 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def with_bashcached_and_client
end
end

def write_store_command(client, command, key, flags, exptime, value, noreply)
client << "#{command} #{key} #{flags} #{exptime} #{value.bytesize}#{noreply ? " noreply" : ""}\r\n"
def write_store_command(client, command, key, flags, exptime, value, noreply, cas_unique = nil)
client << "#{command} #{key} #{flags} #{exptime} #{value.bytesize}#{cas_unique && " #{cas_unique}"}#{noreply ? " noreply" : ""}\r\n"
client << "#{value.b}\r\n"
end

Expand Down Expand Up @@ -112,6 +112,13 @@ def expect_prepend(client, key: "test", value:, flags: 0, exptime: 0, noreply: f
end
end

def expect_cas(client, key: "test", value:, flags: 0, exptime: 0, cas_unique:, noreply: false, result:)
write_store_command client, "cas", key, flags, exptime, value, noreply, cas_unique
unless noreply
client.gets.must_equal "#{result}\r\n"
end
end

def expect_get(client, key: "test", value:, flags: 0)
expect_get_many client, key => {value: value, flags: flags}
end
Expand Down

0 comments on commit 5952dc0

Please sign in to comment.