Skip to content

Commit

Permalink
Moves conversion of nil slices up to HTTP layer for prepared queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Phillips committed Nov 16, 2015
1 parent b8ddb21 commit e1ce1a3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
10 changes: 10 additions & 0 deletions command/agent/prepared_query_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func (s *HTTPServer) PreparedQueryGeneral(resp http.ResponseWriter, req *http.Re
if err := s.agent.RPC(endpoint+".List", &args, &reply); err != nil {
return nil, err
}

// Use empty list instead of nil.
if reply.Queries == nil {
reply.Queries = make(structs.PreparedQueries, 0)
}
return reply.Queries, nil

default:
Expand Down Expand Up @@ -111,6 +116,11 @@ func (s *HTTPServer) PreparedQuerySpecific(resp http.ResponseWriter, req *http.R
}
return nil, err
}

// Use empty list instead of nil.
if reply.Nodes == nil {
reply.Nodes = make(structs.CheckServiceNodes, 0)
}
return reply, nil
} else {
args := structs.PreparedQuerySpecificRequest{
Expand Down
68 changes: 68 additions & 0 deletions command/agent/prepared_query_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,40 @@ func TestPreparedQuery_Create(t *testing.T) {
}

func TestPreparedQuery_List(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}

m.listFn = func(args *structs.DCSpecificRequest, reply *structs.IndexedPreparedQueries) error {
// Return an empty response.
return nil
}

body := bytes.NewBuffer(nil)
req, err := http.NewRequest("GET", "/v1/query", body)
if err != nil {
t.Fatalf("err: %v", err)
}

resp := httptest.NewRecorder()
obj, err := srv.PreparedQueryGeneral(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
t.Fatalf("bad code: %d", resp.Code)
}
r, ok := obj.(structs.PreparedQueries)
if !ok {
t.Fatalf("unexpected: %T", obj)
}
if r == nil || len(r) != 0 {
t.Fatalf("bad: %v", r)
}
})

httpTest(t, func(srv *HTTPServer) {
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
Expand Down Expand Up @@ -194,6 +228,40 @@ func TestPreparedQuery_List(t *testing.T) {
}

func TestPreparedQuery_Execute(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
t.Fatalf("err: %v", err)
}

m.executeFn = func(args *structs.PreparedQueryExecuteRequest, reply *structs.PreparedQueryExecuteResponse) error {
// Just return an empty response.
return nil
}

body := bytes.NewBuffer(nil)
req, err := http.NewRequest("GET", "/v1/query/my-id/execute", body)
if err != nil {
t.Fatalf("err: %v", err)
}

resp := httptest.NewRecorder()
obj, err := srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 200 {
t.Fatalf("bad code: %d", resp.Code)
}
r, ok := obj.(structs.PreparedQueryExecuteResponse)
if !ok {
t.Fatalf("unexpected: %T", obj)
}
if r.Nodes == nil || len(r.Nodes) != 0 {
t.Fatalf("bad: %v", r)
}
})

httpTest(t, func(srv *HTTPServer) {
m := MockPreparedQuery{}
if err := srv.agent.InjectEndpoint("PreparedQuery", &m); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion consul/state/prepared_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (s *StateStore) PreparedQueryList() (uint64, structs.PreparedQueries, error
}

// Go over all of the queries and build the response.
result := make(structs.PreparedQueries, 0)
var result structs.PreparedQueries
for query := queries.Next(); query != nil; query = queries.Next() {
result = append(result, query.(*structs.PreparedQuery))
}
Expand Down
4 changes: 2 additions & 2 deletions consul/state/prepared_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,15 @@ func TestStateStore_PreparedQueryLookup(t *testing.T) {
func TestStateStore_PreparedQueryList(t *testing.T) {
s := testStateStore(t)

// Make sure an empty (non-nil) slice is returned if there are no queries.
// Make sure nothing is returned for an empty query
idx, actual, err := s.PreparedQueryList()
if err != nil {
t.Fatalf("err: %s", err)
}
if idx != 0 {
t.Fatalf("bad index: %d", idx)
}
if actual == nil || len(actual) != 0 {
if len(actual) != 0 {
t.Fatalf("bad: %v", actual)
}

Expand Down

0 comments on commit e1ce1a3

Please sign in to comment.