Skip to content

Commit

Permalink
Add paralleltest linter, parallelize all tests (#175)
Browse files Browse the repository at this point in the history
Add paralleltest linter, parallelize all tests
  • Loading branch information
JohnStarich authored Jul 2, 2022
1 parent e5a13fc commit 5d498d1
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ linters:
- revive
- unconvert
- unparam
- paralleltest

issues:
exclude-rules:
Expand Down
6 changes: 6 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (p *testPlannerCounter) Plan(*PlanningContext) (QueryPlanList, error) {
}

func TestCacheOptions(t *testing.T) {
t.Parallel()
// turn the combo into a remote schema
schema, _ := graphql.LoadSchema(`
type Query {
Expand All @@ -48,6 +49,7 @@ func TestCacheOptions(t *testing.T) {
}

func TestNoQueryPlanCache(t *testing.T) {
t.Parallel()
cacheKey := "asdf"
// the plan we are expecting back
plans := QueryPlanList{}
Expand Down Expand Up @@ -77,6 +79,7 @@ func TestNoQueryPlanCache(t *testing.T) {
}

func TestAutomaticQueryPlanCache(t *testing.T) {
t.Parallel()
cacheKey := "asdf"
// the plan we are expecting back
plans := QueryPlanList{}
Expand Down Expand Up @@ -115,6 +118,7 @@ func TestAutomaticQueryPlanCache(t *testing.T) {
}

func TestAutomaticQueryPlanCache_passPlannerErrors(t *testing.T) {
t.Parallel()
cacheKey := "asdf"
// instantiate a planner that can count how many times it was invoked
planner := &MockErrPlanner{errors.New("Error")}
Expand All @@ -130,6 +134,7 @@ func TestAutomaticQueryPlanCache_passPlannerErrors(t *testing.T) {
}

func TestAutomaticQueryPlanCache_setCacheKey(t *testing.T) {
t.Parallel()
// instantiate a planner that can count how many times it was invoked
planner := &testPlannerCounter{
Plans: QueryPlanList{},
Expand All @@ -153,6 +158,7 @@ func TestAutomaticQueryPlanCache_setCacheKey(t *testing.T) {
}

func TestAutomaticQueryPlanCache_garbageCollection(t *testing.T) {
t.Parallel()
cacheKey := "asdf"
// the plan we are expecting back
plans := QueryPlanList{}
Expand Down
28 changes: 28 additions & 0 deletions execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
}

func TestExecutor_plansOfOne(t *testing.T) {
t.Parallel()
// build a query plan that the executor will follow
result, err := (&ParallelExecutor{}).Execute(&ExecutionContext{
logger: &DefaultLogger{},
Expand Down Expand Up @@ -70,6 +71,7 @@ func TestExecutor_plansOfOne(t *testing.T) {
}

func TestExecutor_plansWithDependencies(t *testing.T) {
t.Parallel()
// the query we want to execute is
// {
// user { <- from serviceA
Expand Down Expand Up @@ -169,6 +171,7 @@ func TestExecutor_plansWithDependencies(t *testing.T) {
}

func TestExecutor_emptyPlansWithDependencies(t *testing.T) {
t.Parallel()
// the query we want to execute is
// {
// user { <- from serviceA
Expand Down Expand Up @@ -239,6 +242,7 @@ func TestExecutor_emptyPlansWithDependencies(t *testing.T) {
}

func TestExecutor_insertIntoFragmentSpread(t *testing.T) {
t.Parallel()
// the query we want to execute is
// {
// photo { <- Query.services @ serviceA
Expand Down Expand Up @@ -367,6 +371,7 @@ func TestExecutor_insertIntoFragmentSpread(t *testing.T) {
}

func TestExecutor_insertIntoListFragmentSpreads(t *testing.T) {
t.Parallel()
// {
// photos { <-- Query.services @ serviceA, list
// ...photosFragment
Expand Down Expand Up @@ -509,6 +514,7 @@ func TestExecutor_insertIntoListFragmentSpreads(t *testing.T) {
}

func TestExecutor_insertIntoFragmentSpreadLists(t *testing.T) {
t.Parallel()
// {
// photo { <-- Query.services @ serviceA
// ...photoFragment
Expand Down Expand Up @@ -647,6 +653,7 @@ func TestExecutor_insertIntoFragmentSpreadLists(t *testing.T) {
}

func TestExecutor_insertIntoInlineFragment(t *testing.T) {
t.Parallel()
// the query we want to execute is
// {
// photo { <- Query.services @ serviceA
Expand Down Expand Up @@ -756,6 +763,7 @@ func TestExecutor_insertIntoInlineFragment(t *testing.T) {
}

func TestExecutor_insertIntoListInlineFragments(t *testing.T) {
t.Parallel()
// {
// photos { <-- Query.services @ serviceA, list
// ... on Photo {
Expand Down Expand Up @@ -878,6 +886,7 @@ func TestExecutor_insertIntoListInlineFragments(t *testing.T) {
}

func TestExecutor_insertIntoInlineFragmentsList(t *testing.T) {
t.Parallel()
// {
// photo { <-- Query.services @ serviceA
// ... on Photo {
Expand Down Expand Up @@ -997,6 +1006,7 @@ func TestExecutor_insertIntoInlineFragmentsList(t *testing.T) {
}

func TestExecutor_insertIntoLists(t *testing.T) {
t.Parallel()
// the query we want to execute is
// {
// users { <- Query.services @ serviceA
Expand Down Expand Up @@ -1255,6 +1265,7 @@ func TestExecutor_insertIntoLists(t *testing.T) {
}

func TestExecutor_multipleErrors(t *testing.T) {
t.Parallel()
// an executor should return a list of every error that it encounters while executing the plan

// build a query plan that the executor will follow
Expand Down Expand Up @@ -1324,6 +1335,7 @@ func TestExecutor_multipleErrors(t *testing.T) {
}

func TestExecutor_includeIf(t *testing.T) {
t.Parallel()

// the query we want to execute is
// {
Expand Down Expand Up @@ -1423,6 +1435,7 @@ func TestExecutor_includeIf(t *testing.T) {
}

func TestExecutor_appliesRequestMiddlewares(t *testing.T) {
t.Parallel()
schema, _ := graphql.LoadSchema(
`
type Query {
Expand Down Expand Up @@ -1525,6 +1538,7 @@ func TestExecutor_appliesRequestMiddlewares(t *testing.T) {
}

func TestExecutor_threadsVariables(t *testing.T) {
t.Parallel()
// the variables we'll be threading through
fullVariables := map[string]interface{}{
"hello": "world",
Expand Down Expand Up @@ -1601,6 +1615,7 @@ func TestExecutor_threadsVariables(t *testing.T) {
}
}
func TestFindInsertionPoint_rootList(t *testing.T) {
t.Parallel()
// in this example, the step before would have just resolved (need to be inserted at)
// ["users", "photoGallery"]. There would be an id field underneath each photo in the list
// of users.photoGallery
Expand Down Expand Up @@ -1723,6 +1738,7 @@ func TestFindInsertionPoint_rootList(t *testing.T) {
}

func TestFindObject(t *testing.T) {
t.Parallel()
// create an object we want to extract
source := map[string]interface{}{
"hello": []interface{}{
Expand Down Expand Up @@ -1783,6 +1799,7 @@ func TestFindObject(t *testing.T) {
}

func TestFindString(t *testing.T) {
t.Parallel()
// create an object we want to extract
source := map[string]interface{}{
"hello": []interface{}{
Expand Down Expand Up @@ -1821,6 +1838,7 @@ func TestFindString(t *testing.T) {
}

func TestExecutorInsertObject_insertObjectValues(t *testing.T) {
t.Parallel()
// the object to mutate
source := map[string]interface{}{}

Expand Down Expand Up @@ -1898,6 +1916,7 @@ func TestExecutorInsertObject_insertObjectValues(t *testing.T) {
}

func TestExecutorInsertObject_insertListElements(t *testing.T) {
t.Parallel()
// the object to mutate
source := map[string]interface{}{}

Expand Down Expand Up @@ -1949,6 +1968,7 @@ func TestExecutorInsertObject_insertListElements(t *testing.T) {
}

func TestExecutorGetPointData(t *testing.T) {
t.Parallel()
table := []struct {
point string
data *extractorPointData
Expand All @@ -1961,7 +1981,9 @@ func TestExecutorGetPointData(t *testing.T) {
}

for _, row := range table {
row := row // enable parallel sub-tests
t.Run(row.point, func(t *testing.T) {
t.Parallel()
pointData, err := executorGetPointData(row.point)
if err != nil {
t.Error(err.Error())
Expand All @@ -1974,6 +1996,7 @@ func TestExecutorGetPointData(t *testing.T) {
}

func TestFindInsertionPoint_bailOnNil(t *testing.T) {
t.Parallel()
// we want the list of insertion points that point to
planInsertionPoint := []string{"post", "author"}
expected := [][]string{}
Expand Down Expand Up @@ -2012,6 +2035,7 @@ func TestFindInsertionPoint_bailOnNil(t *testing.T) {
}

func TestFindInsertionPoint_stitchIntoObject(t *testing.T) {
t.Parallel()
// we want the list of insertion points that point to
planInsertionPoint := []string{"users", "photoGallery", "author"}

Expand Down Expand Up @@ -2087,11 +2111,14 @@ func TestFindInsertionPoint_stitchIntoObject(t *testing.T) {
assert.Equal(t, finalInsertionPoint, generatedPoint)
}

/* TODO
func TestFindInsertionPoint_handlesNullObjects(t *testing.T) {
t.Skip("Not yet implemented")
}
*/

func TestSingleObjectWithColonInID(t *testing.T) {
t.Parallel()
var source = make(map[string]interface{})
_ = json.Unmarshal([]byte(
`{"hello": {"id": "Thing:1337", "firstName": "Foo", "lastName": "bar"}}`),
Expand All @@ -2113,6 +2140,7 @@ func TestSingleObjectWithColonInID(t *testing.T) {
// Test provides quite deep query to illustrate problem with
// reused memory during slice `appends`
func TestExecutor_plansWithManyDeepDependencies(t *testing.T) {
t.Parallel()

// the query we want to execute is
// {
Expand Down
16 changes: 16 additions & 0 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type schemaTableRow struct {
}

func TestGateway(t *testing.T) {
t.Parallel()
schemas := []schemaTableRow{
{
"url1",
Expand Down Expand Up @@ -53,6 +54,7 @@ func TestGateway(t *testing.T) {
}

t.Run("Compute Field URLs", func(t *testing.T) {
t.Parallel()
locations := fieldURLs(sources, false)

allUsersURL, err := locations.URLFor(typeNameQuery, "allUsers")
Expand Down Expand Up @@ -82,6 +84,7 @@ func TestGateway(t *testing.T) {
})

t.Run("Options", func(t *testing.T) {
t.Parallel()
// create a new schema with the sources and some configuration
gateway, err := New([]*graphql.RemoteSchema{sources[0]}, func(schema *Gateway) {
schema.sources = append(schema.sources, sources[1])
Expand All @@ -97,6 +100,7 @@ func TestGateway(t *testing.T) {
})

t.Run("WithPlanner", func(t *testing.T) {
t.Parallel()
// the planner we will assign
planner := &MockPlanner{}

Expand All @@ -110,6 +114,7 @@ func TestGateway(t *testing.T) {
})

t.Run("WithQueryerFactory", func(t *testing.T) {
t.Parallel()
// the planner we will assign
planner := &MinQueriesPlanner{}

Expand All @@ -128,6 +133,7 @@ func TestGateway(t *testing.T) {
})

t.Run("WithLocationPriorities", func(t *testing.T) {
t.Parallel()
priorities := []string{"url1", "url2"}

gateway, err := New(sources, WithLocationPriorities(priorities))
Expand All @@ -140,6 +146,7 @@ func TestGateway(t *testing.T) {
})

t.Run("WithLogger", func(t *testing.T) {
t.Parallel()
logger := &DefaultLogger{}

go func() { // verify no race condition between Gateway instances (singleton): https://github.com/nautilus/gateway/issues/154
Expand All @@ -159,6 +166,7 @@ func TestGateway(t *testing.T) {
})

t.Run("fieldURLs ignore introspection", func(t *testing.T) {
t.Parallel()
locations := fieldURLs(sources, true)

for key := range locations {
Expand All @@ -174,6 +182,7 @@ func TestGateway(t *testing.T) {
})

t.Run("Response Middleware Error", func(t *testing.T) {
t.Parallel()
// create a new schema with the sources and some configuration
gateway, err := New(sources,
WithExecutor(ExecutorFunc(func(ctx *ExecutionContext) (map[string]interface{}, error) {
Expand Down Expand Up @@ -206,6 +215,7 @@ func TestGateway(t *testing.T) {
})

t.Run("Response Middleware Error Empty Data", func(t *testing.T) {
t.Parallel()
// create a new schema with the sources and some configuration
gateway, err := New(sources,
WithExecutor(ExecutorFunc(func(ctx *ExecutionContext) (map[string]interface{}, error) {
Expand Down Expand Up @@ -239,6 +249,7 @@ func TestGateway(t *testing.T) {
})

t.Run("Response Middleware Success", func(t *testing.T) {
t.Parallel()
// create a new schema with the sources and some configuration
gateway, err := New(sources,
WithExecutor(ExecutorFunc(func(ctx *ExecutionContext) (map[string]interface{}, error) {
Expand Down Expand Up @@ -285,6 +296,7 @@ func TestGateway(t *testing.T) {
})

t.Run("filter out automatically inserted ids", func(t *testing.T) {
t.Parallel()
// the query we're going to fire. Query.allUsers comes from service one. User.lastName
// from service two.
query := `
Expand Down Expand Up @@ -406,6 +418,7 @@ func TestGateway(t *testing.T) {
})

t.Run("Introspection field on services", func(t *testing.T) {
t.Parallel()
// compute the location of each field
locations := fieldURLs(sources, false)

Expand All @@ -416,6 +429,7 @@ func TestGateway(t *testing.T) {
})

t.Run("Gateway fields", func(t *testing.T) {
t.Parallel()
// define a gateway field
viewerField := &QueryField{
Name: "viewer",
Expand Down Expand Up @@ -497,6 +511,7 @@ func TestGateway(t *testing.T) {
}

func TestGatewayExecuteRespectsOperationName(t *testing.T) {
t.Parallel()
// define a schema source
schema, _ := graphql.LoadSchema(`
type Query {
Expand Down Expand Up @@ -631,6 +646,7 @@ func TestGatewayExecuteRespectsOperationName(t *testing.T) {
}

func TestFieldURLs_concat(t *testing.T) {
t.Parallel()
// create a field url map
first := FieldURLMap{}
first.RegisterURL("Parent", "field1", "url1")
Expand Down
Loading

0 comments on commit 5d498d1

Please sign in to comment.