2016-05-01 23:30:14 +03:00
|
|
|
package mdns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWatcher(t *testing.T) {
|
|
|
|
testData := []*registry.Service{
|
|
|
|
®istry.Service{
|
|
|
|
Name: "test1",
|
|
|
|
Version: "1.0.1",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
®istry.Node{
|
|
|
|
Id: "test1-1",
|
|
|
|
Address: "10.0.0.1",
|
|
|
|
Port: 10001,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
®istry.Service{
|
|
|
|
Name: "test2",
|
|
|
|
Version: "1.0.2",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
®istry.Node{
|
|
|
|
Id: "test2-1",
|
|
|
|
Address: "10.0.0.2",
|
|
|
|
Port: 10002,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo2": "bar2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
®istry.Service{
|
|
|
|
Name: "test3",
|
|
|
|
Version: "1.0.3",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
®istry.Node{
|
|
|
|
Id: "test3-1",
|
|
|
|
Address: "10.0.0.3",
|
|
|
|
Port: 10003,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"foo3": "bar3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
testFn := func(service, s *registry.Service) {
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("Expected one result for %s got nil", service.Name)
|
2016-05-01 23:30:14 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if s.Name != service.Name {
|
|
|
|
t.Fatalf("Expected name %s got %s", service.Name, s.Name)
|
2016-05-01 23:30:14 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if s.Version != service.Version {
|
|
|
|
t.Fatalf("Expected version %s got %s", service.Version, s.Version)
|
2016-05-01 23:38:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if len(s.Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node, got %d", len(s.Nodes))
|
2016-05-01 23:38:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
node := s.Nodes[0]
|
2016-05-01 23:30:14 +03: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Port != service.Nodes[0].Port {
|
|
|
|
t.Fatalf("Expected node port %s got %s", service.Nodes[0].Port, node.Port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
// new registry
|
|
|
|
r := NewRegistry()
|
|
|
|
|
|
|
|
w, err := r.Watch()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer w.Stop()
|
|
|
|
|
2016-05-01 23:30:14 +03:00
|
|
|
for _, service := range testData {
|
2016-05-02 00:05:21 +03:00
|
|
|
// register service
|
|
|
|
if err := r.Register(service); err != nil {
|
2016-05-01 23:30:14 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
var ok bool
|
2016-05-01 23:30:14 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// get registered service
|
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-01 23:38:22 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if res.Service.Name != service.Name {
|
|
|
|
break
|
|
|
|
}
|
2016-05-01 23:38:22 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if res.Action != "create" {
|
2016-05-01 23:38:22 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
testFn(service, res.Service)
|
|
|
|
ok = true
|
|
|
|
break
|
2016-05-01 23:30:14 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Watch test failed for creation of %s", service.Name)
|
2016-05-01 23:30:14 +03:00
|
|
|
}
|
2016-05-02 00:05:21 +03:00
|
|
|
}
|
2016-05-01 23:30:14 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
for _, service := range testData {
|
|
|
|
// deregister
|
|
|
|
if err := r.Deregister(service); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-05-01 23:30:14 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
var ok bool
|
2016-05-01 23:30:14 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// get registered service
|
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-01 23:30:14 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if res.Action != "delete" {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-01 23:30:14 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if res.Service.Name != service.Name {
|
|
|
|
break
|
|
|
|
}
|
2016-05-01 23:30:14 +03:00
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
testFn(service, res.Service)
|
|
|
|
ok = true
|
|
|
|
break
|
2016-05-01 23:30:14 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 00:05:21 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Watch test failed for deletion of %s", service.Name)
|
2016-05-01 23:30:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|