|
| 1 | +/* |
| 2 | + * Copyright 2015-2020 the original author or authors. |
| 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 | + * http://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 io.rsocket.kotlin.internal |
| 18 | + |
| 19 | +import app.cash.turbine.FlowTurbine |
| 20 | +import io.rsocket.kotlin.* |
| 21 | +import io.rsocket.kotlin.frame.* |
| 22 | +import io.rsocket.kotlin.frame.io.Version |
| 23 | +import io.rsocket.kotlin.keepalive.DefaultKeepAlive |
| 24 | +import io.rsocket.kotlin.payload.DefaultPayloadMimeType |
| 25 | +import io.rsocket.kotlin.payload.buildPayload |
| 26 | +import io.rsocket.kotlin.payload.data |
| 27 | +import io.rsocket.kotlin.test.TestExceptionHandler |
| 28 | +import io.rsocket.kotlin.test.TestServer |
| 29 | +import io.rsocket.kotlin.test.TestWithLeakCheck |
| 30 | +import io.rsocket.kotlin.test.payload |
| 31 | +import io.rsocket.kotlin.transport.ServerTransport |
| 32 | +import kotlinx.coroutines.* |
| 33 | +import kotlinx.coroutines.channels.Channel |
| 34 | +import kotlinx.coroutines.flow.asFlow |
| 35 | +import kotlinx.coroutines.flow.map |
| 36 | +import kotlinx.coroutines.flow.onEach |
| 37 | +import kotlin.test.Test |
| 38 | +import kotlin.test.assertEquals |
| 39 | +import kotlin.test.assertTrue |
| 40 | +import kotlin.time.Duration.Companion.seconds |
| 41 | + |
| 42 | +class RSocketResponderRequestNTest : TestWithLeakCheck, TestWithConnection() { |
| 43 | + private val testJob: Job = Job() |
| 44 | + |
| 45 | + private suspend fun start(handler: RSocket) { |
| 46 | + val serverTransport = ServerTransport { accept -> |
| 47 | + GlobalScope.async { accept(connection) } |
| 48 | + } |
| 49 | + |
| 50 | + val scope = CoroutineScope(Dispatchers.Unconfined + testJob + TestExceptionHandler) |
| 51 | + @Suppress("DeferredResultUnused") |
| 52 | + TestServer().bindIn(scope, serverTransport) { |
| 53 | + config.setupPayload.close() |
| 54 | + handler |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + override suspend fun after() { |
| 59 | + super.after() |
| 60 | + testJob.cancelAndJoin() |
| 61 | + } |
| 62 | + |
| 63 | + private val setupFrame |
| 64 | + get() = SetupFrame( |
| 65 | + version = Version.Current, |
| 66 | + honorLease = false, |
| 67 | + keepAlive = DefaultKeepAlive, |
| 68 | + resumeToken = null, |
| 69 | + payloadMimeType = DefaultPayloadMimeType, |
| 70 | + payload = payload("setup"), |
| 71 | + ) |
| 72 | + |
| 73 | + @Test |
| 74 | + fun testStreamInitialEnoughToConsume() = test { |
| 75 | + start( |
| 76 | + RSocketRequestHandler { |
| 77 | + requestStream { payload -> |
| 78 | + payload.close() |
| 79 | + (0..9).asFlow().map { buildPayload { data("$it") } } |
| 80 | + } |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + connection.test { |
| 85 | + connection.sendToReceiver(setupFrame) |
| 86 | + |
| 87 | + connection.sendToReceiver(RequestStreamFrame(initialRequestN = 16, streamId = 1, payload = payload("request"))) |
| 88 | + |
| 89 | + awaitAndReleasePayloadFrames(amount = 10) |
| 90 | + awaitCompleteFrame() |
| 91 | + expectNoEventsIn(200) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun testStreamSuspendWhenNoRequestsLeft() = test { |
| 97 | + var lastSent = -1 |
| 98 | + start( |
| 99 | + RSocketRequestHandler { |
| 100 | + requestStream { payload -> |
| 101 | + payload.close() |
| 102 | + (0..9).asFlow() |
| 103 | + .onEach { lastSent = it } |
| 104 | + .map { buildPayload { data("$it") } } |
| 105 | + } |
| 106 | + } |
| 107 | + ) |
| 108 | + |
| 109 | + connection.test { |
| 110 | + connection.sendToReceiver(setupFrame) |
| 111 | + |
| 112 | + connection.sendToReceiver(RequestStreamFrame(initialRequestN = 3, streamId = 1, payload = payload("request"))) |
| 113 | + |
| 114 | + awaitAndReleasePayloadFrames(amount = 3) |
| 115 | + expectNoEventsIn(200) |
| 116 | + assertEquals(3, lastSent) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun testStreamRequestNFrameResumesOperation() = test { |
| 122 | + start( |
| 123 | + RSocketRequestHandler { |
| 124 | + requestStream { payload -> |
| 125 | + payload.close() |
| 126 | + (0..15).asFlow().map { buildPayload { data("$it") } } |
| 127 | + } |
| 128 | + } |
| 129 | + ) |
| 130 | + connection.test { |
| 131 | + connection.sendToReceiver(setupFrame) |
| 132 | + |
| 133 | + connection.sendToReceiver(RequestStreamFrame(initialRequestN = 3, streamId = 1, payload = payload("request"))) |
| 134 | + awaitAndReleasePayloadFrames(amount = 3) |
| 135 | + expectNoEventsIn(200) |
| 136 | + |
| 137 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = 5)) |
| 138 | + awaitAndReleasePayloadFrames(amount = 5) |
| 139 | + expectNoEventsIn(200) |
| 140 | + |
| 141 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = 5)) |
| 142 | + awaitAndReleasePayloadFrames(amount = 5) |
| 143 | + expectNoEventsIn(200) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun testStreamRequestNEnoughToComplete() = test { |
| 149 | + val total = 20 |
| 150 | + start( |
| 151 | + RSocketRequestHandler { |
| 152 | + requestStream { payload -> |
| 153 | + payload.close() |
| 154 | + (0 until total).asFlow().map { buildPayload { data("$it") } } |
| 155 | + } |
| 156 | + } |
| 157 | + ) |
| 158 | + connection.test { |
| 159 | + connection.sendToReceiver(setupFrame) |
| 160 | + |
| 161 | + val firstRequest = 3 |
| 162 | + connection.sendToReceiver(RequestStreamFrame(initialRequestN = firstRequest, streamId = 1, payload = payload("request"))) |
| 163 | + awaitAndReleasePayloadFrames(amount = firstRequest) |
| 164 | + expectNoEventsIn(200) |
| 165 | + |
| 166 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = Int.MAX_VALUE)) |
| 167 | + awaitAndReleasePayloadFrames(amount = total - firstRequest) |
| 168 | + awaitCompleteFrame() |
| 169 | + expectNoEventsIn(200) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + fun testStreamRequestNAttemptedIntOverflow() = test { |
| 175 | + val latch = Channel<Unit>(1) |
| 176 | + start( |
| 177 | + RSocketRequestHandler { |
| 178 | + requestStream { payload -> |
| 179 | + payload.close() |
| 180 | + latch.receive() |
| 181 | + // make sure limiter has got the RequestNFrame before emitting the values |
| 182 | + delay(200) |
| 183 | + (0..19).asFlow().map { buildPayload { data("$it") } } |
| 184 | + } |
| 185 | + } |
| 186 | + ) |
| 187 | + connection.test { |
| 188 | + connection.sendToReceiver(setupFrame) |
| 189 | + |
| 190 | + connection.sendToReceiver(RequestStreamFrame(initialRequestN = Int.MAX_VALUE, streamId = 1, payload = payload("request"))) |
| 191 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = Int.MAX_VALUE)) |
| 192 | + latch.send(Unit) |
| 193 | + |
| 194 | + awaitAndReleasePayloadFrames(amount = 20) |
| 195 | + awaitCompleteFrame() |
| 196 | + expectNoEventsIn(200) |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + @Test |
| 202 | + fun testStreamRequestNSummingUpToOverflow() = test { |
| 203 | + val latch = Channel<Unit>(1) |
| 204 | + start( |
| 205 | + RSocketRequestHandler { |
| 206 | + requestStream { payload -> |
| 207 | + payload.close() |
| 208 | + latch.receive() |
| 209 | + // make sure limiter has got the RequestNFrame before emitting the values |
| 210 | + delay(200) |
| 211 | + (0..19).asFlow().map { buildPayload { data("$it") } } |
| 212 | + } |
| 213 | + } |
| 214 | + ) |
| 215 | + |
| 216 | + connection.test { |
| 217 | + connection.sendToReceiver(setupFrame) |
| 218 | + |
| 219 | + connection.sendToReceiver(RequestStreamFrame(initialRequestN = 5, streamId = 1, payload = payload("request"))) |
| 220 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = Int.MAX_VALUE / 3)) |
| 221 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = Int.MAX_VALUE / 3)) |
| 222 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = Int.MAX_VALUE / 3)) |
| 223 | + connection.sendToReceiver(RequestNFrame(streamId = 1, requestN = Int.MAX_VALUE / 3)) |
| 224 | + latch.send(Unit) |
| 225 | + |
| 226 | + awaitAndReleasePayloadFrames(amount = 20) |
| 227 | + awaitCompleteFrame() |
| 228 | + expectNoEventsIn(200) |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private suspend fun FlowTurbine<Frame>.awaitAndReleasePayloadFrames(amount: Int) { |
| 233 | + repeat(amount) { |
| 234 | + awaitFrame { frame -> |
| 235 | + assertTrue(frame is RequestFrame) |
| 236 | + assertEquals(FrameType.Payload, frame.type) |
| 237 | + frame.payload.close() |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + private suspend fun FlowTurbine<Frame>.awaitCompleteFrame() { |
| 243 | + awaitFrame { frame -> |
| 244 | + assertTrue(frame is RequestFrame) |
| 245 | + assertEquals(FrameType.Payload, frame.type) |
| 246 | + assertTrue(frame.complete, "Frame should be complete") |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments