Skip to content

Commit

Permalink
Cleaning up specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun Davis authored and Shaun Davis committed Aug 2, 2021
1 parent bc9be46 commit 2c4beee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion lib/etcdv3/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Etcdv3
VERSION = '0.11.2'.freeze
VERSION = '0.11.3'.freeze
end
122 changes: 68 additions & 54 deletions spec/etcdv3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -523,31 +523,38 @@
end

describe "namespace" do
let(:ns_conn) { local_connection_with_namespace("/namespace/") }

describe '#get' do
let(:get_conn) { local_connection_with_namespace("/namespace-get/") }

before do
conn.put('/apples/', 'app')
conn.put('/namespace/apple', 'apple')
conn.put('/namespace/apples', 'apples')
conn.put('/namespace/appless', 'appless')
conn.put('/namespace-get/apple', 'apple')
conn.put('/namespace-get/apples', 'apples')
conn.put('/namespace-get/appless', 'appless')
end

it 'returns key w/o namespace' do
expect(ns_conn.get("apple").kvs.last.value).to eq('apple')
expect(get_conn.get("apple").kvs.last.value).to eq('apple')
end

it 'returns keys w/o namespace' do
expect(ns_conn.get("apple", range_end: 'applf').kvs.size).to eq(3)
expect(get_conn.get("apple", range_end: 'applf').kvs.size).to eq(3)
end

it 'returns all keys under namespace' do
expect(get_conn.get("", range_end: "\0").kvs.size).to eq(3)
end
end

describe '#put' do
let(:put_conn) { local_connection_with_namespace("/namespace-put/") }

before do
ns_conn.put('apple_put', 'test')
put_conn.put('apple_put', 'test')
end
it 'returns key with namespace' do
expect(conn.get("/namespace/apple_put").kvs.last.value).to eq('test')
expect(conn.get("/namespace-put/apple_put").kvs.last.value).to eq('test')
end
end

Expand All @@ -559,10 +566,13 @@
del_conn.put('test', "key")
del_conn.put('test2', "key2")
conn.put('wall', 'zzzz')
conn.put('walzz', 'adsfas')
end

it 'deleting all keys should be scoped to namespace' do
resp = del_conn.del('', range_end: "\0")
resp = del_conn.del("", range_end: "\0")
expect(resp.deleted).to eq(2)
expect(conn.get("wall").kvs.last.value).to eq('zzzz')
end
end

Expand All @@ -583,24 +593,26 @@
end

describe '#transaction' do
let(:trans_conn) { local_connection_with_namespace("/namespace/") }

describe 'txn.value' do
before { ns_conn.put('txn', 'value') }
after { ns_conn.del('txn') }
before { trans_conn.put('txn', 'value') }
after { trans_conn.del('txn') }
context 'success' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.value('txn', :equal, 'value') ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('success')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('success')
expect(conn.get("/namespace/txn-test").kvs.first.value).to eq('success')
end
it "raises a GRPC::DeadlineExceeded exception when it takes too long" do
expect do
ns_conn.transaction(timeout: 0) do |txn|
trans_conn.transaction(timeout: 0) do |txn|
txn.compare = [ txn.value('txn', :equal, 'value') ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
Expand All @@ -609,7 +621,7 @@
end
it "accepts a timeout" do
expect do
ns_conn.transaction(timeout: 1) do |txn|
trans_conn.transaction(timeout: 1) do |txn|
txn.compare = [ txn.value('txn', :equal, 'value') ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
Expand All @@ -618,121 +630,121 @@
end
end
context "success, value with lease" do
let!(:lease_id) { ns_conn.lease_grant(2)['ID'] }
let!(:lease_id) { trans_conn.lease_grant(2)['ID'] }
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.value('txn', :equal, 'value') ]
txn.success = [ txn.put('txn-test', 'success', lease_id) ]
txn.failure = [ txn.put('txn-test', 'failed', lease_id) ]
end
end
it 'sets correct key, with a lease' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('success')
expect(ns_conn.get('txn-test').kvs.first.lease).to eq(lease_id)
expect(trans_conn.get('txn-test').kvs.first.value).to eq('success')
expect(trans_conn.get('txn-test').kvs.first.lease).to eq(lease_id)
end
end
context 'failure' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.value('txn', :equal, 'notright') ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed')
end
end
end

describe 'txn.create_revision' do
before { ns_conn.put('txn', 'value') }
after { ns_conn.del('txn') }
before { trans_conn.put('txn', 'value') }
after { trans_conn.del('txn') }
context 'success' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.create_revision('txn', :greater, 1) ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('success')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('success')
end
end
context 'failure' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.create_revision('txn', :equal, 1) ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed')
expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('failed')
end
end
end

describe 'txn.mod_revision' do
before { ns_conn.put('txn', 'value') }
after { ns_conn.del('txn') }
before { trans_conn.put('txn', 'value') }
after { trans_conn.del('txn') }
context 'success' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.mod_revision('txn', :less, 1000) ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('success')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('success')
expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('success')
end
end
context 'failure' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.mod_revision('txn', :greater, 1000) ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed')
expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('failed')

end
end
end

describe 'txn.version' do
before { ns_conn.put('txn-version', 'value') }
after { ns_conn.del('txn-version') }
before { trans_conn.put('txn-version', 'value') }
after { trans_conn.del('txn-version') }
context 'success' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.version('txn-version', :equal, 1) ]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('success')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('success')
expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('success')
end
end
context 'failure' do
subject! do
ns_conn.transaction do |txn|
trans_conn.transaction do |txn|
txn.compare = [ txn.version('txn', :equal, 100)]
txn.success = [ txn.put('txn-test', 'success') ]
txn.failure = [ txn.put('txn-test', 'failed') ]
end
end
it 'sets correct key' do
expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed')
expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed')
expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('failed')
end
end
Expand All @@ -741,27 +753,29 @@

# Locking is not implemented in etcd v3.1.X
unless $instance.version < Gem::Version.new("3.2.0")
describe '#lock' do
let(:lease_id) { lease_stub.lease_grant(10)['ID'] }
subject { ns_conn.lock('mylocklock', lease_id) }
it 'should lock key under specified namespace' do
expect(conn.get("/namespace/#{subject.key}").kvs).to_not be_empty
describe "locking" do
let(:ns_conn) { local_connection_with_namespace("/namespace/") }

describe '#lock' do
let(:lease_id) { lease_stub.lease_grant(10)['ID'] }
subject { ns_conn.lock('mylocklock', lease_id) }
it 'should lock key under specified namespace' do
expect(conn.get("/namespace/#{subject.key}").kvs).to_not be_empty
end
end
end

describe '#with_lock' do
let(:lease_id) { lease_stub.lease_grant(10)['ID'] }
let(:lease_id_2) { lease_stub.lease_grant(15)['ID'] }
it 'enforces lock' do
ns_conn.with_lock('mylock', lease_id) do
expect { ns_conn.lock('mylock', lease_id_2, timeout: 0.1) }
.to raise_error(GRPC::DeadlineExceeded)
describe '#with_lock' do
let(:lease_id) { lease_stub.lease_grant(10)['ID'] }
let(:lease_id_2) { lease_stub.lease_grant(15)['ID'] }
it 'enforces lock' do
ns_conn.with_lock('mylock', lease_id) do
expect { ns_conn.lock('mylock', lease_id_2, timeout: 0.1) }
.to raise_error(GRPC::DeadlineExceeded)
end
end
end
end
end


end
end
end

0 comments on commit 2c4beee

Please sign in to comment.