From 3e7afe6b2ead4929b8c4b16c7a52f0337a75c62a Mon Sep 17 00:00:00 2001 From: Artemyev Vyacheslav Date: Thu, 2 Nov 2023 15:43:27 +0300 Subject: [PATCH] Remove useless testss --- build.gradle | 4 +- .../publisher/ConfirmPublisher.kt | 9 +-- .../publisher/ConfirmPublisherTest.kt | 66 ------------------- .../publisher/PublisherIntegrationTest.kt | 53 +++++++-------- .../example/PublisherExample.kt | 13 ++-- 5 files changed, 35 insertions(+), 110 deletions(-) delete mode 100644 src/test/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisherTest.kt diff --git a/build.gradle b/build.gradle index 29bb7755..f61dee9f 100644 --- a/build.gradle +++ b/build.gradle @@ -76,8 +76,10 @@ configure(rootProject) { testImplementation("org.apache.httpcomponents.client5:httpclient5:$httpclient_version") testImplementation("org.testcontainers:testcontainers:$testcontainers_version") testImplementation("org.testcontainers:junit-jupiter:$testcontainers_version") - testImplementation("com.nhaarman:mockito-kotlin:1.6.0") + testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0") + testImplementation("org.mockito:mockito-inline:2.13.0") testImplementation("org.junit.jupiter:junit-jupiter-api:$junit_jupiter_version") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junit_jupiter_version") } } diff --git a/src/main/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisher.kt b/src/main/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisher.kt index b1bd1531..26c1a8b4 100644 --- a/src/main/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisher.kt +++ b/src/main/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisher.kt @@ -1,7 +1,6 @@ package com.viartemev.thewhiterabbit.publisher import com.rabbitmq.client.Channel -import com.viartemev.thewhiterabbit.common.cancelOnIOException import kotlinx.coroutines.suspendCancellableCoroutine import mu.KotlinLogging import java.util.concurrent.ConcurrentHashMap @@ -31,13 +30,11 @@ class ConfirmPublisher internal constructor(private val channel: Channel) { */ suspend fun publishWithConfirm(message: OutboundMessage): Boolean { val messageSequenceNumber = channel.nextPublishSeqNo - logger.debug { "The message Sequence Number: $messageSequenceNumber" } + logger.debug { "Generated message Sequence Number: $messageSequenceNumber" } return suspendCancellableCoroutine { continuation -> - continuations[messageSequenceNumber] = continuation continuation.invokeOnCancellation { continuations.remove(messageSequenceNumber) } - cancelOnIOException(continuation) { - message.run { channel.basicPublish(exchange, routingKey, properties, msg) } - } + continuations[messageSequenceNumber] = continuation + message.apply { channel.basicPublish(exchange, routingKey, properties, msg) } } } } diff --git a/src/test/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisherTest.kt b/src/test/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisherTest.kt deleted file mode 100644 index 9998eb69..00000000 --- a/src/test/kotlin/com/viartemev/thewhiterabbit/publisher/ConfirmPublisherTest.kt +++ /dev/null @@ -1,66 +0,0 @@ -package com.viartemev.thewhiterabbit.publisher - -import com.nhaarman.mockito_kotlin.any -import com.nhaarman.mockito_kotlin.doNothing -import com.nhaarman.mockito_kotlin.doThrow -import com.nhaarman.mockito_kotlin.mock -import com.rabbitmq.client.Channel -import com.rabbitmq.client.MessageProperties -import kotlinx.coroutines.* -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.fail -import org.mockito.AdditionalMatchers -import java.io.IOException -import java.util.concurrent.atomic.AtomicInteger - - -class ConfirmPublisherTest { - private val poisonMessage = OutboundMessage("", "", MessageProperties.PERSISTENT_BASIC, "poisonMessage") - private val validMessage = OutboundMessage("", "", MessageProperties.PERSISTENT_BASIC, "validMessage") - - @Test - fun `publish several messages with one poison message`() { - val channel = mock() - doNothing().`when`(channel) - .basicPublish(any(), any(), any(), AdditionalMatchers.aryEq("validMessage".toByteArray())) - doThrow(IOException("Boom")).`when`(channel) - .basicPublish(any(), any(), any(), AdditionalMatchers.aryEq("poisonMessage".toByteArray())) - - val counter = AtomicInteger() - val confirmPublisher = ConfirmPublisher(channel) - - runBlocking { - try { - coroutineScope { - val task1 = async { - println("Task1 has started...") - delay(50) - confirmPublisher.publishWithConfirm(poisonMessage) - counter.getAndAdd(1) - println("Task1 finished") - } - val task2 = async { - println("Task2 has started...") - confirmPublisher.publishWithConfirm(validMessage) - println("Task2 finished") - } - val task3 = async { - println("Task3 has started...") - confirmPublisher.publishWithConfirm(validMessage) - counter.getAndAdd(1) - println("Task3 finished") - } - awaitAll(task1, task2, task3) - } - fail("The method didn't throw when I expected it to") - } catch (e: IOException) { - println("CancellationException caught: $e") - } - } - assertEquals(0, counter.get()) - assertTrue(confirmPublisher.continuations.isEmpty()) - } - -} diff --git a/src/test/kotlin/com/viartemev/thewhiterabbit/publisher/PublisherIntegrationTest.kt b/src/test/kotlin/com/viartemev/thewhiterabbit/publisher/PublisherIntegrationTest.kt index ebee3de3..9d2ac61c 100644 --- a/src/test/kotlin/com/viartemev/thewhiterabbit/publisher/PublisherIntegrationTest.kt +++ b/src/test/kotlin/com/viartemev/thewhiterabbit/publisher/PublisherIntegrationTest.kt @@ -6,7 +6,10 @@ import com.viartemev.thewhiterabbit.channel.publish import com.viartemev.thewhiterabbit.queue.QueueSpecification import com.viartemev.thewhiterabbit.queue.declareQueue import com.viartemev.thewhiterabbit.utils.createMessage -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test @@ -14,43 +17,35 @@ import org.junit.jupiter.api.Test class PublisherIntegrationTest : AbstractTestContainersTest() { @Test - fun `test one message publishing`() { + fun `test one message publishing`(): Unit = runBlocking { factory.newConnection().use { val connection = it - runBlocking { - connection.confirmChannel { - val queue = declareQueue(QueueSpecification("")).queue - publish { - val message = createMessage(queue = queue, body = "Hello") - val ack = publishWithConfirm(message) - assertTrue { ack } - } - delay(5000) - val info = httpRabbitMQClient.getQueue(DEFAULT_VHOST, queue) - assertEquals(queue, info.name) - assertEquals(1, info.messagesReady) + connection.confirmChannel { + val queue = declareQueue(QueueSpecification("")).queue + publish { + val message = createMessage(queue = queue, body = "Hello") + val ack = publishWithConfirm(message) + assertTrue { ack } } + delay(5000) + val info = httpRabbitMQClient.getQueue(DEFAULT_VHOST, queue) + assertEquals(queue, info.name) + assertEquals(1, info.messagesReady) } } } @Test - fun `test n-messages publishing manually`() { - val times = 10 + fun `test n-messages publishing manually`(): Unit = runBlocking { factory.newConnection().use { connection -> - runBlocking { - connection.confirmChannel { - val queue = declareQueue(QueueSpecification("")).queue - publish { - val acks = coroutineScope { - (1..times).map { - async { - publishWithConfirm(createMessage(queue = queue, body = "Hello #$it")) - } - }.awaitAll() - } - assertTrue { acks.all { it } } - } + connection.confirmChannel { + val queue = declareQueue(QueueSpecification("")).queue + val publisher = this.publisher() + val res1 = async(Dispatchers.IO) { + publisher.publishWithConfirm(createMessage(queue = queue, body = "Hello #1")) + } + val res2 = async(Dispatchers.IO) { + publisher.publishWithConfirm(createMessage(queue = queue, body = "Hello #2")) } } } diff --git a/the-white-rabbit-example/src/main/kotlin/com/viartemev/thewhiterabbit/example/PublisherExample.kt b/the-white-rabbit-example/src/main/kotlin/com/viartemev/thewhiterabbit/example/PublisherExample.kt index 983e28f0..f7c3435e 100644 --- a/the-white-rabbit-example/src/main/kotlin/com/viartemev/thewhiterabbit/example/PublisherExample.kt +++ b/the-white-rabbit-example/src/main/kotlin/com/viartemev/thewhiterabbit/example/PublisherExample.kt @@ -13,16 +13,13 @@ const val PUBLISHER_EXCHANGE_NAME = "" const val PUBLISHER_QUEUE_NAME = "test_queue" const val TIMES = 100_000 -fun main() { +fun main() = runBlocking { val connectionFactory = ConnectionFactory().apply { useNio() } val connection = connectionFactory.newConnection() - runBlocking { - connection.confirmChannel { - publish { - coroutineScope { - val messages = (1..TIMES).map { createMessage("") } - publishWithConfirmAsync(this.coroutineContext, messages).awaitAll() - } + connection.confirmChannel { + publish { + coroutineScope { + val messages = (1..TIMES).map { createMessage("") }.map { publishWithConfirm(it) } } } }