-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parked: Tombstone services before sending updates #50
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package catalog | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Nitro/sidecar/service" | ||
"github.com/relistan/go-director" | ||
|
@@ -47,36 +50,57 @@ func Test_prepareCookieJar(t *testing.T) { | |
} | ||
|
||
func Test_Listen(t *testing.T) { | ||
Convey("Listen()", t, func() { | ||
Convey("Listen()", t, func(c C) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not know this was a thing. Interesting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's handy for testing inside goroutines, although I still haven't gotten around to fixing this bug: smartystreets/goconvey#477 |
||
url := "http://beowulf.example.com" | ||
|
||
httpmock.RegisterResponder( | ||
"POST", url, | ||
func(req *http.Request) (*http.Response, error) { | ||
return httpmock.NewStringResponse(500, "so bad!"), nil | ||
}, | ||
) | ||
|
||
httpmock.Activate() | ||
listener := NewUrlListener(url, false) | ||
errors := make(chan error) | ||
listener.looper = director.NewFreeLooper(1, errors) | ||
|
||
hostname := "grendel" | ||
|
||
svcId1 := "deadbeef123" | ||
service1 := service.Service{ID: svcId1, Hostname: hostname} | ||
svcId2 := "ecgtheow" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) |
||
service2 := service.Service{ID: svcId2, Hostname: hostname} | ||
|
||
state := NewServicesState() | ||
state.Hostname = hostname | ||
state.AddServiceEntry(service1) | ||
state.Servers[hostname].Services[service1.ID].Tombstone() | ||
|
||
postShouldErr := false | ||
var changeEventTime time.Time | ||
httpmock.RegisterResponder( | ||
"POST", url, | ||
func(req *http.Request) (*http.Response, error) { | ||
if postShouldErr { | ||
return httpmock.NewStringResponse(500, "so bad!"), nil | ||
} | ||
|
||
bodyBytes, err := ioutil.ReadAll(req.Body) | ||
c.So(err, ShouldBeNil) | ||
|
||
var evt StateChangedEvent | ||
err = json.Unmarshal(bodyBytes, &evt) | ||
c.So(err, ShouldBeNil) | ||
c.So(evt.ChangeEvent.PreviousStatus, ShouldEqual, service.ALIVE) | ||
|
||
// Make sure each new event comes in with a different timestamp | ||
c.So(evt.ChangeEvent.Time, ShouldNotEqual, changeEventTime) | ||
changeEventTime = evt.ChangeEvent.Time | ||
|
||
return httpmock.NewBytesResponse(200, nil), nil | ||
}, | ||
) | ||
httpmock.Activate() | ||
Reset(func() { | ||
httpmock.DeactivateAndReset() | ||
}) | ||
|
||
Convey("handles a bad post", func() { | ||
postShouldErr = true | ||
|
||
state.AddServiceEntry(service1) | ||
state.Servers[hostname].Services[service1.ID].Tombstone() | ||
|
||
listener := NewUrlListener(url, false) | ||
errors := make(chan error) | ||
listener.looper = director.NewFreeLooper(1, errors) | ||
|
||
listener.eventChannel <- ChangeEvent{} | ||
listener.Retries = 0 | ||
listener.Watch(state) | ||
|
@@ -85,5 +109,26 @@ func Test_Listen(t *testing.T) { | |
So(err, ShouldBeNil) | ||
So(len(errors), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("gets all updates when a server expires", func() { | ||
state.AddServiceEntry(service1) | ||
state.AddServiceEntry(service2) | ||
|
||
listener := NewUrlListener(url, false) | ||
errors := make(chan error) | ||
// Do two iterations: One for each service from the expired server | ||
listener.looper = director.NewFreeLooper( | ||
len(state.Servers[hostname].Services), errors) | ||
listener.Retries = 0 | ||
|
||
listener.Watch(state) | ||
|
||
state.ExpireServer(hostname) | ||
|
||
// Block until both iterations are done | ||
err := listener.looper.Wait() | ||
So(err, ShouldBeNil) | ||
So(len(errors), ShouldEqual, 0) | ||
}) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know it occurs to me that maybe we ought to be just doing this outside the loop by capturing the initial state and the final state. No one can process those intermediate changes that fast anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I guess if anything relied on
svc.Updated
that would break them. Maybe there is some middle ground.