consul/state: test for index modification during deletes

This commit is contained in:
Ryan Uber 2015-08-24 19:03:28 -07:00 committed by James Phillips
parent 8a8aef0503
commit 1d4a6ea790
2 changed files with 28 additions and 2 deletions

View File

@ -40,6 +40,26 @@ func NewStateStore(logOutput io.Writer) (*StateStore, error) {
return s, nil
}
// maxIndex is a helper used to retrieve the highest known index
// amongst a set of tables in the db.
func (s *StateStore) maxIndex(tables ...string) uint64 {
tx := s.db.Txn(false)
defer tx.Abort()
var lindex uint64
for _, table := range tables {
ti, err := tx.First("index", "id", table)
if err != nil {
panic(fmt.Sprintf("unknown index: %s", table))
}
idx := ti.(*IndexEntry).Value
if idx > lindex {
lindex = idx
}
}
return lindex
}
// EnsureNode is used to upsert node registration or modification.
func (s *StateStore) EnsureNode(idx uint64, node *structs.Node) error {
tx := s.db.Txn(true)

View File

@ -143,10 +143,13 @@ func TestStateStore_DeleteNode(t *testing.T) {
t.Fatalf("err: %s", err)
}
// The node is now gone
// The node is now gone and the index was updated
if n, err := s.GetNode("node1"); err != nil || n != nil {
t.Fatalf("bad: %#v (err: %#v)", node, err)
}
if idx := s.maxIndex("nodes"); idx != 2 {
t.Fatalf("bad index: %d", idx)
}
}
func TestStateStore_EnsureService_NodeServices(t *testing.T) {
@ -262,9 +265,12 @@ func TestStateStore_DeleteNodeService(t *testing.T) {
t.Fatalf("err: %s", err)
}
// The service doesn't exist.
// The service doesn't exist and the index was updated
ns, err = s.NodeServices("node1")
if err != nil || ns == nil || len(ns.Services) != 0 {
t.Fatalf("bad: %#v (err: %#v)", ns, err)
}
if idx := s.maxIndex("services"); idx != 3 {
t.Fatalf("bad index: %d", idx)
}
}