consul/state: test for index modification during deletes
This commit is contained in:
parent
8ae69b6878
commit
0a000f63a3
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue