merge_logger_test.go 1.6 KB

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