From 8b29bfa303db13248790e9fe108c99d1b1a0d3a9 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Wed, 2 Sep 2015 13:32:12 -0700 Subject: [PATCH] consul/state: fix for maxIndex and better tests --- consul/state/state_store.go | 5 ++--- consul/state/state_store_test.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/consul/state/state_store.go b/consul/state/state_store.go index 98d57b29a4..5d1a2cda87 100644 --- a/consul/state/state_store.go +++ b/consul/state/state_store.go @@ -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 diff --git a/consul/state/state_store_test.go b/consul/state/state_store_test.go index 2f8816e774..da5aa67f0a 100644 --- a/consul/state/state_store_test.go +++ b/consul/state/state_store_test.go @@ -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) + } }