From fc2ce6640c9782bc328842d7e5ba2a999cebf6b2 Mon Sep 17 00:00:00 2001 From: Ruben Gees Date: Tue, 16 Jan 2024 18:01:31 +0100 Subject: [PATCH] Simplify tests --- .../socketio/emitter/JedisPublisherTest.kt | 31 ++------------- .../socketio/emitter/LettucePublisherTest.kt | 28 +++----------- .../emitter/SpringDataPublisherTest.kt | 22 ++--------- .../smartsquare/socketio/emitter/TestUtils.kt | 38 +++++++++++++++++++ 4 files changed, 51 insertions(+), 68 deletions(-) diff --git a/src/test/kotlin/de/smartsquare/socketio/emitter/JedisPublisherTest.kt b/src/test/kotlin/de/smartsquare/socketio/emitter/JedisPublisherTest.kt index 14148c0..9bfcced 100644 --- a/src/test/kotlin/de/smartsquare/socketio/emitter/JedisPublisherTest.kt +++ b/src/test/kotlin/de/smartsquare/socketio/emitter/JedisPublisherTest.kt @@ -2,7 +2,6 @@ package de.smartsquare.socketio.emitter import com.redis.testcontainers.RedisContainer import org.amshove.kluent.shouldBeEqualTo -import org.amshove.kluent.shouldBeTrue import org.amshove.kluent.shouldContain import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach @@ -10,10 +9,6 @@ import org.junit.jupiter.api.Test import org.testcontainers.junit.jupiter.Container import org.testcontainers.junit.jupiter.Testcontainers import redis.clients.jedis.JedisPool -import redis.clients.jedis.JedisPubSub -import java.util.concurrent.CountDownLatch -import java.util.concurrent.Executors -import java.util.concurrent.TimeUnit @Testcontainers class JedisPublisherTest { @@ -37,29 +32,11 @@ class JedisPublisherTest { fun `publish string message`() { val publisher = Emitter(JedisPublisher(pool)) - val countDownLatch = CountDownLatch(1) - - val listener = object : JedisPubSub() { - override fun onMessage(channel: String, message: String) { - channel shouldBeEqualTo "socket.io#/#" - message shouldContain "test 123" - - countDownLatch.countDown() - } - } - - val executor = Executors.newSingleThreadExecutor() - val jedis = pool.resource - - try { - executor.submit { jedis.subscribe(listener, "socket.io#/#") } - + val (channel, message) = awaitRedisMessage(redis.redisURI, "socket.io#/#") { publisher.broadcast("topic", "test 123") - - countDownLatch.await(5, TimeUnit.SECONDS).shouldBeTrue() - } finally { - listener.unsubscribe() - executor.shutdown() } + + channel shouldBeEqualTo "socket.io#/#" + message shouldContain "test 123" } } diff --git a/src/test/kotlin/de/smartsquare/socketio/emitter/LettucePublisherTest.kt b/src/test/kotlin/de/smartsquare/socketio/emitter/LettucePublisherTest.kt index d0d6380..691b12c 100644 --- a/src/test/kotlin/de/smartsquare/socketio/emitter/LettucePublisherTest.kt +++ b/src/test/kotlin/de/smartsquare/socketio/emitter/LettucePublisherTest.kt @@ -2,17 +2,13 @@ package de.smartsquare.socketio.emitter import com.redis.testcontainers.RedisContainer import io.lettuce.core.RedisClient -import io.lettuce.core.pubsub.RedisPubSubAdapter import org.amshove.kluent.shouldBeEqualTo -import org.amshove.kluent.shouldBeTrue import org.amshove.kluent.shouldContain import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.testcontainers.junit.jupiter.Container import org.testcontainers.junit.jupiter.Testcontainers -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit @Testcontainers class LettucePublisherTest { @@ -34,27 +30,13 @@ class LettucePublisherTest { @Test fun `publish string message`() { - val connection = client.connect().sync() - val publisher = Emitter(LettucePublisher(connection)) + val publisher = Emitter(LettucePublisher(client.connect().sync())) - val countDownLatch = CountDownLatch(1) - - val listener = object : RedisPubSubAdapter() { - override fun message(channel: String, message: String) { - channel shouldBeEqualTo "socket.io#/#" - message shouldContain "test 123" - - countDownLatch.countDown() - } + val (channel, message) = awaitRedisMessage(redis.redisURI, "socket.io#/#") { + publisher.broadcast("topic", "test 123") } - client.connectPubSub().apply { - addListener(listener) - sync().subscribe("socket.io#/#") - } - - publisher.broadcast("topic", "test 123") - - countDownLatch.await(5, TimeUnit.SECONDS).shouldBeTrue() + channel shouldBeEqualTo "socket.io#/#" + message shouldContain "test 123" } } diff --git a/src/test/kotlin/de/smartsquare/socketio/emitter/SpringDataPublisherTest.kt b/src/test/kotlin/de/smartsquare/socketio/emitter/SpringDataPublisherTest.kt index 6920e11..258a31d 100644 --- a/src/test/kotlin/de/smartsquare/socketio/emitter/SpringDataPublisherTest.kt +++ b/src/test/kotlin/de/smartsquare/socketio/emitter/SpringDataPublisherTest.kt @@ -2,18 +2,14 @@ package de.smartsquare.socketio.emitter import com.redis.testcontainers.RedisContainer import org.amshove.kluent.shouldBeEqualTo -import org.amshove.kluent.shouldBeTrue import org.amshove.kluent.shouldContain import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import org.springframework.data.redis.connection.Message import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory import org.springframework.data.redis.core.StringRedisTemplate import org.testcontainers.junit.jupiter.Container import org.testcontainers.junit.jupiter.Testcontainers -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit @Testcontainers class SpringDataPublisherTest { @@ -45,21 +41,11 @@ class SpringDataPublisherTest { fun `publish string message`() { val publisher = Emitter(SpringDataPublisher(template)) - val countDownLatch = CountDownLatch(1) - - val messageListener = { message: Message, _: ByteArray? -> - message.channel.decodeToString() shouldBeEqualTo "socket.io#/#" - message.body.decodeToString() shouldContain "test 123" - - countDownLatch.countDown() + val (channel, message) = awaitRedisMessage(redis.redisURI, "socket.io#/#") { + publisher.broadcast("topic", "test 123") } - template.requiredConnectionFactory.connection.also { - it.subscribe(messageListener, "socket.io#/#".toByteArray()) - } - - publisher.broadcast("topic", "test 123") - - countDownLatch.await(5, TimeUnit.SECONDS).shouldBeTrue() + channel shouldBeEqualTo "socket.io#/#" + message shouldContain "test 123" } } diff --git a/src/test/kotlin/de/smartsquare/socketio/emitter/TestUtils.kt b/src/test/kotlin/de/smartsquare/socketio/emitter/TestUtils.kt index 0aa2592..cd3388e 100644 --- a/src/test/kotlin/de/smartsquare/socketio/emitter/TestUtils.kt +++ b/src/test/kotlin/de/smartsquare/socketio/emitter/TestUtils.kt @@ -1,5 +1,43 @@ package de.smartsquare.socketio.emitter +import io.lettuce.core.RedisClient +import io.lettuce.core.pubsub.RedisPubSubAdapter +import org.amshove.kluent.shouldBeTrue import org.skyscreamer.jsonassert.JSONAssert +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit infix fun String.shouldBeEqualToJson(expected: String): String = apply { JSONAssert.assertEquals(expected, this, true) } + +/** + * Returns the first message published on the given [channel] of the redis available under the [redisURI]. Waits for at + * most five seconds after executing [body] for the message to arrive. + */ +fun awaitRedisMessage(redisURI: String, channel: String, body: () -> Unit): RedisMessage { + return RedisClient.create(redisURI).use { client -> + var result: RedisMessage? = null + + val countDownLatch = CountDownLatch(1) + + val listener = object : RedisPubSubAdapter() { + override fun message(channel: String, message: String) { + result = RedisMessage(channel, message) + + countDownLatch.countDown() + } + } + + client.connectPubSub().apply { + addListener(listener) + sync().subscribe(channel) + } + + body() + + countDownLatch.await(5, TimeUnit.SECONDS).shouldBeTrue() + + result!! + } +} + +data class RedisMessage(val channel: String, val message: String)