-
Notifications
You must be signed in to change notification settings - Fork 5
/
discovery_test.go
148 lines (125 loc) · 3.17 KB
/
discovery_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
package discovery
import (
"log"
"testing"
"github.com/peak-ai/ais-service-discovery-go/pkg/types"
"github.com/stretchr/testify/assert"
)
type mockLocator struct{}
func (l *mockLocator) Discover(service *types.Signature) (*types.Service, error) {
return &types.Service{}, nil
}
type mockFunctionAdapter struct{}
func (fa *mockFunctionAdapter) CallWithOpts(
service *types.Service,
request types.Request,
opts types.Options,
) (*types.Response, error) {
return nil, nil
}
type mockQueueAdapter struct {
messages chan *types.Response
}
func (qa *mockQueueAdapter) QueueWithOpts(
service *types.Service,
request types.Request,
opts types.Options,
) (string, error) {
response := &types.Response{
Body: request.Body,
}
go func() { qa.messages <- response }()
return "abc123", nil
}
func (qa *mockQueueAdapter) ListenWithOpts(
service *types.Service,
options types.Options,
) (<-chan *types.Response, error) {
return qa.messages, nil
}
type mockAutomateAdapter struct{}
func (aa *mockAutomateAdapter) ExecuteWithOpts(
service *types.Service,
request types.Request,
opts types.Options,
) (*types.Response, error) {
return &types.Response{}, nil
}
type mockPubsubAdapter struct{}
func (pa *mockPubsubAdapter) PublishWithOpts(
service *types.Service,
request types.Request,
opts types.Options,
) error {
return nil
}
func (pa *mockPubsubAdapter) SubscribeWithOpts(
service *types.Service,
opts types.Options,
) (<-chan *types.Response, error) {
channel := make(chan *types.Response)
return channel, nil
}
type mockLogger struct{}
func (ml *mockLogger) Log(service *types.Service, message string) {}
type mockTracer struct{}
func (mt *mockTracer) Trace(service *types.Service) {}
func TestCanCallRequest(t *testing.T) {
discover := NewDiscovery(
SetLocator(&mockLocator{}),
SetAutomate(&mockAutomateAdapter{}),
SetLogger(&mockLogger{}),
SetTracer(&mockTracer{}),
SetFunction(&mockFunctionAdapter{}),
)
discover.Request("service->handler", types.Request{
Body: []byte(""),
})
}
func TestCanCallAutomate(t *testing.T) {
discover := NewDiscovery(
SetLocator(&mockLocator{}),
SetAutomate(&mockAutomateAdapter{}),
SetLogger(&mockLogger{}),
SetTracer(&mockTracer{}),
)
_, err := discover.Automate("service->handler", types.Request{
Body: []byte(""),
})
assert.NoError(t, err)
}
func TestCanCallPubSub(t *testing.T) {
discover := NewDiscovery(
SetLocator(&mockLocator{}),
SetPubsub(&mockPubsubAdapter{}),
SetLogger(&mockLogger{}),
SetTracer(&mockTracer{}),
)
err := discover.Publish("my-service->my-topic", types.Request{
Body: []byte(""),
})
assert.NoError(t, err)
}
func TestCanCallQueue(t *testing.T) {
val := []byte("{}")
response := &types.Response{
Body: val,
}
discover := NewDiscovery(
SetLocator(&mockLocator{}),
SetQueue(&mockQueueAdapter{
messages: make(chan *types.Response),
}),
SetLogger(&mockLogger{}),
SetTracer(&mockTracer{}),
)
token, err := discover.Queue("my-service->my-queue", types.Request{
Body: val,
})
assert.NoError(t, err)
assert.Equal(t, token, "abc123")
messages, err := discover.Listen("my-service->my-queue")
log.Println(messages)
assert.NoError(t, err)
assert.Equal(t, <-messages, response)
}