-
Notifications
You must be signed in to change notification settings - Fork 39
/
mock_controller_test.go
91 lines (69 loc) · 1.51 KB
/
mock_controller_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
package minimock
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewController(t *testing.T) {
c := NewController(t)
assert.Equal(t, &safeTester{Tester: t}, c.Tester)
}
func TestController_RegisterMocker(t *testing.T) {
c := &Controller{}
c.RegisterMocker(nil)
assert.Len(t, c.mockers, 1)
}
type dummyMocker struct {
finishCounter int32
waitCounter int32
}
func (dm *dummyMocker) MinimockFinish() {
atomic.AddInt32(&dm.finishCounter, 1)
}
func (dm *dummyMocker) MinimockWait(time.Duration) {
atomic.AddInt32(&dm.waitCounter, 1)
}
func TestController_Finish(t *testing.T) {
dm := &dummyMocker{}
c := &Controller{
mockers: []Mocker{dm, dm},
}
c.Finish()
assert.Equal(t, int32(2), atomic.LoadInt32(&dm.finishCounter))
}
func TestController_Wait(t *testing.T) {
dm := &dummyMocker{}
c := &Controller{
mockers: []Mocker{dm, dm},
}
c.Wait(0)
assert.Equal(t, int32(2), atomic.LoadInt32(&dm.waitCounter))
}
func TestController_WaitConcurrent(t *testing.T) {
um1 := &unsafeMocker{}
um2 := &unsafeMocker{}
c := &Controller{
Tester: newSafeTester(&unsafeTester{}),
mockers: []Mocker{um1, um2},
}
um1.tester = c
um2.tester = c
c.Wait(0) //shouln't produce data races
}
type unsafeMocker struct {
Mocker
tester Tester
}
func (um *unsafeMocker) MinimockWait(time.Duration) {
um.tester.Fatal()
}
type unsafeTester struct {
Tester
finished bool
}
func (u *unsafeTester) Fatal(...interface{}) {
u.finished = true
}
func (u *unsafeTester) Helper() {
}