1/* 2Copyright 2018 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package logreduction 18 19import ( 20 "testing" 21 "time" 22) 23 24var time0 = time.Unix(1000, 0) 25var time1 = time.Unix(1001, 0) 26var time2 = time.Unix(1012, 0) 27var identicalErrorDelay = 10 * time.Second 28var testCount = 0 29 30const ( 31 mesg1 = "This is a message" 32 mesg2 = "This is not a message" 33 id1 = "Container1" 34 id2 = "Container2" 35) 36 37func checkThat(t *testing.T, r *LogReduction, m, id string) { 38 testCount++ 39 if !r.ShouldMessageBePrinted(m, id) { 40 t.Errorf("Case %d failed (%s/%s should be printed)", testCount, m, id) 41 } 42} 43 44func checkThatNot(t *testing.T, r *LogReduction, m, id string) { 45 testCount++ 46 if r.ShouldMessageBePrinted(m, id) { 47 t.Errorf("Case %d failed (%s/%s should not be printed)", testCount, m, id) 48 } 49} 50 51func TestLogReduction(t *testing.T) { 52 var timeToReturn = time0 53 nowfunc = func() time.Time { return timeToReturn } 54 r := NewLogReduction(identicalErrorDelay) 55 checkThat(t, r, mesg1, id1) // 1 56 checkThatNot(t, r, mesg1, id1) // 2 57 checkThat(t, r, mesg1, id2) // 3 58 checkThatNot(t, r, mesg1, id1) // 4 59 timeToReturn = time1 60 checkThatNot(t, r, mesg1, id1) // 5 61 timeToReturn = time2 62 checkThat(t, r, mesg1, id1) // 6 63 checkThatNot(t, r, mesg1, id1) // 7 64 checkThat(t, r, mesg2, id1) // 8 65 checkThat(t, r, mesg1, id1) // 9 66 checkThat(t, r, mesg1, id2) // 10 67 r.ClearID(id1) 68 checkThat(t, r, mesg1, id1) // 11 69 checkThatNot(t, r, mesg1, id2) // 12 70} 71