|
| 1 | +/* |
| 2 | + * Copyright 2020 Expedia, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.expediagroup.graphql.execution |
| 18 | + |
| 19 | +import com.expediagroup.graphql.SchemaGeneratorConfig |
| 20 | +import com.expediagroup.graphql.TopLevelObject |
| 21 | +import com.expediagroup.graphql.annotations.GraphQLContext |
| 22 | +import com.expediagroup.graphql.exceptions.GraphQLKotlinException |
| 23 | +import com.expediagroup.graphql.toSchema |
| 24 | +import graphql.ExecutionInput |
| 25 | +import graphql.ExecutionResult |
| 26 | +import graphql.ExecutionResultImpl |
| 27 | +import graphql.GraphQL |
| 28 | +import graphql.GraphQLError |
| 29 | +import graphql.GraphqlErrorBuilder |
| 30 | +import graphql.schema.GraphQLSchema |
| 31 | +import kotlinx.coroutines.InternalCoroutinesApi |
| 32 | +import kotlinx.coroutines.delay |
| 33 | +import kotlinx.coroutines.flow.Flow |
| 34 | +import kotlinx.coroutines.flow.catch |
| 35 | +import kotlinx.coroutines.flow.collect |
| 36 | +import kotlinx.coroutines.flow.flow |
| 37 | +import kotlinx.coroutines.flow.onEach |
| 38 | +import kotlinx.coroutines.runBlocking |
| 39 | +import org.junit.jupiter.api.Test |
| 40 | +import kotlin.test.assertEquals |
| 41 | +import kotlin.test.assertNull |
| 42 | +import kotlin.test.assertTrue |
| 43 | + |
| 44 | +@InternalCoroutinesApi |
| 45 | +class FlowSubscriptionExecutionStrategyTest { |
| 46 | + |
| 47 | + private val testSchema: GraphQLSchema = toSchema( |
| 48 | + config = SchemaGeneratorConfig(supportedPackages = listOf("com.expediagroup.graphql.spring.execution")), |
| 49 | + queries = listOf(TopLevelObject(BasicQuery())), |
| 50 | + subscriptions = listOf(TopLevelObject(FlowSubscription())) |
| 51 | + ) |
| 52 | + private val testGraphQL: GraphQL = GraphQL.newGraphQL(testSchema).subscriptionExecutionStrategy(FlowSubscriptionExecutionStrategy()).build() |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `verify subscription`() = runBlocking { |
| 56 | + val request = ExecutionInput.newExecutionInput().query("subscription { ticker }").build() |
| 57 | + val response = testGraphQL.execute(request) |
| 58 | + val flow = response.getData<Flow<ExecutionResult>>() |
| 59 | + val list = mutableListOf<Int>() |
| 60 | + flow.collect { |
| 61 | + list.add(it.getData<Map<String, Int>>().getValue("ticker")) |
| 62 | + } |
| 63 | + assertEquals(5, list.size) |
| 64 | + for (i in list.indices) { |
| 65 | + assertEquals(i + 1, list[i]) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `verify subscription with context`() = runBlocking { |
| 71 | + val request = ExecutionInput.newExecutionInput() |
| 72 | + .query("subscription { contextualTicker }") |
| 73 | + .context(SubscriptionContext("junitHandler")) |
| 74 | + .build() |
| 75 | + val response = testGraphQL.execute(request) |
| 76 | + val flow = response.getData<Flow<ExecutionResult>>() |
| 77 | + val list = mutableListOf<Int>() |
| 78 | + flow.collect { |
| 79 | + val contextValue = it.getData<Map<String, String>>().getValue("contextualTicker") |
| 80 | + assertTrue(contextValue.startsWith("junitHandler:")) |
| 81 | + list.add(contextValue.substringAfter("junitHandler:").toInt()) |
| 82 | + } |
| 83 | + assertEquals(5, list.size) |
| 84 | + for (i in list.indices) { |
| 85 | + assertEquals(i + 1, list[i]) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `verify subscription to failing publisher`() = runBlocking { |
| 91 | + val request = ExecutionInput.newExecutionInput().query("subscription { alwaysThrows }").build() |
| 92 | + val response = testGraphQL.execute(request) |
| 93 | + val flow = response.getData<Flow<ExecutionResult>>() |
| 94 | + val errors = mutableListOf<GraphQLError>() |
| 95 | + val results = mutableListOf<Int>() |
| 96 | + flow.onEach { |
| 97 | + val dataMap = it.getData<Map<String, Int>>() |
| 98 | + if (dataMap != null) { |
| 99 | + results.add(dataMap.getValue("alwaysThrows")) |
| 100 | + } |
| 101 | + errors.addAll(it.errors) |
| 102 | + }.catch { |
| 103 | + errors.add(GraphqlErrorBuilder.newError().message(it.message).build()) |
| 104 | + }.collect() |
| 105 | + assertEquals(2, results.size) |
| 106 | + for (i in results.indices) { |
| 107 | + assertEquals(i + 1, results[i]) |
| 108 | + } |
| 109 | + assertEquals(1, errors.size) |
| 110 | + assertEquals("JUNIT subscription failure", errors[0].message) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun `verify subscription to exploding publisher`() = runBlocking { |
| 115 | + val request = ExecutionInput.newExecutionInput().query("subscription { throwsFast }").build() |
| 116 | + val response = testGraphQL.execute(request) |
| 117 | + val flow = response.getData<Flow<ExecutionResult>>() |
| 118 | + val errors = response.errors |
| 119 | + assertNull(flow) |
| 120 | + assertEquals(1, errors.size) |
| 121 | + assertEquals("JUNIT flow failure", errors[0].message.substringAfter(" : ")) |
| 122 | + } |
| 123 | + |
| 124 | + // GraphQL spec requires at least single query to be present as Query type is needed to run introspection queries |
| 125 | + // see: https://github.com/graphql/graphql-spec/issues/490 and https://github.com/graphql/graphql-spec/issues/568 |
| 126 | + class BasicQuery { |
| 127 | + @Suppress("Detekt.FunctionOnlyReturningConstant") |
| 128 | + fun query(): String = "hello" |
| 129 | + } |
| 130 | + |
| 131 | + class FlowSubscription { |
| 132 | + fun ticker(): Flow<Int> { |
| 133 | + return flow { |
| 134 | + for (i in 1..5) { |
| 135 | + delay(100) |
| 136 | + emit(i) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + fun throwsFast(): Flow<Int> { |
| 142 | + throw GraphQLKotlinException("JUNIT flow failure") |
| 143 | + } |
| 144 | + |
| 145 | + fun alwaysThrows(): Flow<Int> { |
| 146 | + return flow { |
| 147 | + for (i in 1..5) { |
| 148 | + if (i > 2) { |
| 149 | + throw GraphQLKotlinException("JUNIT subscription failure") |
| 150 | + } |
| 151 | + delay(100) |
| 152 | + emit(i) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + fun contextualTicker(@GraphQLContext context: SubscriptionContext): Flow<String> { |
| 158 | + return flow { |
| 159 | + for (i in 1..5) { |
| 160 | + delay(100) |
| 161 | + emit("${context.value}:$i") |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + data class SubscriptionContext(val value: String) |
| 168 | +} |
0 commit comments