forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Issue2068.groovy
194 lines (169 loc) · 6.77 KB
/
Issue2068.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package graphql
import graphql.execution.instrumentation.ChainedInstrumentation
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import graphql.schema.StaticDataFetcher
import graphql.schema.idl.RuntimeWiring
import org.apache.commons.lang3.concurrent.BasicThreadFactory
import org.dataloader.BatchLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderOptions
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification
import spock.lang.Timeout
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import java.util.concurrent.SynchronousQueue
import java.util.concurrent.ThreadFactory
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring
import static graphql.ExecutionInput.newExecutionInput
class Issue2068 extends Specification {
def "shouldn't hang on exception in resolveFieldWithInfo"() {
setup:
def sdl = """
type Nation {
name: String
}
type Toy {
name: String
}
type Owner {
nation: Nation
}
type Cat {
toys: [Toy]
}
type Dog {
owner: Owner
}
type Pets {
cats: [Cat]
dogs: [Dog]
}
type Query {
pets: Pets
}
"""
def cats = [['id': "cat-1"]]
def dogs = [['id': "dog-1"]]
ThreadFactory threadFactory = new BasicThreadFactory.Builder()
.namingPattern("resolver-chain-thread-%d").build()
def executor = new ThreadPoolExecutor(15, 15, 0L,
TimeUnit.MILLISECONDS, new SynchronousQueue<>(), threadFactory,
new ThreadPoolExecutor.CallerRunsPolicy())
DataFetcher nationsDf = { env -> env.getDataLoader("owner.nation").load(env) }
DataFetcher ownersDf = { env -> env.getDataLoader("dog.owner").load(env) }
def wiring = RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("pets", new StaticDataFetcher(['cats': cats, 'dogs': dogs])))
.type(newTypeWiring("Cat")
.dataFetcher("toys", new StaticDataFetcher(new AbstractList() {
@Override
Object get(int i) {
throw new RuntimeException("Simulated failure");
}
@Override
int size() {
return 1
}
})))
.type(newTypeWiring("Dog")
.dataFetcher("owner", ownersDf))
.type(newTypeWiring("Owner")
.dataFetcher("nation", nationsDf))
.build()
def schema = TestUtil.schema(sdl, wiring)
when:
def graphql = GraphQL.newGraphQL(schema)
.instrumentation(new DataLoaderDispatcherInstrumentation())
.build()
DataLoaderRegistry dataLoaderRegistry = mkNewDataLoaderRegistry(executor)
graphql.execute(newExecutionInput()
.dataLoaderRegistry(dataLoaderRegistry)
.query("""
query LoadPets {
pets {
cats {
toys {
name
}
}
dogs {
owner {
nation {
name
}
}
}
}
}
""")
.build())
then: "execution with single instrumentation shouldn't hang"
// wait for each future to complete and grab the results
thrown(RuntimeException)
when:
graphql = GraphQL.newGraphQL(schema)
.instrumentation(new ChainedInstrumentation(
Collections.singletonList(new DataLoaderDispatcherInstrumentation()))
)
.build()
graphql.execute(newExecutionInput()
.dataLoaderRegistry(dataLoaderRegistry)
.query("""
query LoadPets {
pets {
cats {
toys {
name
}
}
dogs {
owner {
nation {
name
}
}
}
}
}
""")
.build())
then: "execution with chained instrumentation shouldn't hang"
// wait for each future to complete and grab the results
thrown(RuntimeException)
}
private static DataLoaderRegistry mkNewDataLoaderRegistry(executor) {
def dataLoaderNations = new DataLoader<Object, Object>(new BatchLoader<DataFetchingEnvironment, List<Object>>() {
@Override
CompletionStage<List<List<Object>>> load(List<DataFetchingEnvironment> keys) {
return CompletableFuture.supplyAsync({
def nations = []
for (int i = 1; i <= 1; i++) {
nations.add(['id': "nation-$i", 'name': "nation-$i"])
}
return nations
}, executor) as CompletionStage<List<List<Object>>>
}
}, DataLoaderOptions.newOptions().setMaxBatchSize(5))
def dataLoaderOwners = new DataLoader<Object, Object>(new BatchLoader<DataFetchingEnvironment, List<Object>>() {
@Override
CompletionStage<List<List<Object>>> load(List<DataFetchingEnvironment> keys) {
return CompletableFuture.supplyAsync({
def owners = []
for (int i = 1; i <= 1; i++) {
owners.add(['id': "owner-$i"])
}
return owners
}, executor) as CompletionStage<List<List<Object>>>
}
}, DataLoaderOptions.newOptions().setMaxBatchSize(5))
def dataLoaderRegistry = new DataLoaderRegistry()
dataLoaderRegistry.register("dog.owner", dataLoaderOwners)
dataLoaderRegistry.register("owner.nation", dataLoaderNations)
dataLoaderRegistry
}
}