-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Spring Data and update Lettuce support #8
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions
6
src/main/kotlin/de/smartsquare/socketio/emitter/LettucePublisher.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,9 +1,9 @@ | ||
package de.smartsquare.socketio.emitter | ||
|
||
import io.lettuce.core.api.StatefulRedisConnection | ||
import io.lettuce.core.api.sync.RedisCommands | ||
|
||
class LettucePublisher(private val connection: StatefulRedisConnection<Any, Any>) : RedisPublisher { | ||
class LettucePublisher(private val commands: RedisCommands<String, String>) : RedisPublisher { | ||
override fun publish(channel: String, message: ByteArray) { | ||
connection.use { it.sync().publish(channel, message.decodeToString()) } | ||
commands.publish(channel, message.decodeToString()) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/de/smartsquare/socketio/emitter/SpringDataPublisher.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,9 @@ | ||
package de.smartsquare.socketio.emitter | ||
|
||
import org.springframework.data.redis.core.RedisTemplate | ||
|
||
class SpringDataPublisher(private val redisTemplate: RedisTemplate<String, String>) : RedisPublisher { | ||
override fun publish(channel: String, message: ByteArray) { | ||
redisTemplate.execute({ it.publish(channel.toByteArray(), message) }, true) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/kotlin/de/smartsquare/socketio/emitter/JedisPublisherTest.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,42 @@ | ||
package de.smartsquare.socketio.emitter | ||
|
||
import com.redis.testcontainers.RedisContainer | ||
import org.amshove.kluent.shouldBeEqualTo | ||
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 | ||
|
||
@Testcontainers | ||
class JedisPublisherTest { | ||
|
||
@Container | ||
private val redis = RedisContainer("redis:6-alpine") | ||
|
||
private lateinit var pool: JedisPool | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
pool = JedisPool(redis.redisURI) | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
pool.close() | ||
} | ||
|
||
@Test | ||
fun `publish string message`() { | ||
val publisher = Emitter(JedisPublisher(pool)) | ||
|
||
val (channel, message) = awaitRedisMessage(redis.redisURI, "socket.io#/#") { | ||
publisher.broadcast("topic", "test 123") | ||
} | ||
|
||
channel shouldBeEqualTo "socket.io#/#" | ||
message shouldContain "test 123" | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/kotlin/de/smartsquare/socketio/emitter/LettucePublisherTest.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,42 @@ | ||
package de.smartsquare.socketio.emitter | ||
|
||
import com.redis.testcontainers.RedisContainer | ||
import io.lettuce.core.RedisClient | ||
import org.amshove.kluent.shouldBeEqualTo | ||
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 | ||
|
||
@Testcontainers | ||
class LettucePublisherTest { | ||
|
||
@Container | ||
private val redis = RedisContainer("redis:6-alpine") | ||
|
||
private lateinit var client: RedisClient | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
client = RedisClient.create(redis.redisURI) | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
client.close() | ||
} | ||
|
||
@Test | ||
fun `publish string message`() { | ||
val publisher = Emitter(LettucePublisher(client.connect().sync())) | ||
|
||
val (channel, message) = awaitRedisMessage(redis.redisURI, "socket.io#/#") { | ||
publisher.broadcast("topic", "test 123") | ||
} | ||
|
||
channel shouldBeEqualTo "socket.io#/#" | ||
message shouldContain "test 123" | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/kotlin/de/smartsquare/socketio/emitter/SpringDataPublisherTest.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,51 @@ | ||
package de.smartsquare.socketio.emitter | ||
|
||
import com.redis.testcontainers.RedisContainer | ||
import org.amshove.kluent.shouldBeEqualTo | ||
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.lettuce.LettuceConnectionFactory | ||
import org.springframework.data.redis.core.StringRedisTemplate | ||
import org.testcontainers.junit.jupiter.Container | ||
import org.testcontainers.junit.jupiter.Testcontainers | ||
|
||
@Testcontainers | ||
class SpringDataPublisherTest { | ||
|
||
@Container | ||
private val redis = RedisContainer("redis:6-alpine") | ||
|
||
private lateinit var lettuceConnectionFactory: LettuceConnectionFactory | ||
private lateinit var template: StringRedisTemplate | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
lettuceConnectionFactory = LettuceConnectionFactory(redis.host, redis.firstMappedPort).apply { | ||
afterPropertiesSet() | ||
} | ||
|
||
template = StringRedisTemplate().apply { | ||
connectionFactory = lettuceConnectionFactory | ||
afterPropertiesSet() | ||
} | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
lettuceConnectionFactory.destroy() | ||
} | ||
|
||
@Test | ||
fun `publish string message`() { | ||
val publisher = Emitter(SpringDataPublisher(template)) | ||
|
||
val (channel, message) = awaitRedisMessage(redis.redisURI, "socket.io#/#") { | ||
publisher.broadcast("topic", "test 123") | ||
} | ||
|
||
channel shouldBeEqualTo "socket.io#/#" | ||
message shouldContain "test 123" | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great! 🤩 |
||
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) |
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,17 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="org.testcontainers" level="INFO"/> | ||
<!-- The following logger can be used for containers logs since 1.18.0 --> | ||
<logger name="tc" level="INFO"/> | ||
<logger name="com.github.dockerjava" level="WARN"/> | ||
<logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="OFF"/> | ||
</configuration> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just wondering if the key/value is always String?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For lettuce, the consumer can pass in a
Codec
that encodes and decodes what is sent to redis. So you could also have e.g.RedisCommands<ByteArray, ByteArray>
. When using the instance, we need to know what the types are though so we now what we can send and we request an instance that handles Strings. I think it will be that anyways in almost all cases since all default APIs return that.