-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_expectation.go
96 lines (71 loc) · 1.71 KB
/
server_expectation.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
package grpcmock
import (
"sync"
"github.com/spf13/afero"
"go.nhat.io/wait"
"google.golang.org/grpc/codes"
xmatcher "go.nhat.io/grpcmock/matcher"
"go.nhat.io/grpcmock/service"
)
type baseExpectation struct {
locker sync.Locker
fs afero.Fs
waiter wait.Waiter
serviceDesc *service.Method
// requestHeader is a list of expected headers of the given request.
requestHeader xmatcher.HeaderMatcher
// requestPayload is the expected parameters of the given request.
requestPayload *xmatcher.PayloadMatcher
// statusCode is the response code when the request is handled.
statusCode codes.Code //nolint: structcheck
// statusMessage is the error message in case of failure.
statusMessage string //nolint: structcheck
fulfilledTimes uint
repeatTimes uint
}
func (e *baseExpectation) lock() {
e.locker.Lock()
}
func (e *baseExpectation) unlock() {
e.locker.Unlock()
}
// nolint: unused
func (e *baseExpectation) withFs(fs afero.Fs) {
e.lock()
defer e.unlock()
e.fs = fs
}
func (e *baseExpectation) withTimes(t uint) {
e.lock()
defer e.unlock()
e.repeatTimes = t
}
func (e *baseExpectation) ServiceMethod() service.Method {
return *e.serviceDesc
}
func (e *baseExpectation) HeaderMatcher() xmatcher.HeaderMatcher {
e.lock()
defer e.unlock()
return e.requestHeader
}
func (e *baseExpectation) PayloadMatcher() *xmatcher.PayloadMatcher {
return e.requestPayload
}
func (e *baseExpectation) RemainTimes() uint {
e.lock()
defer e.unlock()
return e.repeatTimes
}
func (e *baseExpectation) Fulfilled() {
e.lock()
defer e.unlock()
if e.repeatTimes > 0 {
e.repeatTimes--
}
e.fulfilledTimes++
}
func (e *baseExpectation) FulfilledTimes() uint {
e.lock()
defer e.unlock()
return e.fulfilledTimes
}