logreduction_test.go 1.9 KB

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