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 15package metrics 16 17import ( 18 "testing" 19 "time" 20) 21 22func TestEnd(t *testing.T) { 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 29 et := &EventTracer{} 30 et.push(&event{ 31 desc: "test", 32 name: "test", 33 start: startTime, 34 }) 35 36 perf := et.End() 37 if perf.GetRealTime() != uint64(dur.Nanoseconds()) { 38 t.Errorf("got %d, want %d nanoseconds for event duration", perf.GetRealTime(), dur.Nanoseconds()) 39 } 40} 41 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} 64