github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/base/cluster_id_test.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package base_test 12 13 import ( 14 "context" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/base" 18 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 19 "github.com/cockroachdb/cockroach/pkg/util/uuid" 20 ) 21 22 func TestClusterIDContainerEmpty(t *testing.T) { 23 defer leaktest.AfterTest(t)() 24 c := &base.ClusterIDContainer{} 25 26 if val := c.Get(); val != uuid.Nil { 27 t.Errorf("initial value should be uuid.Nil, not %s", val) 28 } 29 if str := c.String(); str != "?" { 30 t.Errorf("initial string should be ?, not %s", str) 31 } 32 } 33 34 func TestClusterIDContainerSet(t *testing.T) { 35 defer leaktest.AfterTest(t)() 36 c := &base.ClusterIDContainer{} 37 u := uuid.MakeV4() 38 39 for i := 0; i < 2; i++ { 40 c.Set(context.Background(), u) 41 if val := c.Get(); val != u { 42 t.Errorf("value should be %s, not %s", u, val) 43 } 44 if str := c.String(); str != u.String() { 45 t.Errorf("string should be %s, not %s", u.String(), str) 46 } 47 } 48 } 49 50 func TestClusterIDContainerReset(t *testing.T) { 51 defer leaktest.AfterTest(t)() 52 c := &base.ClusterIDContainer{} 53 uuid1 := uuid.MakeV4() 54 uuid2 := uuid.MakeV4() 55 56 c.Set(context.Background(), uuid1) 57 c.Reset(uuid2) 58 if val := c.Get(); val != uuid2 { 59 t.Errorf("value should be %s, not %s", uuid2, val) 60 } 61 if str := c.String(); str != uuid2.String() { 62 t.Errorf("string should be %s, not %s", uuid2.String(), str) 63 } 64 }