merge_logger_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 The etcd Authors
  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. package logutil
  15. import (
  16. "fmt"
  17. "strings"
  18. "testing"
  19. "time"
  20. "github.com/coreos/pkg/capnslog"
  21. )
  22. var (
  23. testLogger = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "logutil")
  24. )
  25. func TestMergeLogger(t *testing.T) {
  26. var (
  27. txt = "hello"
  28. repeatN = 6
  29. duration = 2049843762 * time.Nanosecond
  30. mg = NewMergeLogger(testLogger)
  31. )
  32. // overwrite this for testing
  33. defaultMergePeriod = time.Minute
  34. for i := 0; i < repeatN; i++ {
  35. mg.MergeError(txt)
  36. if i == 0 {
  37. time.Sleep(duration)
  38. }
  39. }
  40. if len(mg.statusm) != 1 {
  41. t.Errorf("got = %d, want = %d", len(mg.statusm), 1)
  42. }
  43. var l line
  44. for k := range mg.statusm {
  45. l = k
  46. break
  47. }
  48. if l.level != capnslog.ERROR {
  49. t.Errorf("got = %v, want = %v", l.level, capnslog.DEBUG)
  50. }
  51. if l.str != txt {
  52. t.Errorf("got = %s, want = %s", l.str, txt)
  53. }
  54. if mg.statusm[l].count != repeatN-1 {
  55. t.Errorf("got = %d, want = %d", mg.statusm[l].count, repeatN-1)
  56. }
  57. sum := mg.statusm[l].summary(time.Now())
  58. pre := fmt.Sprintf("[merged %d repeated lines in ", repeatN-1)
  59. if !strings.HasPrefix(sum, pre) {
  60. t.Errorf("got = %s, want = %s...", sum, pre)
  61. }
  62. }