-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
51 additions
and
68 deletions.
There are no files selected for viewing
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
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
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
38 changes: 38 additions & 0 deletions
38
src/test/kotlin/de/smartsquare/socketio/emitter/TestUtils.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,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) |