2020-07-27 13:22:00 +01:00
|
|
|
package registry
|
2019-11-05 17:44:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-09 14:05:46 +03:00
|
|
|
"os"
|
2019-11-05 17:44:24 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
"github.com/micro/go-micro/v3/registry/memory"
|
|
|
|
"github.com/micro/go-micro/v3/router"
|
2019-11-05 17:44:24 +00:00
|
|
|
)
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func routerTestSetup() router.Router {
|
2019-11-05 17:44:24 +00:00
|
|
|
r := memory.NewRegistry()
|
2020-07-27 13:22:00 +01:00
|
|
|
return NewRouter(router.Registry(r))
|
2019-11-05 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:09:16 +01:00
|
|
|
func TestRouterClose(t *testing.T) {
|
2019-11-05 17:44:24 +00:00
|
|
|
r := routerTestSetup()
|
|
|
|
|
|
|
|
_, err := r.Advertise()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to start advertising: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-06-24 11:09:16 +01:00
|
|
|
if err := r.Close(); err != nil {
|
2019-11-05 17:44:24 +00:00
|
|
|
t.Errorf("failed to stop router: %v", err)
|
|
|
|
}
|
2020-04-09 14:05:46 +03:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("TestRouterStartStop STOPPED")
|
|
|
|
}
|
2019-11-05 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterAdvertise(t *testing.T) {
|
|
|
|
r := routerTestSetup()
|
|
|
|
|
|
|
|
// lower the advertise interval
|
|
|
|
AdvertiseEventsTick = 500 * time.Millisecond
|
|
|
|
|
|
|
|
ch, err := r.Advertise()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to start advertising: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// receive announce event
|
|
|
|
ann := <-ch
|
2020-04-09 14:05:46 +03:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("received announce advert: %v", ann)
|
|
|
|
}
|
2019-11-05 17:44:24 +00:00
|
|
|
|
|
|
|
// Generate random unique routes
|
|
|
|
nrRoutes := 5
|
2020-07-27 13:22:00 +01:00
|
|
|
routes := make([]router.Route, nrRoutes)
|
|
|
|
route := router.Route{
|
2019-11-05 17:44:24 +00:00
|
|
|
Service: "dest.svc",
|
|
|
|
Address: "dest.addr",
|
|
|
|
Gateway: "dest.gw",
|
|
|
|
Network: "dest.network",
|
|
|
|
Router: "src.router",
|
2019-12-02 17:36:20 +00:00
|
|
|
Link: "local",
|
2019-11-05 17:44:24 +00:00
|
|
|
Metric: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < nrRoutes; i++ {
|
|
|
|
testRoute := route
|
|
|
|
testRoute.Service = fmt.Sprintf("%s-%d", route.Service, i)
|
|
|
|
routes[i] = testRoute
|
|
|
|
}
|
|
|
|
|
|
|
|
var advertErr error
|
|
|
|
|
2019-11-20 14:53:12 +00:00
|
|
|
createDone := make(chan bool)
|
2019-11-05 17:44:24 +00:00
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
wg.Done()
|
2019-11-20 14:53:12 +00:00
|
|
|
defer close(createDone)
|
2019-11-05 17:44:24 +00:00
|
|
|
for _, route := range routes {
|
2020-04-09 14:05:46 +03:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("Creating route %v", route)
|
|
|
|
}
|
2019-11-05 17:44:24 +00:00
|
|
|
if err := r.Table().Create(route); err != nil {
|
2020-04-09 14:05:46 +03:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("Failed to create route: %v", err)
|
|
|
|
}
|
2019-11-05 17:44:24 +00:00
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var adverts int
|
2019-11-20 14:53:12 +00:00
|
|
|
readDone := make(chan bool)
|
2019-11-05 17:44:24 +00:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
wg.Done()
|
2019-11-20 14:53:12 +00:00
|
|
|
readDone <- true
|
2019-11-05 17:44:24 +00:00
|
|
|
}()
|
|
|
|
for advert := range ch {
|
|
|
|
select {
|
|
|
|
case advertErr = <-errChan:
|
|
|
|
t.Errorf("failed advertising events: %v", advertErr)
|
|
|
|
default:
|
|
|
|
// do nothing for now
|
2020-04-09 14:05:46 +03:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("Router advert received: %v", advert)
|
|
|
|
}
|
2019-11-05 17:44:24 +00:00
|
|
|
adverts += len(advert.Events)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-20 14:53:12 +00:00
|
|
|
// done adding routes to routing table
|
|
|
|
<-createDone
|
|
|
|
// done reading adverts from the routing table
|
|
|
|
<-readDone
|
2019-11-05 17:44:24 +00:00
|
|
|
|
|
|
|
if adverts != nrRoutes {
|
|
|
|
t.Errorf("Expected %d adverts, received: %d", nrRoutes, adverts)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2020-06-24 11:09:16 +01:00
|
|
|
if err := r.Close(); err != nil {
|
2019-11-05 17:44:24 +00:00
|
|
|
t.Errorf("failed to stop router: %v", err)
|
|
|
|
}
|
|
|
|
}
|