-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Artemyev Vyacheslav
authored and
Artemyev Vyacheslav
committed
Nov 10, 2023
1 parent
156badc
commit b097576
Showing
5 changed files
with
53 additions
and
47 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/viartemev/thewhiterabbit/common/Connection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.viartemev.thewhiterabbit.common | ||
|
||
import com.rabbitmq.client.AMQP | ||
|
||
fun AMQP.Connection.withConnection() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
src/main/kotlin/com/viartemev/thewhiterabbit/exception/AcknowledgeException.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 34 additions & 29 deletions
63
src/test/kotlin/com/viartemev/thewhiterabbit/consumer/flow/ConsumerFlowTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,60 @@ | ||
package com.viartemev.thewhiterabbit.consumer.flow | ||
|
||
import com.rabbitmq.client.MessageProperties | ||
import com.viartemev.thewhiterabbit.AbstractTestContainersTest | ||
import com.viartemev.thewhiterabbit.channel.confirmChannel | ||
import com.viartemev.thewhiterabbit.channel.publish | ||
import com.viartemev.thewhiterabbit.channel.channel | ||
import com.viartemev.thewhiterabbit.queue.QueueSpecification | ||
import com.viartemev.thewhiterabbit.queue.declareQueue | ||
import com.viartemev.thewhiterabbit.utils.createMessage | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.single | ||
import kotlinx.coroutines.flow.take | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class ConsumerFlowTest : AbstractTestContainersTest() { | ||
private val QUEUE_NAME = "test_queue" | ||
private val connection = factory.newConnection() | ||
|
||
private suspend fun generateMessages(count: Int) = coroutineScope { | ||
connection.channel { | ||
declareQueue(QueueSpecification(QUEUE_NAME)) | ||
} | ||
connection.channel { | ||
(1..count).map { | ||
basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_BASIC, "Hello #$it".toByteArray()) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun testAutoAckFlow(): Unit = runBlocking { | ||
factory.newConnection().use { connection -> | ||
connection.confirmChannel { | ||
declareQueue(QueueSpecification(QUEUE_NAME)) | ||
publish { | ||
(1..10).map { createMessage(queue = QUEUE_NAME, body = "1") } | ||
.map { m -> async { publishWithConfirm(m) } }.awaitAll() | ||
} | ||
ConsumerFlow(this, QUEUE_NAME).consumerAutoAckFlow(2).take(10) | ||
.collect { delivery -> println(String(delivery.body)) } | ||
} | ||
val messagesCount = 100 | ||
generateMessages(messagesCount) | ||
connection.channel { | ||
ConsumerFlow(this, QUEUE_NAME) | ||
.consumerAutoAckFlow(2) | ||
.take(messagesCount) | ||
.collect { delivery -> println(String(delivery.body)) } | ||
} | ||
} | ||
|
||
@Test | ||
fun testConfirmAckFlow(): Unit = runBlocking { | ||
factory.newConnection().use { connection -> | ||
connection.confirmChannel { | ||
declareQueue(QueueSpecification(QUEUE_NAME)) | ||
publish { | ||
(1..10).map { i -> createMessage(queue = QUEUE_NAME, body = i.toString()) } | ||
.map { m -> async { publishWithConfirm(m) } }.awaitAll() | ||
val messagesCount = 10 | ||
generateMessages(messagesCount) | ||
connection.channel { | ||
ConsumerFlow(this, QUEUE_NAME) | ||
.consumerConfirmAckFlow(2) | ||
.flowOn(Dispatchers.IO) | ||
.take(messagesCount) | ||
.catch { e -> println("Caught exception: $e") }.collect { delivery -> | ||
println("Got the message: ${delivery.body}") | ||
} | ||
val delivery = ConsumerFlow(this, QUEUE_NAME) | ||
.consumerConfirmAckFlow(2) | ||
.flowOn(Dispatchers.IO) | ||
.catch { e -> println("Caught exception: $e") } | ||
.single() | ||
println("Got the message: ${delivery.body}") | ||
} | ||
} | ||
} | ||
|
||
} |