2019-10-03 11:23:56 +03:00
|
|
|
package gossip
|
|
|
|
|
|
|
|
import (
|
2021-01-30 00:30:51 +03:00
|
|
|
"context"
|
2019-10-03 11:23:56 +03:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/hashicorp/memberlist"
|
2021-01-30 00:30:51 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2019-10-03 11:23:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func newMemberlistConfig() *memberlist.Config {
|
|
|
|
mc := memberlist.DefaultLANConfig()
|
|
|
|
mc.DisableTcpPings = false
|
|
|
|
mc.GossipVerifyIncoming = false
|
|
|
|
mc.GossipVerifyOutgoing = false
|
|
|
|
mc.EnableCompression = false
|
|
|
|
mc.PushPullInterval = 3 * time.Second
|
|
|
|
mc.LogOutput = os.Stderr
|
|
|
|
mc.ProtocolVersion = 4
|
|
|
|
mc.Name = uuid.New().String()
|
|
|
|
return mc
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
func newRegister(opts ...register.Option) register.Register {
|
|
|
|
options := []register.Option{
|
2019-10-03 11:23:56 +03:00
|
|
|
ConnectRetry(true),
|
|
|
|
ConnectTimeout(60 * time.Second),
|
|
|
|
}
|
|
|
|
|
|
|
|
options = append(options, opts...)
|
2021-01-30 00:30:51 +03:00
|
|
|
r := NewRegister(options...)
|
2019-10-03 11:23:56 +03:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
func TestGossipRegisterBroadcast(t *testing.T) {
|
2020-02-27 13:06:16 +03:00
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
t.Skip()
|
|
|
|
}
|
2021-01-30 00:30:51 +03:00
|
|
|
ctx := context.Background()
|
2020-02-27 13:06:16 +03:00
|
|
|
|
2019-10-03 11:23:56 +03:00
|
|
|
mc1 := newMemberlistConfig()
|
2021-01-30 00:30:51 +03:00
|
|
|
r1 := newRegister(Config(mc1), Address("127.0.0.1:54321"))
|
2019-10-03 11:23:56 +03:00
|
|
|
|
|
|
|
mc2 := newMemberlistConfig()
|
2021-01-30 00:30:51 +03:00
|
|
|
r2 := newRegister(Config(mc2), Address("127.0.0.1:54322"), register.Addrs("127.0.0.1:54321"))
|
2019-10-03 11:23:56 +03:00
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
if err := r1.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := r2.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-10-03 11:23:56 +03:00
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
if err := r1.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := r2.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r1.(*gossipRegister).Stop()
|
|
|
|
defer r2.(*gossipRegister).Stop()
|
2019-10-03 11:23:56 +03:00
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
svc1 := ®ister.Service{Name: "service.1", Version: "0.0.0.1"}
|
|
|
|
svc2 := ®ister.Service{Name: "service.2", Version: "0.0.0.2"}
|
|
|
|
|
|
|
|
if err := r1.Register(ctx, svc1, register.RegisterTTL(10*time.Second)); err != nil {
|
2019-10-03 11:23:56 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-30 00:30:51 +03:00
|
|
|
if err := r2.Register(ctx, svc2, register.RegisterTTL(10*time.Second)); err != nil {
|
2019-10-03 11:23:56 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
2021-01-30 00:30:51 +03:00
|
|
|
svcs, err := r1.ListServices(ctx)
|
2019-10-03 11:23:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.Name == "service.2" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-01-30 00:30:51 +03:00
|
|
|
t.Fatalf("[gossip register] service.2 not found in r1, broadcast not work")
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
found = false
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
svcs, err = r2.ListServices(ctx)
|
2019-10-03 11:23:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.Name == "service.1" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2021-01-30 00:30:51 +03:00
|
|
|
t.Fatalf("[gossip register] broadcast failed: service.1 not found in r2")
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
if err := r1.Deregister(ctx, svc1); err != nil {
|
2019-10-03 11:23:56 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-30 00:30:51 +03:00
|
|
|
if err := r2.Deregister(ctx, svc2); err != nil {
|
2019-10-03 11:23:56 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-01-30 00:30:51 +03:00
|
|
|
func TestGossipRegisterRetry(t *testing.T) {
|
2020-02-27 13:06:16 +03:00
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
t.Skip()
|
|
|
|
}
|
2021-01-30 00:30:51 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.TraceLevel))
|
2019-10-03 11:23:56 +03:00
|
|
|
mc1 := newMemberlistConfig()
|
2021-01-30 00:30:51 +03:00
|
|
|
r1 := newRegister(Config(mc1), Address("127.0.0.1:54321"))
|
2019-10-03 11:23:56 +03:00
|
|
|
|
|
|
|
mc2 := newMemberlistConfig()
|
2021-01-30 00:30:51 +03:00
|
|
|
r2 := newRegister(Config(mc2), Address("127.0.0.1:54322"), register.Addrs("127.0.0.1:54321"))
|
|
|
|
|
|
|
|
if err := r1.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := r2.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r1.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := r2.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-10-03 11:23:56 +03:00
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
defer r1.(*gossipRegister).Stop()
|
|
|
|
defer r2.(*gossipRegister).Stop()
|
2019-10-03 11:23:56 +03:00
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
svc1 := ®ister.Service{Name: "service.1", Version: "0.0.0.1"}
|
|
|
|
svc2 := ®ister.Service{Name: "service.2", Version: "0.0.0.2"}
|
2019-10-03 11:23:56 +03:00
|
|
|
|
|
|
|
var mu sync.Mutex
|
|
|
|
ch := make(chan struct{})
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-01-30 00:30:51 +03:00
|
|
|
case <-ctx.Done():
|
|
|
|
break
|
2019-10-03 11:23:56 +03:00
|
|
|
case <-ticker.C:
|
|
|
|
mu.Lock()
|
|
|
|
if r1 != nil {
|
2021-01-30 00:30:51 +03:00
|
|
|
r1.Register(ctx, svc1, register.RegisterTTL(2*time.Second))
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
if r2 != nil {
|
2021-01-30 00:30:51 +03:00
|
|
|
r2.Register(ctx, svc2, register.RegisterTTL(2*time.Second))
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
if ch != nil {
|
|
|
|
close(ch)
|
|
|
|
ch = nil
|
|
|
|
}
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-ch
|
|
|
|
var found bool
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
svcs, err := r2.ListServices(ctx)
|
2019-10-03 11:23:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.Name == "service.1" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2021-01-30 00:30:51 +03:00
|
|
|
t.Fatalf("[gossip register] broadcast failed: service.1 not found in r2")
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
if err = r1.(*gossipRegister).Stop(); err != nil {
|
|
|
|
t.Fatalf("[gossip register] failed to stop register: %v", err)
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
r1 = nil
|
|
|
|
mu.Unlock()
|
|
|
|
|
|
|
|
<-time.After(3 * time.Second)
|
|
|
|
|
|
|
|
found = false
|
2021-01-30 00:30:51 +03:00
|
|
|
svcs, err = r2.ListServices(ctx)
|
2019-10-03 11:23:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.Name == "service.1" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
2021-01-30 00:30:51 +03:00
|
|
|
t.Fatalf("[gossip register] service.1 found in r2")
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
r1 = newRegister(Config(mc1), Address("127.0.0.1:54321"))
|
2019-10-03 11:23:56 +03:00
|
|
|
<-time.After(2 * time.Second)
|
|
|
|
|
|
|
|
found = false
|
2021-01-30 00:30:51 +03:00
|
|
|
svcs, err = r2.ListServices(ctx)
|
2019-10-03 11:23:56 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.Name == "service.1" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2021-01-30 00:30:51 +03:00
|
|
|
t.Fatalf("[gossip register] connect retry failed: service.1 not found in r2")
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
if err := r1.Deregister(ctx, svc1); err != nil {
|
2019-10-03 11:23:56 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-30 00:30:51 +03:00
|
|
|
if err := r2.Deregister(ctx, svc2); err != nil {
|
2019-10-03 11:23:56 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:30:51 +03:00
|
|
|
r1.(*gossipRegister).Stop()
|
|
|
|
r2.(*gossipRegister).Stop()
|
2019-10-03 11:23:56 +03:00
|
|
|
}
|