1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/registry/watcher_test.go

150 lines
2.4 KiB
Go
Raw Normal View History

2019-01-15 18:50:37 +02:00
package registry
2016-05-01 22:30:14 +02:00
import (
2019-11-18 14:58:06 +02:00
"os"
2016-05-01 22:30:14 +02:00
"testing"
2019-11-18 14:58:06 +02:00
"time"
2016-05-01 22:30:14 +02:00
)
func TestWatcher(t *testing.T) {
if travis := os.Getenv("TRAVIS"); travis == "true" {
t.Skip()
}
2019-01-15 18:50:37 +02:00
testData := []*Service{
{
2016-05-01 22:30:14 +02:00
Name: "test1",
Version: "1.0.1",
2019-01-15 18:50:37 +02:00
Nodes: []*Node{
{
2016-05-01 22:30:14 +02:00
Id: "test1-1",
2019-07-08 09:01:42 +02:00
Address: "10.0.0.1:10001",
2016-05-01 22:30:14 +02:00
Metadata: map[string]string{
"foo": "bar",
},
},
},
},
{
2016-05-01 22:30:14 +02:00
Name: "test2",
Version: "1.0.2",
2019-01-15 18:50:37 +02:00
Nodes: []*Node{
{
2016-05-01 22:30:14 +02:00
Id: "test2-1",
2019-07-08 09:01:42 +02:00
Address: "10.0.0.2:10002",
2016-05-01 22:30:14 +02:00
Metadata: map[string]string{
"foo2": "bar2",
},
},
},
},
{
2016-05-01 22:30:14 +02:00
Name: "test3",
Version: "1.0.3",
2019-01-15 18:50:37 +02:00
Nodes: []*Node{
{
2016-05-01 22:30:14 +02:00
Id: "test3-1",
2019-07-08 09:01:42 +02:00
Address: "10.0.0.3:10003",
2016-05-01 22:30:14 +02:00
Metadata: map[string]string{
"foo3": "bar3",
},
},
},
},
}
2019-01-15 18:50:37 +02:00
testFn := func(service, s *Service) {
2016-05-01 23:05:21 +02:00
if s == nil {
t.Fatalf("Expected one result for %s got nil", service.Name)
2016-05-01 22:30:14 +02:00
}
2016-05-01 23:05:21 +02:00
if s.Name != service.Name {
t.Fatalf("Expected name %s got %s", service.Name, s.Name)
2016-05-01 22:30:14 +02:00
}
2016-05-01 23:05:21 +02:00
if s.Version != service.Version {
t.Fatalf("Expected version %s got %s", service.Version, s.Version)
2016-05-01 22:38:22 +02:00
}
2016-05-01 23:05:21 +02:00
if len(s.Nodes) != 1 {
t.Fatalf("Expected 1 node, got %d", len(s.Nodes))
2016-05-01 22:38:22 +02:00
}
2016-05-01 23:05:21 +02:00
node := s.Nodes[0]
2016-05-01 22:30:14 +02:00
if node.Id != service.Nodes[0].Id {
t.Fatalf("Expected node id %s got %s", service.Nodes[0].Id, node.Id)
}
if node.Address != service.Nodes[0].Address {
t.Fatalf("Expected node address %s got %s", service.Nodes[0].Address, node.Address)
}
}
2019-11-18 14:58:06 +02:00
travis := os.Getenv("TRAVIS")
var opts []Option
if travis == "true" {
opts = append(opts, Timeout(time.Millisecond*100))
}
2016-05-01 23:05:21 +02:00
// new registry
2019-11-18 14:58:06 +02:00
r := NewRegistry(opts...)
2016-05-01 23:05:21 +02:00
w, err := r.Watch()
if err != nil {
t.Fatal(err)
}
defer w.Stop()
2016-05-01 22:30:14 +02:00
for _, service := range testData {
2016-05-01 23:05:21 +02:00
// register service
if err := r.Register(service); err != nil {
2016-05-01 22:30:14 +02:00
t.Fatal(err)
}
2016-05-01 23:30:33 +02:00
for {
2016-05-01 23:05:21 +02:00
res, err := w.Next()
if err != nil {
t.Fatal(err)
}
2016-05-01 22:38:22 +02:00
2016-05-01 23:05:21 +02:00
if res.Service.Name != service.Name {
2016-05-01 23:30:33 +02:00
continue
2016-05-01 23:05:21 +02:00
}
2016-05-01 22:38:22 +02:00
2016-05-01 23:05:21 +02:00
if res.Action != "create" {
2016-05-01 23:30:33 +02:00
t.Fatalf("Expected create event got %s for %s", res.Action, res.Service.Name)
2016-05-01 22:38:22 +02:00
}
2016-05-01 23:05:21 +02:00
testFn(service, res.Service)
break
2016-05-01 22:30:14 +02:00
}
2016-05-01 23:05:21 +02:00
// deregister
if err := r.Deregister(service); err != nil {
t.Fatal(err)
2016-05-01 22:30:14 +02:00
}
2016-05-01 23:30:33 +02:00
for {
2016-05-01 23:05:21 +02:00
res, err := w.Next()
if err != nil {
t.Fatal(err)
}
2016-05-01 22:30:14 +02:00
2016-05-01 23:30:33 +02:00
if res.Service.Name != service.Name {
2016-05-01 23:05:21 +02:00
continue
}
2016-05-01 22:30:14 +02:00
2016-05-01 23:30:33 +02:00
if res.Action != "delete" {
continue
2016-05-01 23:05:21 +02:00
}
2016-05-01 22:30:14 +02:00
2016-05-01 23:05:21 +02:00
testFn(service, res.Service)
break
2016-05-01 22:30:14 +02:00
}
}
}