-
Notifications
You must be signed in to change notification settings - Fork 3
/
event_helper_test.go
94 lines (70 loc) · 2.36 KB
/
event_helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package rkquery
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewEventHelper(t *testing.T) {
helper := NewEventHelper(NewEventFactory())
assert.NotNil(t, helper)
assert.NotNil(t, helper.Factory)
}
func TestNewEventHelper_WithEmptyFactory(t *testing.T) {
helper := NewEventHelper(nil)
assert.NotNil(t, helper.Factory)
}
func TestEventHelper_Start(t *testing.T) {
helper := NewEventHelper(NewEventFactory())
event := helper.Start("")
assert.NotNil(t, event)
assert.NotZero(t, event.GetStartTime().Unix())
}
func TestEventHelper_Finish(t *testing.T) {
helper := NewEventHelper(NewEventFactory())
event := helper.Start("")
helper.Finish(event)
assert.NotZero(t, event.GetStartTime().Unix())
assert.NotZero(t, event.GetEndTime().Unix())
}
func TestEventHelper_FinishWithCond_WithSuccess(t *testing.T) {
helper := NewEventHelper(NewEventFactory())
event := helper.Start("")
helper.FinishWithCond(event, true)
assert.NotZero(t, event.GetStartTime().Unix())
assert.NotZero(t, event.GetEndTime().Unix())
assert.Equal(t, int64(1), event.GetCounter("success"))
}
func TestEventHelper_FinishWithCond_WithFailure(t *testing.T) {
helper := NewEventHelper(NewEventFactory())
event := helper.Start("")
helper.FinishWithCond(event, false)
assert.NotZero(t, event.GetStartTime().Unix())
assert.NotZero(t, event.GetEndTime().Unix())
assert.Equal(t, int64(1), event.GetCounter("failure"))
}
func TestEventHelper_FinishWithError_WithoutError(t *testing.T) {
helper := NewEventHelper(NewEventFactory())
event := helper.Start("")
helper.FinishWithError(event, nil)
assert.NotZero(t, event.GetStartTime().Unix())
assert.NotZero(t, event.GetEndTime().Unix())
assert.Equal(t, int64(1), event.GetCounter("success"))
assert.Zero(t, event.GetErrCount(errors.New("")))
}
func TestEventHelper_FinishWithError_WithError(t *testing.T) {
helper := NewEventHelper(NewEventFactory())
event := helper.Start("")
helper.FinishWithError(event, &MyErr{})
assert.NotZero(t, event.GetStartTime().Unix())
assert.NotZero(t, event.GetEndTime().Unix())
assert.Equal(t, int64(1), event.GetCounter("failure"))
assert.NotZero(t, event.GetErrCount(&MyErr{}))
}
type MyErr struct{}
func (err MyErr) Error() string {
return ""
}