From 434155f97f318b57cd210d5133325df0bcb2a4d5 Mon Sep 17 00:00:00 2001 From: Asim Date: Sun, 1 May 2016 21:30:14 +0100 Subject: [PATCH] Add watcher test --- mdns_test.go | 8 +-- watcher_test.go | 164 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 watcher_test.go diff --git a/mdns_test.go b/mdns_test.go index 33789b2..0953734 100644 --- a/mdns_test.go +++ b/mdns_test.go @@ -63,26 +63,22 @@ func TestMDNS(t *testing.T) { } // get registered service - s, err := r.GetService(service.Name) if err != nil { t.Fatal(err) } if len(s) != 1 { - for _, srv := range s { - t.Logf("%+v", srv) - } t.Fatalf("Expected one result for %s got %d", service.Name, len(s)) } if s[0].Name != service.Name { - t.Fatal("Expected name %s got %s", service.Name, s[0].Name) + t.Fatalf("Expected name %s got %s", service.Name, s[0].Name) } if s[0].Version != service.Version { - t.Fatal("Expected version %s got %s", service.Version, s[0].Version) + t.Fatalf("Expected version %s got %s", service.Version, s[0].Version) } if len(s[0].Nodes) != 1 { diff --git a/watcher_test.go b/watcher_test.go new file mode 100644 index 0000000..ab209f6 --- /dev/null +++ b/watcher_test.go @@ -0,0 +1,164 @@ +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", + }, + }, + }, + }, + } + + // new registry + r := NewRegistry() + + w, err := r.Watch() + if err != nil { + t.Fatal(err) + } + defer w.Stop() + + for _, service := range testData { + // register service + if err := r.Register(service); err != nil { + t.Fatal(err) + } + + // get registered service + res, err := w.Next() + if err != nil { + t.Fatal(err) + } + + if res.Action != "create" { + t.Fatal("Expected create action for %s", service.Name) + } + + s := []*registry.Service{res.Service} + + if len(s) != 1 { + t.Fatalf("Expected one result for %s got %d", service.Name, len(s)) + + } + + if s[0].Name != service.Name { + t.Fatalf("Expected name %s got %s", service.Name, s[0].Name) + } + + if s[0].Version != service.Version { + t.Fatalf("Expected version %s got %s", service.Version, s[0].Version) + } + + if len(s[0].Nodes) != 1 { + t.Fatalf("Expected 1 node, got %d", len(s[0].Nodes)) + } + + node := s[0].Nodes[0] + + 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) + } + } + + for _, service := range testData { + // deregister + if err := r.Deregister(service); err != nil { + t.Fatal(err) + } + + // get registered service + res, err := w.Next() + if err != nil { + t.Fatal(err) + } + + if res.Action != "delete" { + t.Fatalf("Expected create action for %s", service.Name) + } + + s := []*registry.Service{res.Service} + + if len(s) != 1 { + t.Fatalf("Expected one result for %s got %d", service.Name, len(s)) + + } + + if s[0].Name != service.Name { + t.Fatalf("Expected name %s got %s", service.Name, s[0].Name) + } + + if s[0].Version != service.Version { + t.Fatalf("Expected version %s got %s", service.Version, s[0].Version) + } + + if len(s[0].Nodes) != 1 { + t.Fatalf("Expected 1 node, got %d", len(s[0].Nodes)) + } + + node := s[0].Nodes[0] + + 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) + } + } + +}