Browse Source

agent: make event buffer test non-sequential

pull/489/head
Ryan Uber 10 years ago
parent
commit
dbd420fc4b
  1. 47
      command/agent/event_endpoint_test.go

47
command/agent/event_endpoint_test.go

@ -130,14 +130,14 @@ func TestEventList_Filter(t *testing.T) {
func TestEventList_Blocking(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
p := &UserEvent{Name: "foo"}
p := &UserEvent{Name: "test"}
if err := srv.agent.UserEvent("", p); err != nil {
t.Fatalf("err: %v", err)
}
var index string
testutil.WaitForResult(func() (bool, error) {
req, err := http.NewRequest("GET", "/v1/event/list?name=foo", nil)
req, err := http.NewRequest("GET", "/v1/event/list", nil)
if err != nil {
return false, err
}
@ -158,14 +158,14 @@ func TestEventList_Blocking(t *testing.T) {
go func() {
time.Sleep(50 * time.Millisecond)
p := &UserEvent{Name: "bar"}
p := &UserEvent{Name: "second"}
if err := srv.agent.UserEvent("", p); err != nil {
t.Fatalf("err: %v", err)
}
}()
testutil.WaitForResult(func() (bool, error) {
url := "/v1/event/list?name=bar&index=" + index
url := "/v1/event/list?index=" + index
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, err
@ -175,32 +175,42 @@ func TestEventList_Blocking(t *testing.T) {
if err != nil {
return false, err
}
header := resp.Header().Get("X-Consul-Index")
if header == "" || header == "0" {
return false, fmt.Errorf("bad: %#v", header)
}
list, ok := obj.([]*UserEvent)
if !ok {
return false, fmt.Errorf("bad: %#v", obj)
}
if len(list) != 1 || list[0].Name != "bar" {
if len(list) != 2 || list[1].Name != "second" {
return false, fmt.Errorf("bad: %#v", list)
}
index = header
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
})
}
// Test again to make sure that the event order is preserved
// when name filtering on a list of > 1 matching event.
p = &UserEvent{Name: "bar"}
if err := srv.agent.UserEvent("", p); err != nil {
func TestEventList_EventBufOrder(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
// Fire some events in a non-sequential order
expected := &UserEvent{Name: "foo"}
for _, e := range []*UserEvent{
&UserEvent{Name: "foo"},
&UserEvent{Name: "bar"},
&UserEvent{Name: "foo"},
expected,
&UserEvent{Name: "bar"},
} {
if err := srv.agent.UserEvent("", e); err != nil {
t.Fatalf("err: %v", err)
}
}
// Test that the event order is preserved when name
// filtering on a list of > 1 matching event.
testutil.WaitForResult(func() (bool, error) {
url := "/v1/event/list?name=bar&index=" + index
url := "/v1/event/list?name=foo"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, err
@ -210,18 +220,13 @@ func TestEventList_Blocking(t *testing.T) {
if err != nil {
return false, err
}
header := resp.Header().Get("X-Consul-Index")
if header == "" || header == "0" {
return false, fmt.Errorf("bad: %#v", header)
}
list, ok := obj.([]*UserEvent)
if !ok {
return false, fmt.Errorf("bad: %#v", obj)
}
if len(list) != 2 || list[1].Name != "bar" || list[1].ID != p.ID {
if len(list) != 3 || list[2].ID != expected.ID {
return false, fmt.Errorf("bad: %#v", list)
}
index = header
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)

Loading…
Cancel
Save