From d047c753e63f765061f12645a296192538ccef96 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Tue, 2 Oct 2018 14:49:17 +1000 Subject: [PATCH] Add a stress test on query cache --- handler/graphql_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/handler/graphql_test.go b/handler/graphql_test.go index 685c9b994a8..aa60f4a2f61 100644 --- a/handler/graphql_test.go +++ b/handler/graphql_test.go @@ -18,6 +18,30 @@ func TestHandlerPOST(t *testing.T) { assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String()) }) + t.Run("query caching", func(t *testing.T) { + // Run enough unique queries to evict a bunch of them + for i := 0; i < 2000; i++ { + query := `{"query":"` + strings.Repeat(" ", i) + "{ me { name } }" + `"}` + resp := doRequest(h, "POST", "/graphql", query) + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String()) + } + + t.Run("evicted queries run", func(t *testing.T) { + query := `{"query":"` + strings.Repeat(" ", 0) + "{ me { name } }" + `"}` + resp := doRequest(h, "POST", "/graphql", query) + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String()) + }) + + t.Run("non-evicted queries run", func(t *testing.T) { + query := `{"query":"` + strings.Repeat(" ", 1999) + "{ me { name } }" + `"}` + resp := doRequest(h, "POST", "/graphql", query) + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String()) + }) + }) + t.Run("decode failure", func(t *testing.T) { resp := doRequest(h, "POST", "/graphql", "notjson") assert.Equal(t, http.StatusBadRequest, resp.Code)