Add SyncNow test

This commit is contained in:
Michael Schurter 2016-12-02 16:44:18 -08:00
parent 92f579891c
commit 9798e7c7c1
2 changed files with 77 additions and 6 deletions

View File

@ -35,7 +35,6 @@ import (
"time"
consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/nomad/structs"
@ -56,11 +55,11 @@ const (
nomadServicePrefix = "_nomad"
// The periodic time interval for syncing services and checks with Consul
syncInterval = 5 * time.Second
defaultSyncInterval = 6 * time.Second
// syncJitter provides a little variance in the frequency at which
// defaultSyncJitter provides a little variance in the frequency at which
// Syncer polls Consul.
syncJitter = 8
defaultSyncJitter = time.Second
// ttlCheckBuffer is the time interval that Nomad can take to report Consul
// the check result
@ -144,6 +143,13 @@ type Syncer struct {
periodicCallbacks map[string]types.PeriodicCallback
notifySyncCh chan struct{}
periodicLock sync.RWMutex
// The periodic time interval for syncing services and checks with Consul
syncInterval time.Duration
// syncJitter provides a little variance in the frequency at which
// Syncer polls Consul.
syncJitter time.Duration
}
// NewSyncer returns a new consul.Syncer
@ -170,7 +176,9 @@ func NewSyncer(consulConfig *config.ConsulConfig, shutdownCh chan struct{}, logg
periodicCallbacks: make(map[string]types.PeriodicCallback),
notifySyncCh: make(chan struct{}, 1),
// default noop implementation of addrFinder
addrFinder: func(string) (string, int) { return "", 0 },
addrFinder: func(string) (string, int) { return "", 0 },
syncInterval: defaultSyncInterval,
syncJitter: defaultSyncJitter,
}
return &consulSyncer, nil
@ -810,7 +818,7 @@ func (c *Syncer) Run() {
for {
select {
case <-sync.C:
d := syncInterval - lib.RandomStagger(syncInterval/syncJitter)
d := c.syncInterval - c.syncJitter
sync.Reset(d)
if err := c.SyncServices(); err != nil {

View File

@ -23,6 +23,69 @@ const (
var logger = log.New(os.Stdout, "", log.LstdFlags)
func TestSyncNow(t *testing.T) {
cs, testconsul := testConsul(t)
defer cs.Shutdown()
defer testconsul.Stop()
cs.SetAddrFinder(func(h string) (string, int) {
a, pstr, _ := net.SplitHostPort(h)
p, _ := net.LookupPort("tcp", pstr)
return a, p
})
cs.syncInterval = 9000 * time.Hour
service := &structs.Service{Name: "foo1", Tags: []string{"a", "b"}}
services := map[ServiceKey]*structs.Service{
GenerateServiceKey(service): service,
}
// Run syncs once on startup and then blocks forever
go cs.Run()
if err := cs.SetServices(serviceGroupName, services); err != nil {
t.Fatalf("error setting services: %v", err)
}
synced := false
for i := 0; !synced && i < 10; i++ {
time.Sleep(250 * time.Millisecond)
agentServices, err := cs.queryAgentServices()
if err != nil {
t.Fatalf("error querying consul services: %v", err)
}
synced = len(agentServices) == 1
}
if !synced {
t.Fatalf("initial sync never occurred")
}
// SetServices again should cause another sync
service1 := &structs.Service{Name: "foo1", Tags: []string{"Y", "Z"}}
service2 := &structs.Service{Name: "bar"}
services = map[ServiceKey]*structs.Service{
GenerateServiceKey(service1): service1,
GenerateServiceKey(service2): service2,
}
if err := cs.SetServices(serviceGroupName, services); err != nil {
t.Fatalf("error setting services: %v", err)
}
synced = false
for i := 0; !synced && i < 10; i++ {
time.Sleep(250 * time.Millisecond)
agentServices, err := cs.queryAgentServices()
if err != nil {
t.Fatalf("error querying consul services: %v", err)
}
synced = len(agentServices) == 2
}
if !synced {
t.Fatalf("SetServices didn't sync immediately")
}
}
func TestCheckRegistration(t *testing.T) {
cs, err := NewSyncer(config.DefaultConsulConfig(), make(chan struct{}), logger)
if err != nil {