Adds a clone method to HealthCheck and uses that in local.go.

This commit is contained in:
James Phillips 2016-04-11 00:05:39 -07:00
parent f6fe6a2197
commit 3f340716fd
3 changed files with 31 additions and 4 deletions

View File

@ -436,13 +436,11 @@ func (l *localState) setSyncState() error {
} else { } else {
// Copy the existing check before potentially modifying // Copy the existing check before potentially modifying
// it before the compare operation. // it before the compare operation.
eCopy := new(structs.HealthCheck) eCopy := existing.Clone()
*eCopy = *existing
// Copy the server's check before modifying, otherwise // Copy the server's check before modifying, otherwise
// in-memory RPC-based unit tests will have side effects. // in-memory RPC-based unit tests will have side effects.
cCopy := new(structs.HealthCheck) cCopy := check.Clone()
*cCopy = *check
// If there's a defer timer active then we've got a // If there's a defer timer active then we've got a
// potentially spammy check so we don't sync the output // potentially spammy check so we don't sync the output

View File

@ -396,6 +396,13 @@ func (c *HealthCheck) IsSame(other *HealthCheck) bool {
return true return true
} }
// Clone returns a distinct clone of the HealthCheck.
func (c *HealthCheck) Clone() *HealthCheck {
clone := new(HealthCheck)
*clone = *c
return clone
}
type HealthChecks []*HealthCheck type HealthChecks []*HealthCheck
// CheckServiceNode is used to provide the node, its service // CheckServiceNode is used to provide the node, its service

View File

@ -211,6 +211,28 @@ func TestStructs_HealthCheck_IsSame(t *testing.T) {
check(&other.ServiceName) check(&other.ServiceName)
} }
func TestStructs_HealthCheck_Clone(t *testing.T) {
hc := &HealthCheck{
Node: "node1",
CheckID: "check1",
Name: "thecheck",
Status: HealthPassing,
Notes: "it's all good",
Output: "lgtm",
ServiceID: "service1",
ServiceName: "theservice",
}
clone := hc.Clone()
if !hc.IsSame(clone) {
t.Fatalf("should be equal to its clone")
}
clone.Output = "different"
if hc.IsSame(clone) {
t.Fatalf("should not longer be equal to its clone")
}
}
func TestStructs_CheckServiceNodes_Shuffle(t *testing.T) { func TestStructs_CheckServiceNodes_Shuffle(t *testing.T) {
// Make a huge list of nodes. // Make a huge list of nodes.
var nodes CheckServiceNodes var nodes CheckServiceNodes