consul/state: fix for maxIndex and better tests

This commit is contained in:
Ryan Uber 2015-09-02 13:32:12 -07:00 committed by James Phillips
parent a0dc2ded8d
commit 8b29bfa303
2 changed files with 17 additions and 3 deletions

View File

@ -63,9 +63,8 @@ func (s *StateStore) maxIndex(tables ...string) uint64 {
if err != nil {
panic(fmt.Sprintf("unknown index: %s", table))
}
idx := ti.(*IndexEntry).Value
if idx > lindex {
lindex = idx
if idx, ok := ti.(*IndexEntry); ok && idx.Value > lindex {
lindex = idx.Value
}
}
return lindex

View File

@ -1012,6 +1012,11 @@ func TestStateStore_KVSSetCAS(t *testing.T) {
}
tx.Abort()
// Index was not updated
if idx := s.maxIndex("kvs"); idx != 0 {
t.Fatalf("bad index: %d", idx)
}
// Doing a CAS with a ModifyIndex of zero when no entry exists
// performs the set and saves into the state store.
entry = &structs.DirEntry{
@ -1034,6 +1039,11 @@ func TestStateStore_KVSSetCAS(t *testing.T) {
}
tx.Abort()
// Index was updated
if idx := s.maxIndex("kvs"); idx != 2 {
t.Fatalf("bad index: %d", idx)
}
// Doing a CAS with a ModifyIndex which does not match the current
// index does not do anything.
entry = &structs.DirEntry{
@ -1060,4 +1070,9 @@ func TestStateStore_KVSSetCAS(t *testing.T) {
result.ModifyIndex != 2 || string(result.Value) != "foo" {
t.Fatalf("bad: %#v", result)
}
// Index was not modified
if idx := s.maxIndex("kvs"); idx != 2 {
t.Fatalf("bad index: %d", idx)
}
}