From 96c5c9de2bda0591c5f887cb036b420bdf91d9b7 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Mon, 19 Oct 2015 17:41:50 -0700 Subject: [PATCH] Gets rid of unique constraint on sessions index in session_checks. --- consul/state/schema.go | 2 +- consul/state/state_store_test.go | 51 ++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/consul/state/schema.go b/consul/state/schema.go index 4f10a195a..ac58f1538 100644 --- a/consul/state/schema.go +++ b/consul/state/schema.go @@ -318,7 +318,7 @@ func sessionChecksTableSchema() *memdb.TableSchema { "session": &memdb.IndexSchema{ Name: "session", AllowMissing: false, - Unique: true, + Unique: false, Indexer: &memdb.UUIDFieldIndex{ Field: "Session", }, diff --git a/consul/state/state_store_test.go b/consul/state/state_store_test.go index ff7a5b726..7b76ea2c1 100644 --- a/consul/state/state_store_test.go +++ b/consul/state/state_store_test.go @@ -3480,24 +3480,51 @@ func TestStateStore_SessionCreate_SessionGet(t *testing.T) { t.Fatalf("err: %s", err) } + // Register a session against two checks. + testRegisterCheck(t, s, 5, "node1", "", "check2", structs.HealthPassing) + sess2 := &structs.Session{ + ID: testUUID(), + Node: "node1", + Checks: []string{"check1", "check2"}, + } + if err := s.SessionCreate(6, sess2); err != nil { + t.Fatalf("err: %s", err) + } + tx := s.db.Txn(false) defer tx.Abort() // Check mappings were inserted - check, err := tx.First("session_checks", "session", sess.ID) + { + check, err := tx.First("session_checks", "session", sess.ID) + if err != nil { + t.Fatalf("err: %s", err) + } + if check == nil { + t.Fatalf("missing session check") + } + expectCheck := &sessionCheck{ + Node: "node1", + CheckID: "check1", + Session: sess.ID, + } + if actual := check.(*sessionCheck); !reflect.DeepEqual(actual, expectCheck) { + t.Fatalf("expected %#v, got: %#v", expectCheck, actual) + } + } + checks, err := tx.Get("session_checks", "session", sess2.ID) if err != nil { t.Fatalf("err: %s", err) } - if check == nil { - t.Fatalf("missing session check") - } - expectCheck := &sessionCheck{ - Node: "node1", - CheckID: "check1", - Session: sess.ID, - } - if actual := check.(*sessionCheck); !reflect.DeepEqual(actual, expectCheck) { - t.Fatalf("expected %#v, got: %#v", expectCheck, actual) + for i, check := 0, checks.Next(); check != nil; i, check = i+1, checks.Next() { + expectCheck := &sessionCheck{ + Node: "node1", + CheckID: fmt.Sprintf("check%d", i+1), + Session: sess2.ID, + } + if actual := check.(*sessionCheck); !reflect.DeepEqual(actual, expectCheck) { + t.Fatalf("expected %#v, got: %#v", expectCheck, actual) + } } // Pulling a nonexistent session gives the table index. @@ -3508,7 +3535,7 @@ func TestStateStore_SessionCreate_SessionGet(t *testing.T) { if session != nil { t.Fatalf("expected not to get a session: %v", session) } - if idx != 5 { + if idx != 6 { t.Fatalf("bad index: %d", idx) } }