Skip to content

Commit

Permalink
Removes some redundant test code, move const definition closer to whe…
Browse files Browse the repository at this point in the history
…re its used
  • Loading branch information
Preetha Appan committed Oct 20, 2017
1 parent 99bd366 commit 585c31c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
8 changes: 3 additions & 5 deletions coordinate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"time"
)

const MAX_RTT_SECONDS = 32400 //9 hours is the upper bound for ping period

// Client manages the estimated network coordinate for a given node, and adjusts
// it as the node observes round trip times and estimated coordinates from other
// nodes. The core algorithm is based on Vivaldi, see the documentation for Config
Expand Down Expand Up @@ -207,9 +205,9 @@ func (c *Client) Update(node string, other *Coordinate, rtt time.Duration) (*Coo
return nil, err
}

durSec := rtt.Seconds()
if durSec <= 0 || durSec > MAX_RTT_SECONDS {
return nil, fmt.Errorf("Round trip time not in valid range, duration %v is not a positive value less than %v ", durSec, MAX_RTT_SECONDS)
const maxRTT = 10 * time.Second
if rtt <= 0 || rtt > maxRTT {
return nil, fmt.Errorf("round trip time not in valid range, duration %v is not a positive value less than %v ", rtt, maxRTT)
}

rttSeconds := c.latencyFilter(node, rtt.Seconds())
Expand Down
34 changes: 6 additions & 28 deletions coordinate/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,24 @@ func TestClient_InvalidInPingValues(t *testing.T) {
t.Fatal(err)
}

// Make sure the Euclidean part of our coordinate is what we expect.
c := client.GetCoordinate()
verifyEqualVectors(t, c.Vec, []float64{0.0, 0.0, 0.0})

// Place another node
other := NewCoordinate(config)
other.Vec[2] = 0.001
rtt := time.Duration(2.0 * other.Vec[2] * secondsToNanoseconds)
c, err = client.Update("node", other, rtt)
if err != nil {
t.Fatalf("err: %v", err)
}

dist_old := client.DistanceTo(other)
// Update with a valid ping period, should have an affect on estimated rtt
ping := 20
_, err = client.Update("node", other, time.Duration(ping*secondsToNanoseconds))
if err != nil {
t.Fatalf("Unexpected error ", err)
}

dist_new := client.DistanceTo(other)
if dist_new <= dist_old {
t.Fatalf("Invalid rtt estimate %v", dist_new)
}
dist := client.DistanceTo(other)

// Update with a series of invalid ping periods, should return an error and estimated rtt remains unchanged
pings := []int{1<<63 - 1, -35, -1 << 63, 35000}
dist_old = dist_new
pings := []int{1<<63 - 1, -35, 11}

for _, ping := range pings {
expectedErr := fmt.Errorf("Round trip time not in valid range, duration %v is not a positive value less than %v", ping, MAX_RTT_SECONDS)
expectedErr := fmt.Errorf("round trip time not in valid range, duration %v is not a positive value less than %v", ping, 10*time.Second)
_, err = client.Update("node", other, time.Duration(ping*secondsToNanoseconds))
if err == nil {
t.Fatalf("Unexpected error, wanted %v but got %v", expectedErr, err)
}

dist_new = client.DistanceTo(other)
if dist_new != dist_old {
t.Fatalf("distance est", dist_new)
dist_new := client.DistanceTo(other)
if dist_new != dist {
t.Fatalf("distance estimate %v not equal to %v", dist_new, dist)
}
}

Expand Down

0 comments on commit 585c31c

Please sign in to comment.