From ab9262c6564ab4af0878fc11b3be21e94bfc6440 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Sat, 18 Apr 2015 20:49:49 -0400 Subject: [PATCH] Add a test case --- command/agent/agent.go | 4 ++-- command/agent/command.go | 2 +- command/agent/local_test.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 1981bd368d..b7a84521f3 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -562,7 +562,7 @@ func (a *Agent) ResumeSync() { // SendCoordinates starts a loop that periodically sends the local coordinate // to a server -func (a *Agent) SendCoordinates(shutdownCh chan struct{}) { +func (a *Agent) SendCoordinates() { for { intv := aeScale(a.config.SyncCoordinateInterval, len(a.LANMembers())) intv = intv + randomStagger(intv) @@ -587,7 +587,7 @@ func (a *Agent) SendCoordinates(shutdownCh chan struct{}) { if err := a.RPC("Coordinate.Update", &req, &reply); err != nil { a.logger.Printf("[ERR] coordinate update error: %s", err.Error()) } - case <-shutdownCh: + case <-a.shutdownCh: return } } diff --git a/command/agent/command.go b/command/agent/command.go index 69bc9233dd..89d2ac7d97 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -710,7 +710,7 @@ func (c *Command) Run(args []string) int { go c.retryJoinWan(config, errWanCh) // Start sending network coordinates to servers - go c.agent.SendCoordinates(c.agent.shutdownCh) + go c.agent.SendCoordinates() // Wait for exit return c.handleSignals(config, errCh, errWanCh) diff --git a/command/agent/local_test.go b/command/agent/local_test.go index 7f45267bbd..c307e0a75b 100644 --- a/command/agent/local_test.go +++ b/command/agent/local_test.go @@ -801,3 +801,40 @@ service "api" { policy = "write" } ` + +func TestAgentSendCoordinates(t *testing.T) { + conf1 := nextConfig() + conf1.SyncCoordinateInterval = 10 * time.Millisecond + dir1, agent1 := makeAgent(t, conf1) + defer os.RemoveAll(dir1) + defer agent1.Shutdown() + + conf2 := nextConfig() + conf2.SyncCoordinateInterval = 10 * time.Millisecond + dir2, agent2 := makeAgent(t, conf2) + defer os.RemoveAll(dir2) + defer agent2.Shutdown() + + agent2Addr := fmt.Sprintf("127.0.0.1:%d", agent2.config.Ports.SerfLan) + if _, err := agent2.JoinLAN([]string{agent2Addr}); err != nil { + t.Fatalf("err: %s", err) + } + + testutil.WaitForLeader(t, agent1.RPC, "dc1") + + go agent1.SendCoordinates() + go agent2.SendCoordinates() + time.Sleep(100 * time.Millisecond) + + var reply structs.IndexedCoordinate + req := structs.CoordinateGetRequest{ + Datacenter: agent1.config.Datacenter, + Node: agent1.config.NodeName, + } + if err := agent1.RPC("Coordinate.Get", &req, &reply); err != nil { + t.Fatalf("err: %v", err) + } + if reply.Coord == nil { + t.Fatalf("should get a coordinate") + } +}