github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/util/election/tracker_test.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package election 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 type testOperation struct { 23 id int64 24 val bool 25 } 26 27 func TestMasterTracker(t *testing.T) { 28 var tests = []struct { 29 ids []int64 30 ops []testOperation 31 count int 32 held []int64 33 str string 34 }{ 35 { 36 ids: []int64{1, 2, 3}, 37 ops: []testOperation{{1, true}}, 38 count: 1, 39 held: []int64{1}, 40 str: "1 . .", 41 }, 42 { 43 ids: []int64{1, 20000, 30000}, 44 ops: []testOperation{{30000, true}}, 45 count: 1, 46 held: []int64{30000}, 47 str: ". ..... 30000", 48 }, 49 { 50 ids: []int64{1, 2, 3}, 51 ops: []testOperation{ 52 {1, true}, 53 {2, true}, 54 {3, true}, 55 {1, false}, 56 }, 57 count: 2, 58 held: []int64{2, 3}, 59 str: ". 2 3", 60 }, 61 { 62 ids: []int64{}, 63 ops: []testOperation{ 64 {1, true}, 65 {2, true}, 66 {3, true}, 67 {1, false}, 68 }, 69 count: 2, 70 held: []int64{2, 3}, 71 str: ". 2 3", 72 }, 73 { 74 ids: []int64{1, 2, 3}, 75 ops: []testOperation{ 76 {1, true}, 77 {1, true}, // error: already true 78 }, 79 count: 1, // count still accurate though 80 held: []int64{1}, 81 str: "1 . .", 82 }, 83 { 84 ids: []int64{1, 2, 3}, 85 ops: []testOperation{ 86 {1, false}, // error: already false 87 }, 88 count: 0, // count still accurate though 89 held: []int64{}, 90 str: ". . .", 91 }, 92 } 93 94 for _, test := range tests { 95 mt := NewMasterTracker(test.ids, nil) 96 for _, op := range test.ops { 97 mt.Set(op.id, op.val) 98 } 99 if got := mt.Count(); got != test.count { 100 t.Errorf("MasterTracker.Count(%+v)=%d; want %d", test.ops, got, test.count) 101 } 102 if got := mt.Held(); !reflect.DeepEqual(got, test.held) { 103 t.Errorf("MasterTracker.Held(%+v)=%v; want %v", test.ops, got, test.held) 104 } 105 if got := mt.String(); got != test.str { 106 t.Errorf("MasterTracker.String(%+v)=%q; want %q", test.ops, got, test.str) 107 } 108 } 109 }