Patrice Arruda | 958b89c | 2020-07-13 18:21:14 +0000 | [diff] [blame] | 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 | |
| 15 | package metrics |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | "time" |
Patrice Arruda | 958b89c | 2020-07-13 18:21:14 +0000 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func TestEnd(t *testing.T) { |
Patrice Arruda | 958b89c | 2020-07-13 18:21:14 +0000 | [diff] [blame] | 23 | 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 Arruda | 8a44a37 | 2020-12-14 20:55:23 +0000 | [diff] [blame] | 29 | et := &EventTracer{} |
| 30 | et.push(&event{ |
Patrice Arruda | 958b89c | 2020-07-13 18:21:14 +0000 | [diff] [blame] | 31 | desc: "test", |
| 32 | name: "test", |
| 33 | start: startTime, |
| 34 | }) |
| 35 | |
Liz Kammer | f2a80c6 | 2022-10-21 10:42:35 -0400 | [diff] [blame] | 36 | perf := et.End() |
Patrice Arruda | 958b89c | 2020-07-13 18:21:14 +0000 | [diff] [blame] | 37 | if perf.GetRealTime() != uint64(dur.Nanoseconds()) { |
| 38 | t.Errorf("got %d, want %d nanoseconds for event duration", perf.GetRealTime(), dur.Nanoseconds()) |
| 39 | } |
| 40 | } |
Liz Kammer | f2a80c6 | 2022-10-21 10:42:35 -0400 | [diff] [blame] | 41 | |
| 42 | func 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 | } |