event_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2020 Google Inc. All Rights Reserved.
  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 metrics
  15. import (
  16. "testing"
  17. "time"
  18. )
  19. func TestEnd(t *testing.T) {
  20. startTime := time.Date(2020, time.July, 13, 13, 0, 0, 0, time.UTC)
  21. dur := time.Nanosecond * 10
  22. initialNow := _now
  23. _now = func() time.Time { return startTime.Add(dur) }
  24. defer func() { _now = initialNow }()
  25. et := &EventTracer{}
  26. et.push(&event{
  27. desc: "test",
  28. name: "test",
  29. start: startTime,
  30. })
  31. perf := et.End()
  32. if perf.GetRealTime() != uint64(dur.Nanoseconds()) {
  33. t.Errorf("got %d, want %d nanoseconds for event duration", perf.GetRealTime(), dur.Nanoseconds())
  34. }
  35. }
  36. func TestEndWithError(t *testing.T) {
  37. startTime := time.Date(2020, time.July, 13, 13, 0, 0, 0, time.UTC)
  38. dur := time.Nanosecond * 10
  39. initialNow := _now
  40. _now = func() time.Time { return startTime.Add(dur) }
  41. defer func() { _now = initialNow }()
  42. err := "foobar"
  43. et := &EventTracer{}
  44. et.push(&event{
  45. desc: "test",
  46. name: "test",
  47. start: startTime,
  48. nonZeroExitCode: true,
  49. errorMsg: &err,
  50. })
  51. perf := et.End()
  52. if msg := perf.GetErrorMessage(); msg != err {
  53. t.Errorf("got %q, want %q for even error message", msg, err)
  54. }
  55. }