vitess.io/vitess@v0.16.2/go/vt/vtorc/collection/collection_test.go (about) 1 /* 2 Copyright 2017 Simon J Mudd 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package collection 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 var randomString = []string{ 25 "RANDOM_STRING", 26 "SOME_OTHER_STRING", 27 } 28 29 // some random base timestamp 30 var ts = time.Date(2016, 12, 27, 13, 36, 40, 0, time.Local) 31 32 // TestCreateOrReturn tests the creation of a named Collection 33 func TestCreateOrReturnCollection(t *testing.T) { 34 name := randomString[0] 35 // check we get the same reference with a single name 36 c1 := CreateOrReturnCollection(name) 37 if c1 == nil { 38 // should not be empty 39 t.Errorf("TestCreateOrReturn: c1 == nil, name=%s", name) 40 } 41 c2 := CreateOrReturnCollection(name) 42 if c2 == nil || c2 != c1 { 43 t.Errorf("TestCreateOrReturn: c2 == nil || c2 != c1") 44 // should not be empty, or different to c1 45 } 46 47 name = randomString[1] 48 // check we get a new reference and it's different to what we had before 49 c3 := CreateOrReturnCollection(name) 50 if c3 == nil || c3 == c1 { 51 // should not be empty, or same as c1 52 t.Errorf("TestCreateOrReturn: c3 == nil || c3 == c1") 53 } 54 c4 := CreateOrReturnCollection(name) 55 // check our reference matches c3 but not c2/c1 56 if c4 == nil || c4 != c3 || c4 == c2 { 57 t.Errorf("TestCreateOrReturn: c3 == nil || c4 != c3 || c4 == c2") 58 } 59 } 60 61 // TestExpirePeriod checks that the set expire period is returned 62 func TestExpirePeriod(t *testing.T) { 63 oneSecond := time.Second 64 twoSeconds := 2 * oneSecond 65 66 // create a new collection 67 c := &Collection{} 68 69 // check if we change it we get back the value we provided 70 c.SetExpirePeriod(oneSecond) 71 if c.ExpirePeriod() != oneSecond { 72 t.Errorf("TestExpirePeriod: did not get back oneSecond") 73 } 74 75 // change the period and check again 76 c.SetExpirePeriod(twoSeconds) 77 if c.ExpirePeriod() != twoSeconds { 78 t.Errorf("TestExpirePeriod: did not get back twoSeconds") 79 } 80 } 81 82 // dummy structure for testing 83 type testMetric struct { 84 } 85 86 func (tm *testMetric) When() time.Time { 87 return ts 88 } 89 90 // check that Append() works as expected 91 func TestAppend(t *testing.T) { 92 c := &Collection{} 93 94 if len(c.Metrics()) != 0 { 95 t.Errorf("TestAppend: len(Metrics) = %d, expecting %d", len(c.Metrics()), 0) 96 } 97 for _, v := range []int{1, 2, 3} { 98 tm := &testMetric{} 99 _ = c.Append(tm) 100 if len(c.Metrics()) != v { 101 t.Errorf("TestExpirePeriod: len(Metrics) = %d, expecting %d", len(c.Metrics()), v) 102 } 103 } 104 }