Skip to content

Commit 19fd195

Browse files
committed
Add SyncNow test
1 parent 5202944 commit 19fd195

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

command/agent/consul/syncer.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"time"
3636

3737
consul "github.com/hashicorp/consul/api"
38-
"github.com/hashicorp/consul/lib"
3938
"github.com/hashicorp/go-multierror"
4039

4140
"github.com/hashicorp/nomad/nomad/structs"
@@ -56,11 +55,11 @@ const (
5655
nomadServicePrefix = "_nomad"
5756

5857
// The periodic time interval for syncing services and checks with Consul
59-
syncInterval = 5 * time.Second
58+
defaultSyncInterval = 6 * time.Second
6059

61-
// syncJitter provides a little variance in the frequency at which
60+
// defaultSyncJitter provides a little variance in the frequency at which
6261
// Syncer polls Consul.
63-
syncJitter = 8
62+
defaultSyncJitter = time.Second
6463

6564
// ttlCheckBuffer is the time interval that Nomad can take to report Consul
6665
// the check result
@@ -144,6 +143,13 @@ type Syncer struct {
144143
periodicCallbacks map[string]types.PeriodicCallback
145144
notifySyncCh chan struct{}
146145
periodicLock sync.RWMutex
146+
147+
// The periodic time interval for syncing services and checks with Consul
148+
syncInterval time.Duration
149+
150+
// syncJitter provides a little variance in the frequency at which
151+
// Syncer polls Consul.
152+
syncJitter time.Duration
147153
}
148154

149155
// NewSyncer returns a new consul.Syncer
@@ -170,7 +176,9 @@ func NewSyncer(consulConfig *config.ConsulConfig, shutdownCh chan struct{}, logg
170176
periodicCallbacks: make(map[string]types.PeriodicCallback),
171177
notifySyncCh: make(chan struct{}, 1),
172178
// default noop implementation of addrFinder
173-
addrFinder: func(string) (string, int) { return "", 0 },
179+
addrFinder: func(string) (string, int) { return "", 0 },
180+
syncInterval: defaultSyncInterval,
181+
syncJitter: defaultSyncJitter,
174182
}
175183

176184
return &consulSyncer, nil
@@ -810,7 +818,7 @@ func (c *Syncer) Run() {
810818
for {
811819
select {
812820
case <-sync.C:
813-
d := syncInterval - lib.RandomStagger(syncInterval/syncJitter)
821+
d := c.syncInterval - c.syncJitter
814822
sync.Reset(d)
815823

816824
if err := c.SyncServices(); err != nil {

command/agent/consul/syncer_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,69 @@ const (
2323

2424
var logger = log.New(os.Stdout, "", log.LstdFlags)
2525

26+
func TestSyncNow(t *testing.T) {
27+
cs, testconsul := testConsul(t)
28+
defer cs.Shutdown()
29+
defer testconsul.Stop()
30+
31+
cs.SetAddrFinder(func(h string) (string, int) {
32+
a, pstr, _ := net.SplitHostPort(h)
33+
p, _ := net.LookupPort("tcp", pstr)
34+
return a, p
35+
})
36+
cs.syncInterval = 9000 * time.Hour
37+
38+
service := &structs.Service{Name: "foo1", Tags: []string{"a", "b"}}
39+
services := map[ServiceKey]*structs.Service{
40+
GenerateServiceKey(service): service,
41+
}
42+
43+
// Run syncs once on startup and then blocks forever
44+
go cs.Run()
45+
46+
if err := cs.SetServices(serviceGroupName, services); err != nil {
47+
t.Fatalf("error setting services: %v", err)
48+
}
49+
50+
synced := false
51+
for i := 0; !synced && i < 10; i++ {
52+
time.Sleep(250 * time.Millisecond)
53+
agentServices, err := cs.queryAgentServices()
54+
if err != nil {
55+
t.Fatalf("error querying consul services: %v", err)
56+
}
57+
synced = len(agentServices) == 1
58+
}
59+
if !synced {
60+
t.Fatalf("initial sync never occurred")
61+
}
62+
63+
// SetServices again should cause another sync
64+
service1 := &structs.Service{Name: "foo1", Tags: []string{"Y", "Z"}}
65+
service2 := &structs.Service{Name: "bar"}
66+
services = map[ServiceKey]*structs.Service{
67+
GenerateServiceKey(service1): service1,
68+
GenerateServiceKey(service2): service2,
69+
}
70+
71+
if err := cs.SetServices(serviceGroupName, services); err != nil {
72+
t.Fatalf("error setting services: %v", err)
73+
}
74+
75+
synced = false
76+
for i := 0; !synced && i < 10; i++ {
77+
time.Sleep(250 * time.Millisecond)
78+
agentServices, err := cs.queryAgentServices()
79+
if err != nil {
80+
t.Fatalf("error querying consul services: %v", err)
81+
}
82+
synced = len(agentServices) == 2
83+
}
84+
if !synced {
85+
t.Fatalf("SetServices didn't sync immediately")
86+
}
87+
}
88+
2689
func TestCheckRegistration(t *testing.T) {
2790
cs, err := NewSyncer(config.DefaultConsulConfig(), make(chan struct{}), logger)
2891
if err != nil {

0 commit comments

Comments
 (0)