Skip to content
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 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ dependencies {
}
```

This library comes with a default implementation for both jedis and lettuce.
This library comes with a default implementation
for [Jedis](https://github.com/redis/jedis),[Lettuce](https://lettuce.io/)
and [Spring Data Redis](https://spring.io/projects/spring-data-redis/).

### Emit Cheatsheet

Expand Down Expand Up @@ -81,19 +83,6 @@ The [example](example) directory contains a working docker-compose setup which c
using `docker-compose --compatibility up`. The setup contains one redis instance, one java publisher, three
socket.io-servers and three consuming socket.io-clients.

### Usage in Spring Boot and Jedis

We don't want to rely on the Spring Data Redis abstractions in this project.
Unfortunately, this makes it necessary that you configure the `JedisPool` manually in your Spring Boot application.
Having JedisPool configured, the emitter can be created as follows:

```kotlin
@Bean
fun emitter(jedisPool: JedisPool): Emitter {
return Emitter(JedisPublisher(jedisPool))
}
```

## :warning: Limitations

- The room and namespaces have not been tested yet.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ buildscript {
gradleVersionsVersion = "0.47.0"
jedisVersion = "4.4.3"
lettuceVersion = "6.2.6.RELEASE"
springDataVersion = "2.7.18"
testcontainersVersion= "1.19.3"
testcontainersRedisVersion= "2.0.1"
}

configurations.classpath {
Expand Down Expand Up @@ -61,12 +64,18 @@ dependencies {
testImplementation "org.amshove.kluent:kluent:$kluentVersion"
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
testImplementation "org.skyscreamer:jsonassert:$jsonAssertVersion"
testImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion"
testImplementation "com.redis:testcontainers-redis:$testcontainersRedisVersion"

testImplementation "redis.clients:jedis:$jedisVersion"
testImplementation "io.lettuce:lettuce-core:$lettuceVersion"
testImplementation "org.springframework.boot:spring-boot-starter-data-redis:$springDataVersion"

testRuntimeOnly "net.bytebuddy:byte-buddy:$byteBuddyVersion"

compileOnly "redis.clients:jedis:$jedisVersion"
compileOnly "io.lettuce:lettuce-core:$lettuceVersion"
compileOnly "org.springframework.boot:spring-boot-starter-data-redis:$springDataVersion"
}

java {
Expand Down
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 {
Copy link
Member

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?

Copy link
Member Author

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.

override fun publish(channel: String, message: ByteArray) {
connection.use { it.sync().publish(channel, message.decodeToString()) }
commands.publish(channel, message.decodeToString())
}
}
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)
}
}
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"
}
}
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"
}
}
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 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 {
Copy link
Member

Choose a reason for hiding this comment

The 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)
17 changes: 17 additions & 0 deletions src/test/resources/logback-test.xml
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>
Loading