-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsyncExecutionStrategyTest.groovy
87 lines (75 loc) · 2.36 KB
/
AsyncExecutionStrategyTest.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
package graphql.execution
import graphql.ExecutionResult
import graphql.NewsSchema
import graphql.async.GraphQL
import spock.lang.Specification
import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicReference
import java.util.function.Consumer
class AsyncExecutionStrategyTest extends Specification {
def 'Example usage of AsyncExecutionStrategy.'() {
given:
def query = """
query receive {
news {
article {
text
}
}
}
"""
def mutation = """
mutation publish {
news {
article (text: "Hello World") {
text
}
}
}
"""
def expected = [
news: [
article: [
text: 'Hello World'
]
]
]
when:
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>() {
@Override
public boolean offer(Runnable e) {
/* queue that always rejects tasks */
return false;
}
};
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2, /* core pool size 2 thread */
2, /* max pool size 2 thread */
30, TimeUnit.SECONDS,
/*
* Do not use the queue to prevent threads waiting on enqueued tasks.
*/
queue,
/*
* If all the threads are working, then the caller thread
* should execute the code in its own thread. (serially)
*/
new ThreadPoolExecutor.CallerRunsPolicy());
def graphQL = GraphQL.newAsyncGraphQL(NewsSchema.newsSchema)
.build()
def received = new AtomicReference<Map>()
def latch = new CountDownLatch(1)
graphQL.execute(query, new Consumer<ExecutionResult>() {
@Override
void accept(ExecutionResult executionResult) {
received.set(executionResult.data)
latch.countDown()
}
})
def published = graphQL.execute(mutation)
latch.await()
def result = received.get()
then:
result == expected
}
}