All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.io.rsocket.kotlin.SetupRejectionTest.kt Maven / Gradle / Ivy

There is a newer version: 0.16.0
Show newest version
/*
 * Copyright 2015-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.rsocket.kotlin

import io.rsocket.kotlin.core.*
import io.rsocket.kotlin.frame.*
import io.rsocket.kotlin.frame.io.*
import io.rsocket.kotlin.keepalive.*
import io.rsocket.kotlin.payload.*
import io.rsocket.kotlin.test.*
import io.rsocket.kotlin.transport.*
import kotlinx.coroutines.*
import kotlin.test.*

class SetupRejectionTest : SuspendTest, TestWithLeakCheck {
    @Test
    fun responderRejectSetup() = test {
        val errorMessage = "error"
        val sendingRSocket = CompletableDeferred()

        val connection = TestConnection()

        val serverTransport = ServerTransport { accept ->
            GlobalScope.async { accept(connection) }
        }

        val deferred = RSocketServer().bind(serverTransport) {
            sendingRSocket.complete(requester)
            error(errorMessage)
        }

        connection.sendToReceiver(SetupFrame(Version.Current, false, DefaultKeepAlive, null, DefaultPayloadMimeType, Payload.Empty))

        assertFailsWith(RSocketError.Setup.Rejected::class, errorMessage) { deferred.await() }

        connection.test {
            expectFrame { frame ->
                assertTrue(frame is ErrorFrame)
                assertTrue(frame.throwable is RSocketError.Setup.Rejected)
                assertEquals(errorMessage, frame.throwable.message)
                assertEquals(errorMessage, frame.data?.readText())
            }
            val sender = sendingRSocket.await()
            assertFalse(sender.job.isActive)
            expectNoEventsIn(100)
        }
        val error = connection.job.getCancellationException().cause
        assertTrue(error is RSocketError.Setup.Rejected)
        assertEquals(errorMessage, error.message)
    }

//    @Test
//    fun requesterStreamsTerminatedOnZeroErrorFrame() = test {
//        val errorMessage = "error"
//        val connection = TestConnection()
//        val requester = RSocketRequester(connection, StreamId.client(), KeepAlive(), RequestStrategy.Default, {})
//        val deferred = GlobalScope.async { requester.requestResponse(Payload.Empty) }
//        delay(100)
//        connection.sendToReceiver(ErrorFrame(0, RSocketError.ConnectionError(errorMessage)))
//        assertFailsWith(errorMessage) { deferred.await() }
//    }
//
//    @Test
//    fun requesterNewStreamsTerminatedAfterZeroErrorFrame() = test {
//        val errorMessage = "error"
//        val connection = TestConnection()
//        val requester = RSocketRequester(connection, StreamId.client(), KeepAlive(), RequestStrategy.Default, {})
//        connection.sendToReceiver(ErrorFrame(0, RSocketError.ConnectionError(errorMessage)))
//        assertFailsWith(errorMessage) { requester.requestResponse(Payload.Empty) }
//    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy