This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry_test.go
157 lines (132 loc) · 3.37 KB
/
registry_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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package mrpc
import (
"context"
"fmt"
"io"
"testing"
"github.com/cdr/grip"
"github.com/stretchr/testify/suite"
"github.com/deciduosity/mrpc/mongowire"
)
type RegistrySuite struct {
registry *OperationRegistry
handler HandlerFunc
suite.Suite
}
func TestRegistrySuite(t *testing.T) {
suite.Run(t, new(RegistrySuite))
}
func (s *RegistrySuite) SetupSuite() {
var callCount int
s.handler = func(ctx context.Context, w io.Writer, m mongowire.Message) {
callCount++
grip.Infof("test handler, call %d", callCount)
}
}
func (s *RegistrySuite) SetupTest() {
s.registry = &OperationRegistry{
ops: map[mongowire.OpScope]HandlerFunc{},
}
}
func (s *RegistrySuite) TestOperationCanOnlyBeRegisteredOnce() {
op := mongowire.OpScope{
Type: mongowire.OP_COMMAND,
Context: "foo",
Command: "bar",
}
s.Len(s.registry.ops, 0)
s.NoError(s.registry.Add(op, s.handler))
s.Len(s.registry.ops, 1)
for i := 0; i < 100; i++ {
s.Error(s.registry.Add(op, s.handler))
s.Len(s.registry.ops, 1)
}
// test with the same content, even if it's a different object
op2 := mongowire.OpScope{
Type: mongowire.OP_COMMAND,
Context: "foo",
Command: "bar",
}
s.Error(s.registry.Add(op2, s.handler))
s.Len(s.registry.ops, 1)
// add something new and make sure that it is added
op3 := mongowire.OpScope{
Type: mongowire.OP_COMMAND,
Context: "bar",
Command: "bar",
}
s.NoError(s.registry.Add(op3, s.handler))
s.Len(s.registry.ops, 2)
}
func (s *RegistrySuite) TestInvalidScopeIsNotAdded() {
op := mongowire.OpScope{}
s.Error(s.registry.Add(op, s.handler))
op.Type = mongowire.OP_KILL_CURSORS
op.Context = "foo"
s.Error(s.registry.Add(op, s.handler))
s.Len(s.registry.ops, 0)
}
func (s *RegistrySuite) TestOpsMustHaveValidHandlers() {
op := mongowire.OpScope{
Type: mongowire.OP_COMMAND,
Context: "foo",
Command: "bar",
}
s.Len(s.registry.ops, 0)
s.NoError(op.Validate())
s.Error(s.registry.Add(op, nil))
s.Len(s.registry.ops, 0)
}
func (s *RegistrySuite) TestUndefinedOperationsRetreiveNilResults() {
op := mongowire.OpScope{}
s.Len(s.registry.ops, 0)
h, ok := s.registry.Get(&op)
s.False(ok)
s.Nil(h)
}
func (s *RegistrySuite) TestOpsAreRetreivable() {
op := mongowire.OpScope{
Type: mongowire.OP_COMMAND,
Context: "foo",
Command: "bar",
}
s.NoError(s.registry.Add(op, s.handler))
s.Len(s.registry.ops, 1)
h, ok := s.registry.Get(&op)
s.True(ok)
s.NotNil(h)
s.Equal(fmt.Sprint(h), fmt.Sprint(s.handler))
}
func (s *RegistrySuite) TestOpsWithContextFallBackToNoContext() {
op := mongowire.OpScope{
Type: mongowire.OP_COMMAND,
Context: "foo",
Command: "bar",
}
noContextOp := op
noContextOp.Context = ""
s.NoError(s.registry.Add(noContextOp, s.handler))
h, ok := s.registry.Get(&op)
s.True(ok)
s.NotNil(h)
s.Equal(fmt.Sprint(s.handler), fmt.Sprint(h))
}
func (s *RegistrySuite) TestOpsWithoutContextFallBackToNoContext() {
op := mongowire.OpScope{
Type: mongowire.OP_COMMAND,
Context: "foo",
Command: "bar",
}
noContextOp := op
noContextOp.Context = ""
s.NoError(s.registry.Add(op, s.handler))
h, ok := s.registry.Get(&noContextOp)
s.False(ok)
s.Nil(h)
var noContextHandler HandlerFunc = func(ctx context.Context, w io.Writer, m mongowire.Message) {}
s.NoError(s.registry.Add(noContextOp, noContextHandler))
h, ok = s.registry.Get(&noContextOp)
s.True(ok)
s.NotNil(h)
s.Equal(fmt.Sprint(noContextHandler), fmt.Sprint(h))
}