consul: Test the KVSDeleteCheckAndSet method

This commit is contained in:
Armon Dadgar 2015-01-08 17:24:36 -08:00
parent 3e0f77682a
commit 2bd5c3f7e5
2 changed files with 54 additions and 1 deletions

View File

@ -1329,7 +1329,10 @@ func (s *StateStore) KVSDeleteCheckAndSet(index uint64, key string, casIndex uin
}
// Do the actual delete
return true, s.kvsDeleteWithIndex(index, "id", key)
if err := s.kvsDeleteWithIndexTxn(index, tx, "id", key); err != nil {
return false, err
}
return true, tx.Commit()
}
// KVSDeleteTree is used to delete all keys with a given prefix

View File

@ -1569,6 +1569,56 @@ func TestKVSDelete(t *testing.T) {
}
}
func TestKVSDeleteCheckAndSet(t *testing.T) {
store, err := testStateStore()
if err != nil {
t.Fatalf("err: %v", err)
}
defer store.Close()
// CAS should fail, no entry
ok, err := store.KVSDeleteCheckAndSet(1000, "/foo", 100)
if err != nil {
t.Fatalf("err: %v", err)
}
if ok {
t.Fatalf("unexpected commit")
}
// CAS should work, no entry
ok, err = store.KVSDeleteCheckAndSet(1000, "/foo", 0)
if err != nil {
t.Fatalf("err: %v", err)
}
if !ok {
t.Fatalf("unexpected failure")
}
// Make an entry
d := &structs.DirEntry{Key: "/foo"}
if err := store.KVSSet(1000, d); err != nil {
t.Fatalf("err: %v", err)
}
// Constrain on a wrong modify time
ok, err = store.KVSDeleteCheckAndSet(1001, "/foo", 42)
if err != nil {
t.Fatalf("err: %v", err)
}
if ok {
t.Fatalf("unexpected commit")
}
// Constrain on a correct modify time
ok, err = store.KVSDeleteCheckAndSet(1002, "/foo", 1000)
if err != nil {
t.Fatalf("err: %v", err)
}
if !ok {
t.Fatalf("expected commit")
}
}
func TestKVSCheckAndSet(t *testing.T) {
store, err := testStateStore()
if err != nil {