diff --git a/command/agent/event_endpoint.go b/command/agent/event_endpoint.go index f1aa39e3a..b93af5ce9 100644 --- a/command/agent/event_endpoint.go +++ b/command/agent/event_endpoint.go @@ -118,16 +118,12 @@ RUN_QUERY: // Filter the events if necessary if nameFilter != "" { - n := len(events) - for i := 0; i < n; i++ { - if events[i].Name == nameFilter { - continue + for i := 0; i < len(events); i++ { + if events[i].Name != nameFilter { + events = append(events[:i], events[i+1:]...) + i-- } - events[i], events[n-1] = events[n-1], nil - i-- - n-- } - events = events[:n] } // Determine the index diff --git a/command/agent/event_endpoint_test.go b/command/agent/event_endpoint_test.go index 6740a3c57..81b384695 100644 --- a/command/agent/event_endpoint_test.go +++ b/command/agent/event_endpoint_test.go @@ -190,6 +190,50 @@ func TestEventList_Blocking(t *testing.T) { }) } +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=foo" + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return false, err + } + resp := httptest.NewRecorder() + obj, err := srv.EventList(resp, req) + if err != nil { + return false, err + } + list, ok := obj.([]*UserEvent) + if !ok { + return false, fmt.Errorf("bad: %#v", obj) + } + if len(list) != 3 || list[2].ID != expected.ID { + return false, fmt.Errorf("bad: %#v", list) + } + return true, nil + }, func(err error) { + t.Fatalf("err: %v", err) + }) + }) +} + func TestUUIDToUint64(t *testing.T) { inp := "cb9a81ad-fff6-52ac-92a7-5f70687805ec"