blob: a80e7cff8e0895ef22520b4100034d37602a376b [file] [log] [blame]
Patrice Arruda958b89c2020-07-13 18:21:14 +00001// 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
15package metrics
16
17import (
18 "testing"
19 "time"
Patrice Arruda958b89c2020-07-13 18:21:14 +000020)
21
22func TestEnd(t *testing.T) {
Patrice Arruda958b89c2020-07-13 18:21:14 +000023 startTime := time.Date(2020, time.July, 13, 13, 0, 0, 0, time.UTC)
24 dur := time.Nanosecond * 10
25 initialNow := _now
26 _now = func() time.Time { return startTime.Add(dur) }
27 defer func() { _now = initialNow }()
28
Patrice Arruda8a44a372020-12-14 20:55:23 +000029 et := &EventTracer{}
30 et.push(&event{
Patrice Arruda958b89c2020-07-13 18:21:14 +000031 desc: "test",
32 name: "test",
33 start: startTime,
34 })
35
Liz Kammerf2a80c62022-10-21 10:42:35 -040036 perf := et.End()
Patrice Arruda958b89c2020-07-13 18:21:14 +000037 if perf.GetRealTime() != uint64(dur.Nanoseconds()) {
38 t.Errorf("got %d, want %d nanoseconds for event duration", perf.GetRealTime(), dur.Nanoseconds())
39 }
40}
Liz Kammerf2a80c62022-10-21 10:42:35 -040041
42func TestEndWithError(t *testing.T) {
43 startTime := time.Date(2020, time.July, 13, 13, 0, 0, 0, time.UTC)
44 dur := time.Nanosecond * 10
45 initialNow := _now
46 _now = func() time.Time { return startTime.Add(dur) }
47 defer func() { _now = initialNow }()
48
49 err := "foobar"
50 et := &EventTracer{}
51 et.push(&event{
52 desc: "test",
53 name: "test",
54 start: startTime,
55 nonZeroExitCode: true,
56 errorMsg: &err,
57 })
58
59 perf := et.End()
60 if msg := perf.GetErrorMessage(); msg != err {
61 t.Errorf("got %q, want %q for even error message", msg, err)
62 }
63}