Try a different way of testing

This commit is contained in:
Asim 2016-05-01 22:05:21 +01:00 committed by Vasiliy Tolstov
parent 26d7b599e9
commit 2482bf3f81

View File

@ -52,6 +52,39 @@ func TestWatcher(t *testing.T) {
}, },
} }
testFn := func(service, s *registry.Service) {
if s == nil {
t.Fatalf("Expected one result for %s got nil", service.Name)
}
if s.Name != service.Name {
t.Fatalf("Expected name %s got %s", service.Name, s.Name)
}
if s.Version != service.Version {
t.Fatalf("Expected version %s got %s", service.Version, s.Version)
}
if len(s.Nodes) != 1 {
t.Fatalf("Expected 1 node, got %d", len(s.Nodes))
}
node := s.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)
}
}
// new registry // new registry
r := NewRegistry() r := NewRegistry()
@ -66,63 +99,31 @@ func TestWatcher(t *testing.T) {
if err := r.Register(service); err != nil { if err := r.Register(service); err != nil {
t.Fatal(err) t.Fatal(err)
} }
}
for i := 0; i < 3; i++ { var ok bool
for i := 0; i < 5; i++ {
// get registered service // get registered service
res, err := w.Next() res, err := w.Next()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if res.Action != "create" { if res.Service.Name != service.Name {
t.Fatal("Expected create action for %s", res.Service.Name)
}
var service *registry.Service
for _, srv := range testData {
if res.Service.Name == srv.Name {
service = res.Service
break break
} }
if res.Action != "create" {
break
} }
if service == nil { testFn(service, res.Service)
t.Fatal("Got watch result for unknown service %s", res.Service.Name) ok = true
break
} }
s := []*registry.Service{res.Service} if !ok {
t.Fatalf("Watch test failed for creation of %s", service.Name)
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)
} }
} }
@ -132,6 +133,9 @@ func TestWatcher(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var ok bool
for i := 0; i < 5; i++ {
// get registered service // get registered service
res, err := w.Next() res, err := w.Next()
if err != nil { if err != nil {
@ -139,53 +143,20 @@ func TestWatcher(t *testing.T) {
} }
if res.Action != "delete" { if res.Action != "delete" {
t.Fatalf("Expected create action for %s", res.Service.Name) continue
} }
var service *registry.Service if res.Service.Name != service.Name {
for _, srv := range testData {
if res.Service.Name == srv.Name {
service = res.Service
break break
} }
testFn(service, res.Service)
ok = true
break
} }
if service == nil { if !ok {
t.Fatal("Got watch result for unknown service %s", res.Service.Name) t.Fatalf("Watch test failed for deletion of %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)
} }
} }