-
Notifications
You must be signed in to change notification settings - Fork 42
/
listener.go
57 lines (45 loc) · 1.36 KB
/
listener.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
package mock
import (
"net"
"github.com/stretchr/testify/mock"
)
// ListenerMock conforms to the net.Listener interface
type ListenerMock struct {
net.Listener
mock.Mock
}
func numberOfMockMethodCalls(mock mock.Mock, method string) int {
count := 0
for _, call := range mock.Calls {
if call.Method == method {
count++
}
}
return count
}
// Accept mocks the method of the same name on the net.Listener interface.
// It is a special mock method that normally blocks forever. When expected
// return values are set it will return those for the first call.
func (l *ListenerMock) Accept() (net.Conn, error) {
args := l.Called()
// block forever for calls that are not expected
if numberOfMockMethodCalls(l.Mock, "Accept") > 1 {
select {}
}
// check for nil because the mock package is unable type assert nil
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(net.Conn), args.Error(1)
}
// Close mocks the method of the same name on the net.Listener interface
func (l *ListenerMock) Close() error {
args := l.Called()
return args.Error(0)
}
// NewListener creates a net.Listener mock with an `Accept` method that returns
// the expectation values only on the first call, otherwise it blocks forever for
// all subsequent calls or if expected return values are not set.
func NewListener() *ListenerMock {
return new(ListenerMock)
}