Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubengees committed Jan 17, 2024
1 parent 7769e85 commit e4567f9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ 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.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 {
Expand All @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String, String>() {
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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
}
38 changes: 38 additions & 0 deletions src/test/kotlin/de/smartsquare/socketio/emitter/TestUtils.kt
Original file line number Diff line number Diff line change
@@ -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<String, String>() {
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)

0 comments on commit e4567f9

Please sign in to comment.