-
Notifications
You must be signed in to change notification settings - Fork 3
/
resolver_test.go
70 lines (56 loc) · 1.57 KB
/
resolver_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
package cloudmap
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/service/servicediscovery"
"google.golang.org/grpc/grpclog"
grpcresolver "google.golang.org/grpc/resolver"
"google.golang.org/grpc/serviceconfig"
"testing"
"time"
)
type mockCC struct{}
func (m mockCC) UpdateState(state grpcresolver.State) error { return nil }
func (m mockCC) ReportError(err error) {}
func (m mockCC) NewAddress(addresses []grpcresolver.Address) {}
func (m mockCC) NewServiceConfig(serviceConfig string) {}
func (m mockCC) ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult {
return nil
}
type mockDiscovery struct{}
func (m mockDiscovery) DiscoverInstances(input *servicediscovery.DiscoverInstancesInput) (*servicediscovery.DiscoverInstancesOutput, error) {
time.Sleep(1 * time.Second)
fmt.Println("DiscoverInstances called")
return &servicediscovery.DiscoverInstancesOutput{
Instances: make([]*servicediscovery.HttpInstanceSummary, 0),
}, nil
}
func Test_resolver(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
r := &resolver{
logger: grpclog.Component("test"),
cc: mockCC{},
sd: mockDiscovery{},
ctx: ctx,
cancel: cancel,
ticker: time.NewTicker(10 * time.Second),
resolveCmd: make(chan struct{}, 1),
}
r.wg.Add(1)
go r.watcher()
timeout := time.After(100 * time.Millisecond)
done := make(chan bool)
go func() {
for i := 0; i < 10; i++ {
r.ResolveNow(grpcresolver.ResolveNowOptions{})
}
done <- true
}()
select {
case <-timeout:
t.Error("timeout")
case <-done:
t.Log("done")
}
r.Close()
}