|
| 1 | +package io.vertx.redis.client.test; |
| 2 | + |
| 3 | +import io.vertx.ext.unit.Async; |
| 4 | +import io.vertx.ext.unit.TestContext; |
| 5 | +import io.vertx.ext.unit.junit.RunTestOnContext; |
| 6 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 7 | +import io.vertx.redis.client.Command; |
| 8 | +import io.vertx.redis.client.ProtocolVersion; |
| 9 | +import io.vertx.redis.client.Redis; |
| 10 | +import io.vertx.redis.client.RedisOptions; |
| 11 | +import io.vertx.redis.client.Request; |
| 12 | +import org.junit.ClassRule; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.testcontainers.containers.GenericContainer; |
| 17 | + |
| 18 | +@RunWith(VertxUnitRunner.class) |
| 19 | +public class RedisProtocolVersionTest { |
| 20 | + @ClassRule |
| 21 | + public static final GenericContainer<?> redis = new GenericContainer<>("redis:7") |
| 22 | + .withExposedPorts(6379); |
| 23 | + |
| 24 | + @Rule |
| 25 | + public final RunTestOnContext rule = new RunTestOnContext(); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void resp2(TestContext test) { |
| 29 | + RedisOptions options = new RedisOptions() |
| 30 | + .setConnectionString("redis://" + redis.getHost() + ":" + redis.getFirstMappedPort()) |
| 31 | + .setPreferredProtocolVersion(ProtocolVersion.RESP2); |
| 32 | + |
| 33 | + Redis client = Redis.createClient(rule.vertx(), options); |
| 34 | + |
| 35 | + Async async = test.async(); |
| 36 | + client |
| 37 | + .send(Request.cmd(Command.DEL).arg("myhash")) |
| 38 | + .flatMap(ignored -> { |
| 39 | + return client.send(Request.cmd(Command.HSET).arg("myhash").arg("field1").arg(1).arg("field2").arg(2)); |
| 40 | + }) |
| 41 | + .flatMap(response -> { |
| 42 | + test.assertEquals(2, response.toInteger()); |
| 43 | + return client.send(Request.cmd(Command.HGETALL).arg("myhash")); |
| 44 | + }) |
| 45 | + .onSuccess(response -> { |
| 46 | + test.assertTrue(response.toString().startsWith("[")); // list |
| 47 | + |
| 48 | + test.assertTrue(response.containsKey("field1")); |
| 49 | + test.assertEquals(1, response.get("field1").toInteger()); |
| 50 | + |
| 51 | + test.assertTrue(response.containsKey("field2")); |
| 52 | + test.assertEquals(2, response.get("field2").toInteger()); |
| 53 | + |
| 54 | + test.assertEquals("field1", response.get(0).toString()); |
| 55 | + |
| 56 | + client.close(); |
| 57 | + async.complete(); |
| 58 | + }).onFailure(test::fail); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void resp3(TestContext test) { |
| 63 | + RedisOptions options = new RedisOptions() |
| 64 | + .setConnectionString("redis://" + redis.getHost() + ":" + redis.getFirstMappedPort()) |
| 65 | + .setPreferredProtocolVersion(ProtocolVersion.RESP3); |
| 66 | + |
| 67 | + Redis client = Redis.createClient(rule.vertx(), options); |
| 68 | + |
| 69 | + Async async = test.async(); |
| 70 | + client |
| 71 | + .send(Request.cmd(Command.DEL).arg("myhash")) |
| 72 | + .flatMap(ignored -> { |
| 73 | + return client.send(Request.cmd(Command.HSET).arg("myhash").arg("field1").arg(3).arg("field2").arg(4)); |
| 74 | + }) |
| 75 | + .flatMap(response -> { |
| 76 | + test.assertEquals(2, response.toInteger()); |
| 77 | + return client.send(Request.cmd(Command.HGETALL).arg("myhash")); |
| 78 | + }) |
| 79 | + .onSuccess(response -> { |
| 80 | + test.assertTrue(response.toString().startsWith("{")); // map |
| 81 | + |
| 82 | + test.assertTrue(response.containsKey("field1")); |
| 83 | + test.assertEquals(3, response.get("field1").toInteger()); |
| 84 | + |
| 85 | + test.assertTrue(response.containsKey("field2")); |
| 86 | + test.assertEquals(4, response.get("field2").toInteger()); |
| 87 | + |
| 88 | + try { |
| 89 | + response.get(0); |
| 90 | + test.fail("Map-typed Multi should fail on get(int)"); |
| 91 | + } catch (Exception expected) { |
| 92 | + } |
| 93 | + |
| 94 | + client.close(); |
| 95 | + async.complete(); |
| 96 | + }).onFailure(test::fail); |
| 97 | + } |
| 98 | +} |
0 commit comments