|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import io.lettuce.core.LettuceFutures; |
| 4 | +import io.lettuce.core.RedisClient; |
| 5 | +import io.lettuce.core.RedisFuture; |
| 6 | +import io.lettuce.core.TransactionResult; |
| 7 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 8 | +import io.lettuce.core.api.async.RedisAsyncCommands; |
| 9 | +import io.lettuce.core.api.sync.RedisCommands; |
| 10 | +import io.lettuce.core.pubsub.RedisPubSubListener; |
| 11 | +import io.lettuce.core.pubsub.StatefulRedisPubSubConnection; |
| 12 | +import io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands; |
| 13 | +import org.junit.AfterClass; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Test; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | + |
| 28 | +public class LettuceIntegrationLiveTest { |
| 29 | + |
| 30 | + private static Logger log = LoggerFactory.getLogger(LettuceIntegrationLiveTest.class); |
| 31 | + |
| 32 | + private static StatefulRedisConnection<String, String> redisConnection; |
| 33 | + |
| 34 | + private static RedisClient redisClient; |
| 35 | + |
| 36 | + @BeforeClass |
| 37 | + public static void setUp() { |
| 38 | + // Docker defaults to mapping redis port to 32768 |
| 39 | + redisClient = RedisClient.create("redis://localhost:32768/"); |
| 40 | + redisConnection = redisClient.connect(); |
| 41 | + } |
| 42 | + |
| 43 | + @AfterClass |
| 44 | + public static void destroy() { |
| 45 | + redisConnection.close(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenAString_thenSaveItAsRedisStringsSync() { |
| 50 | + |
| 51 | + RedisCommands<String, String> syncCommands = redisConnection.sync(); |
| 52 | + |
| 53 | + String key = "key"; |
| 54 | + String value = "value"; |
| 55 | + |
| 56 | + syncCommands.set(key, value); |
| 57 | + String response = syncCommands.get(key); |
| 58 | + |
| 59 | + Assert.assertEquals(value, response); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenValues_thenSaveAsRedisHashSync() { |
| 64 | + |
| 65 | + RedisCommands<String, String> syncCommands = redisConnection.sync(); |
| 66 | + |
| 67 | + String recordName = "record1"; |
| 68 | + String name = "FirstName"; |
| 69 | + String value = "John"; |
| 70 | + String surname = "LastName"; |
| 71 | + String value1 = "Smith"; |
| 72 | + |
| 73 | + syncCommands.hset(recordName, name, value); |
| 74 | + syncCommands.hset(recordName, surname, value1); |
| 75 | + Map<String, String> record = syncCommands.hgetall(recordName); |
| 76 | + |
| 77 | + Assert.assertEquals(record.get(name), value); |
| 78 | + Assert.assertEquals(record.get(surname), value1); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenAString_thenSaveItAsRedisStringsAsync() throws Exception { |
| 83 | + |
| 84 | + RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); |
| 85 | + |
| 86 | + String key = "key"; |
| 87 | + String value = "value"; |
| 88 | + |
| 89 | + asyncCommands.set(key, value); |
| 90 | + RedisFuture<String> redisFuture = asyncCommands.get(key); |
| 91 | + |
| 92 | + String response = redisFuture.get(); |
| 93 | + |
| 94 | + Assert.assertEquals(value, response); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenValues_thenSaveAsRedisHashAsync() throws Exception { |
| 99 | + |
| 100 | + RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); |
| 101 | + |
| 102 | + String recordName = "record1"; |
| 103 | + String name = "FirstName"; |
| 104 | + String value = "John"; |
| 105 | + String surname = "LastName"; |
| 106 | + String value1 = "Smith"; |
| 107 | + |
| 108 | + asyncCommands.hset(recordName, name, value); |
| 109 | + asyncCommands.hset(recordName, surname, value1); |
| 110 | + RedisFuture<Map<String, String>> redisFuture = asyncCommands.hgetall(recordName); |
| 111 | + |
| 112 | + Map<String, String> record = redisFuture.get(); |
| 113 | + |
| 114 | + Assert.assertEquals(record.get(name), value); |
| 115 | + Assert.assertEquals(record.get(surname), value1); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void givenValues_thenSaveAsRedisListAsync() throws Exception { |
| 120 | + |
| 121 | + RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); |
| 122 | + |
| 123 | + String listName = "tasks"; |
| 124 | + String firstTask = "firstTask"; |
| 125 | + String secondTask = "secondTask"; |
| 126 | + |
| 127 | + asyncCommands.del(listName); |
| 128 | + |
| 129 | + asyncCommands.lpush(listName, firstTask); |
| 130 | + asyncCommands.lpush(listName, secondTask); |
| 131 | + RedisFuture<String> redisFuture = asyncCommands.rpop(listName); |
| 132 | + |
| 133 | + String nextTask = redisFuture.get(); |
| 134 | + |
| 135 | + Assert.assertEquals(firstTask, nextTask); |
| 136 | + |
| 137 | + asyncCommands.del(listName); |
| 138 | + |
| 139 | + asyncCommands.lpush(listName, firstTask); |
| 140 | + asyncCommands.lpush(listName, secondTask); |
| 141 | + |
| 142 | + redisFuture = asyncCommands.lpop(listName); |
| 143 | + |
| 144 | + nextTask = redisFuture.get(); |
| 145 | + |
| 146 | + Assert.assertEquals(secondTask, nextTask); |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void givenSetElements_thenSaveThemInRedisSetAsync() throws Exception { |
| 152 | + |
| 153 | + RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); |
| 154 | + |
| 155 | + String countries = "countries"; |
| 156 | + |
| 157 | + String countryOne = "Spain"; |
| 158 | + String countryTwo = "Ireland"; |
| 159 | + String countryThree = "Ireland"; |
| 160 | + |
| 161 | + asyncCommands.sadd(countries, countryOne); |
| 162 | + |
| 163 | + RedisFuture<Set<String>> countriesSetFuture = asyncCommands.smembers(countries); |
| 164 | + Assert.assertEquals(2, countriesSetFuture.get().size()); |
| 165 | + |
| 166 | + asyncCommands.sadd(countries, countryTwo); |
| 167 | + countriesSetFuture = asyncCommands.smembers(countries); |
| 168 | + Assert.assertEquals(2, countriesSetFuture.get().size()); |
| 169 | + |
| 170 | + asyncCommands.sadd(countries, countryThree); |
| 171 | + countriesSetFuture = asyncCommands.smembers(countries); |
| 172 | + Assert.assertEquals(2, countriesSetFuture.get().size()); |
| 173 | + |
| 174 | + RedisFuture<Boolean> exists = asyncCommands.sismember(countries, countryThree); |
| 175 | + assertTrue(exists.get()); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void givenARanking_thenSaveItInRedisSortedSetAsync() throws Exception { |
| 180 | + |
| 181 | + RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); |
| 182 | + |
| 183 | + String key = "sortedset"; |
| 184 | + |
| 185 | + asyncCommands.zadd(key, 1, "one"); |
| 186 | + asyncCommands.zadd(key, 4, "zero"); |
| 187 | + asyncCommands.zadd(key, 2, "two"); |
| 188 | + |
| 189 | + RedisFuture<List<String>> values = asyncCommands.zrevrange(key, 0, 3); |
| 190 | + Assert.assertEquals("zero", values.get().get(0)); |
| 191 | + |
| 192 | + values = asyncCommands.zrange(key, 0, 3); |
| 193 | + Assert.assertEquals("one", values.get().get(0)); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void givenMultipleOperationsThatNeedToBeExecutedAtomically_thenWrapThemInATransaction() throws Exception { |
| 198 | + |
| 199 | + RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); |
| 200 | + |
| 201 | + // Start a transaction |
| 202 | + asyncCommands.multi(); |
| 203 | + |
| 204 | + // Add three sets to it, and save the future responses |
| 205 | + RedisFuture<String> result1 = asyncCommands.set("key1", "value1"); |
| 206 | + RedisFuture<String> result2 = asyncCommands.set("key2", "value2"); |
| 207 | + RedisFuture<String> result3 = asyncCommands.set("key3", "value3"); |
| 208 | + |
| 209 | + // Execute it |
| 210 | + RedisFuture<TransactionResult> execResult = asyncCommands.exec(); |
| 211 | + |
| 212 | + TransactionResult transactionResult = execResult.get(); |
| 213 | + |
| 214 | + // Get the three results in the transaction return |
| 215 | + String firstResult = transactionResult.get(0); |
| 216 | + String secondResult = transactionResult.get(0); |
| 217 | + String thirdResult = transactionResult.get(0); |
| 218 | + |
| 219 | + // Our results are in both! |
| 220 | + assertTrue(firstResult.equals("OK")); |
| 221 | + assertTrue(secondResult.equals("OK")); |
| 222 | + assertTrue(thirdResult.equals("OK")); |
| 223 | + |
| 224 | + assertTrue(result1.get().equals("OK")); |
| 225 | + assertTrue(result2.get().equals("OK")); |
| 226 | + assertTrue(result3.get().equals("OK")); |
| 227 | + } |
| 228 | + |
| 229 | + @Test |
| 230 | + public void givenMultipleIndependentOperations_whenNetworkOptimizationIsImportant_thenFlushManually() throws Exception { |
| 231 | + |
| 232 | + int iterations = 50; |
| 233 | + |
| 234 | + RedisAsyncCommands<String, String> asyncCommands = redisConnection.async(); |
| 235 | + |
| 236 | + asyncCommands.setAutoFlushCommands(false); |
| 237 | + |
| 238 | + List<RedisFuture<?>> futures = new ArrayList<>(); |
| 239 | + for (int i = 0; i < iterations; i++) { |
| 240 | + futures.add(asyncCommands.set("key" + i, "value" + i)); |
| 241 | + } |
| 242 | + |
| 243 | + asyncCommands.flushCommands(); |
| 244 | + |
| 245 | + // Wait until all futures complete |
| 246 | + boolean result = LettuceFutures.awaitAll(5, TimeUnit.SECONDS, futures.toArray(new RedisFuture[futures.size()])); |
| 247 | + |
| 248 | + asyncCommands.setAutoFlushCommands(true); |
| 249 | + |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void givenPubSubChannel_whenMessage_thenMessageReceived() throws Exception { |
| 254 | + |
| 255 | + Listener listener = new Listener(); |
| 256 | + StatefulRedisPubSubConnection<String, String> connection = redisClient.connectPubSub(); |
| 257 | + StatefulRedisPubSubConnection<String, String> pubconnection = redisClient.connectPubSub(); |
| 258 | + connection.addListener(listener); |
| 259 | + |
| 260 | + RedisPubSubAsyncCommands<String, String> async = connection.async(); |
| 261 | + async.subscribe("channel"); |
| 262 | + |
| 263 | + RedisPubSubAsyncCommands<String, String> pubasync = pubconnection.async(); |
| 264 | + RedisFuture<Long> result = pubasync.publish("channel", "hithere"); |
| 265 | + |
| 266 | + // Need a long wait for publish to complete, depending on system. |
| 267 | + result.get(15, TimeUnit.SECONDS); |
| 268 | + assertTrue(listener.getMessage().equals("hithere")); |
| 269 | + |
| 270 | + } |
| 271 | + |
| 272 | + private static class Listener implements RedisPubSubListener<String, String> { |
| 273 | + |
| 274 | + private String message; |
| 275 | + |
| 276 | + String getMessage() { |
| 277 | + return message; |
| 278 | + } |
| 279 | + |
| 280 | + @Override |
| 281 | + public void message(String channel, String message) { |
| 282 | + log.debug("Got {} on {}", message, channel); |
| 283 | + this.message = message; |
| 284 | + } |
| 285 | + |
| 286 | + @Override |
| 287 | + public void message(String pattern, String channel, String message) { |
| 288 | + |
| 289 | + } |
| 290 | + |
| 291 | + @Override |
| 292 | + public void subscribed(String channel, long count) { |
| 293 | + log.debug("Subscribed to {}", channel); |
| 294 | + } |
| 295 | + |
| 296 | + @Override |
| 297 | + public void psubscribed(String pattern, long count) { |
| 298 | + |
| 299 | + } |
| 300 | + |
| 301 | + @Override |
| 302 | + public void unsubscribed(String channel, long count) { |
| 303 | + |
| 304 | + } |
| 305 | + |
| 306 | + @Override |
| 307 | + public void punsubscribed(String pattern, long count) { |
| 308 | + |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | +} |
0 commit comments