This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
/
testing.go
121 lines (100 loc) · 3.25 KB
/
testing.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package adaptor
import (
"sync"
"testing"
"time"
"github.com/compose/transporter/client"
)
var (
_ Adaptor = &Mock{}
_ Adaptor = &UnsupportedMock{}
)
// Mock can be used for mocking tests that need no functional client interfaces.
type Mock struct {
BaseConfig
}
// Client satisfies the V2 interface for providing a client.Client.
func (m *Mock) Client() (client.Client, error) {
return &client.Mock{}, nil
}
// Reader satisfies the V2 interface for providing a client.Reader.
func (m *Mock) Reader() (client.Reader, error) {
return &client.MockReader{}, nil
}
// Writer satisfies the V2 interface for providing a client.Writer.
func (m *Mock) Writer(chan struct{}, *sync.WaitGroup) (client.Writer, error) {
return &client.MockWriter{}, nil
}
// MockClientErr can be used to to mock client.Client() errors.
type MockClientErr struct {
BaseConfig
}
// Client satisfies the client.Client interface.
func (m *MockClientErr) Client() (client.Client, error) {
return &client.MockErr{}, nil
}
// Reader satisfies client.Reader.
func (m *MockClientErr) Reader() (client.Reader, error) {
return &client.MockReader{}, nil
}
// Writer satisfies client.Writer.
func (m *MockClientErr) Writer(chan struct{}, *sync.WaitGroup) (client.Writer, error) {
return &client.MockWriter{}, nil
}
// MockWriterErr can be used to to mock client.Write() errors.
type MockWriterErr struct {
BaseConfig
}
// Client satisfies the client.Client interface.
func (m *MockWriterErr) Client() (client.Client, error) {
return &client.Mock{}, nil
}
// Reader satisfies the V2 interface for providing a client.Reader.
func (m *MockWriterErr) Reader() (client.Reader, error) {
return &client.MockReader{}, nil
}
// Writer satisfies the V2 interface for providing a client.Writer.
func (m *MockWriterErr) Writer(chan struct{}, *sync.WaitGroup) (client.Writer, error) {
return &client.MockErrWriter{}, nil
}
// UnsupportedMock can be used for mocking tests that need no functional client interfaces.
type UnsupportedMock struct {
BaseConfig
}
// Client satisfies the V2 interface for providing a client.Client.
func (m *UnsupportedMock) Client() (client.Client, error) {
return nil, ErrFuncNotSupported{"unsupported", "Client()"}
}
// Reader satisfies the V2 interface for providing a client.Reader.
func (m *UnsupportedMock) Reader() (client.Reader, error) {
return nil, ErrFuncNotSupported{"unsupported", "Reader()"}
}
// Writer satisfies the V2 interface for providing a client.Writer.
func (m *UnsupportedMock) Writer(chan struct{}, *sync.WaitGroup) (client.Writer, error) {
return nil, ErrFuncNotSupported{"unsupported", "Writer()"}
}
// MockConfirmWrites is a helper function for tests needing a confirms chan.
func MockConfirmWrites() (chan struct{}, func() bool) {
confirms := make(chan struct{})
done := make(chan struct{})
var confirmed bool
go func() {
for {
select {
case <-confirms:
confirmed = true
case <-done:
return
}
}
}()
return confirms, func() bool { close(done); return confirmed }
}
// VerifyWriteConfirmed is a helper function to be used in conjunction with
// MockConfirmWrites.
func VerifyWriteConfirmed(f func() bool, t *testing.T) {
time.Sleep(100 * time.Millisecond)
if confirmed := f(); !confirmed {
t.Errorf("writes were not confirmed but should have been")
}
}