From 9113b262865e65dc4b92ddcafaae2e58d39fc765 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Mon, 13 Apr 2015 16:45:42 -0400 Subject: [PATCH] Some fixes --- command/agent/agent.go | 23 +++++++++++------------ consul/coordinate_endpoint_test.go | 24 ++++++++++++++++++------ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index cf018e96ba..fc4104e544 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -557,21 +557,20 @@ func (a *Agent) ResumeSync() { a.state.Resume() } -// StartSendingCoordinate starts a goroutine that periodically sends the local coordinate +// SendCoordinates starts a goroutine that periodically sends the local coordinate // to a server -func (a *Agent) StartSendingCoordinate() { - go func() { - var c coordinate.Coordinate - if a.config.Server { - c = a.server - } - req := structs.CoordinateUpdateRequest{ +func (a *Agent) SendCoordinates() { + var c coordinate.Coordinate + if a.config.Server { + c = a.server + } + req := structs.CoordinateUpdateRequest{ + NodeSpecificRequest: NodeSpecificRequest{ Datacenter: a.config.Datacenter, Node: a.config.NodeName, - - QueryOptions: structs.QueryOptions{Token: a.config.ACLToken}, - } - }() + }, + QueryOptions: structs.QueryOptions{Token: a.config.ACLToken}, + } } // persistService saves a service definition to a JSON file in the data dir diff --git a/consul/coordinate_endpoint_test.go b/consul/coordinate_endpoint_test.go index c32392b8dd..7df632115f 100644 --- a/consul/coordinate_endpoint_test.go +++ b/consul/coordinate_endpoint_test.go @@ -4,25 +4,37 @@ import ( "math/rand" "os" "testing" + "time" "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/testutil" "github.com/hashicorp/serf/coordinate" ) +// getRandomCoordinate generates a random coordinate. func getRandomCoordinate() *coordinate.Coordinate { config := coordinate.DefaultConfig() - coord := coordinate.NewCoordinate(config) - for i := 0; i < len(coord.Vec); i++ { - coord.Vec[i] = rand.Float64() + // Randomly apply updates between n clients + n := 5 + clients := make([]*coordinate.Client, n) + for i := 0; i < n; i++ { + clients[i] = coordinate.NewClient(config) } - return coord + + for i := 0; i < n*100; i++ { + k1 := rand.Intn(n) + k2 := rand.Intn(n) + if k1 == k2 { + continue + } + clients[k1].Update(clients[k2].GetCoordinate(), time.Duration(rand.Int63())*time.Microsecond) + } + return clients[rand.Intn(n)].GetCoordinate() } func coordinatesEqual(a, b *coordinate.Coordinate) bool { config := coordinate.DefaultConfig() - client := coordinate.NewClient(config) - dist, err := client.DistanceBetween(a, b) + dist, err := a.DistanceTo(b, config) if err != nil { panic(err) }