2019-01-14 15:27:25 +00:00
|
|
|
package memory
|
2016-03-14 19:15:42 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-16 02:55:11 -08:00
|
|
|
"fmt"
|
2020-04-09 14:05:46 +03:00
|
|
|
"os"
|
2016-03-14 19:15:42 +00:00
|
|
|
"testing"
|
2019-11-16 02:55:11 -08:00
|
|
|
"time"
|
2016-03-14 19:15:42 +00:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2016-03-14 19:15:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testData = map[string][]*registry.Service{
|
2019-11-01 15:07:53 +00:00
|
|
|
"foo": {
|
2016-03-14 19:15:42 +00:00
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Version: "1.0.0",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.0-123",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "localhost:9999",
|
2016-03-14 19:15:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.0-321",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "localhost:9999",
|
2016-03-14 19:15:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Version: "1.0.1",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.1-321",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "localhost:6666",
|
2016-03-14 19:15:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Version: "1.0.3",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "foo-1.0.3-345",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "localhost:8888",
|
2016-03-14 19:15:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-01 15:07:53 +00:00
|
|
|
"bar": {
|
2016-03-14 19:15:42 +00:00
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Version: "default",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "bar-1.0.0-123",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "localhost:9999",
|
2016-03-14 19:15:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "bar-1.0.0-321",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "localhost:9999",
|
2016-03-14 19:15:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Version: "latest",
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
{
|
|
|
|
Id: "bar-1.0.1-321",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: "localhost:6666",
|
2016-03-14 19:15:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-01-18 17:29:17 +00:00
|
|
|
func TestMemoryRegistry(t *testing.T) {
|
2016-03-14 19:15:42 +00:00
|
|
|
m := NewRegistry()
|
|
|
|
|
|
|
|
fn := func(k string, v []*registry.Service) {
|
|
|
|
services, err := m.GetService(k)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error getting service %s: %v", k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(services) != len(v) {
|
|
|
|
t.Errorf("Expected %d services for %s, got %d", len(v), k, len(services))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range v {
|
|
|
|
var seen bool
|
|
|
|
for _, s := range services {
|
|
|
|
if s.Version == service.Version {
|
|
|
|
seen = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
|
|
|
t.Errorf("expected to find version %s", service.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// register data
|
|
|
|
for _, v := range testData {
|
2019-11-16 02:55:11 -08:00
|
|
|
serviceCount := 0
|
2016-03-14 19:15:42 +00:00
|
|
|
for _, service := range v {
|
|
|
|
if err := m.Register(service); err != nil {
|
|
|
|
t.Errorf("Unexpected register error: %v", err)
|
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
serviceCount++
|
|
|
|
// after the service has been registered we should be able to query it
|
|
|
|
services, err := m.GetService(service.Name)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error getting service %s: %v", service.Name, err)
|
|
|
|
}
|
|
|
|
if len(services) != serviceCount {
|
|
|
|
t.Errorf("Expected %d services for %s, got %d", serviceCount, service.Name, len(services))
|
|
|
|
}
|
2016-03-14 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// using test data
|
|
|
|
for k, v := range testData {
|
|
|
|
fn(k, v)
|
|
|
|
}
|
|
|
|
|
2019-11-16 02:55:11 -08:00
|
|
|
services, err := m.ListServices()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when listing services: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
totalServiceCount := 0
|
|
|
|
for _, testSvc := range testData {
|
|
|
|
for range testSvc {
|
|
|
|
totalServiceCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(services) != totalServiceCount {
|
|
|
|
t.Errorf("Expected total service count: %d, got: %d", totalServiceCount, len(services))
|
|
|
|
}
|
|
|
|
|
2016-03-14 19:15:42 +00:00
|
|
|
// deregister
|
|
|
|
for _, v := range testData {
|
|
|
|
for _, service := range v {
|
|
|
|
if err := m.Deregister(service); err != nil {
|
|
|
|
t.Errorf("Unexpected deregister error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
|
|
|
|
// after all the service nodes have been deregistered we should not get any results
|
|
|
|
for _, v := range testData {
|
|
|
|
for _, service := range v {
|
|
|
|
services, err := m.GetService(service.Name)
|
|
|
|
if err != registry.ErrNotFound {
|
|
|
|
t.Errorf("Expected error: %v, got: %v", registry.ErrNotFound, err)
|
|
|
|
}
|
|
|
|
if len(services) != 0 {
|
|
|
|
t.Errorf("Expected %d services for %s, got %d", 0, service.Name, len(services))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryRegistryTTL(t *testing.T) {
|
|
|
|
m := NewRegistry()
|
|
|
|
|
|
|
|
for _, v := range testData {
|
|
|
|
for _, service := range v {
|
|
|
|
if err := m.Register(service, registry.RegisterTTL(time.Millisecond)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(ttlPruneTime * 2)
|
|
|
|
|
|
|
|
for name := range testData {
|
|
|
|
svcs, err := m.GetService(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if len(svc.Nodes) > 0 {
|
|
|
|
t.Fatalf("Service %q still has nodes registered", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryRegistryTTLConcurrent(t *testing.T) {
|
|
|
|
concurrency := 1000
|
2019-11-16 21:13:06 +00:00
|
|
|
waitTime := ttlPruneTime * 2
|
2019-11-16 02:55:11 -08:00
|
|
|
m := NewRegistry()
|
|
|
|
|
|
|
|
for _, v := range testData {
|
|
|
|
for _, service := range v {
|
2019-11-16 21:13:06 +00:00
|
|
|
if err := m.Register(service, registry.RegisterTTL(waitTime/2)); err != nil {
|
2019-11-16 02:55:11 -08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:05:46 +03:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("test will wait %v, then check TTL timeouts", waitTime)
|
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
|
|
|
|
errChan := make(chan error, concurrency)
|
|
|
|
syncChan := make(chan struct{})
|
|
|
|
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
go func() {
|
|
|
|
<-syncChan
|
|
|
|
for name := range testData {
|
|
|
|
svcs, err := m.GetService(name)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if len(svc.Nodes) > 0 {
|
|
|
|
errChan <- fmt.Errorf("Service %q still has nodes registered", name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan <- nil
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(waitTime)
|
|
|
|
close(syncChan)
|
|
|
|
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
if err := <-errChan; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 19:15:42 +00:00
|
|
|
}
|
2020-06-18 12:39:19 +01:00
|
|
|
|
|
|
|
func TestMemoryWildcard(t *testing.T) {
|
|
|
|
m := NewRegistry()
|
|
|
|
testSrv := ®istry.Service{Name: "foo", Version: "1.0.0"}
|
|
|
|
|
|
|
|
if err := m.Register(testSrv, registry.RegisterDomain("one")); err != nil {
|
|
|
|
t.Fatalf("Register err: %v", err)
|
|
|
|
}
|
|
|
|
if err := m.Register(testSrv, registry.RegisterDomain("two")); err != nil {
|
|
|
|
t.Fatalf("Register err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if recs, err := m.ListServices(registry.ListDomain("one")); err != nil {
|
|
|
|
t.Errorf("List err: %v", err)
|
|
|
|
} else if len(recs) != 1 {
|
|
|
|
t.Errorf("Expected 1 record, got %v", len(recs))
|
|
|
|
}
|
|
|
|
|
|
|
|
if recs, err := m.ListServices(registry.ListDomain("*")); err != nil {
|
|
|
|
t.Errorf("List err: %v", err)
|
|
|
|
} else if len(recs) != 2 {
|
|
|
|
t.Errorf("Expected 2 records, got %v", len(recs))
|
|
|
|
}
|
|
|
|
|
|
|
|
if recs, err := m.GetService(testSrv.Name, registry.GetDomain("one")); err != nil {
|
|
|
|
t.Errorf("Get err: %v", err)
|
|
|
|
} else if len(recs) != 1 {
|
|
|
|
t.Errorf("Expected 1 record, got %v", len(recs))
|
|
|
|
}
|
|
|
|
|
|
|
|
if recs, err := m.GetService(testSrv.Name, registry.GetDomain("*")); err != nil {
|
|
|
|
t.Errorf("Get err: %v", err)
|
|
|
|
} else if len(recs) != 2 {
|
|
|
|
t.Errorf("Expected 2 records, got %v", len(recs))
|
|
|
|
}
|
|
|
|
}
|