|
25 | 25 | */ |
26 | 26 | public class JedisTest { |
27 | 27 |
|
28 | | - private Jedis jedis; |
29 | | - private static RedisServer redisServer; |
| 28 | + private Jedis jedis; |
| 29 | + private static RedisServer redisServer; |
30 | 30 |
|
31 | | - public JedisTest() { |
32 | | - jedis = new Jedis(); |
33 | | - } |
| 31 | + public JedisTest() { |
| 32 | + jedis = new Jedis(); |
| 33 | + } |
34 | 34 |
|
35 | | - @BeforeClass |
36 | | - public static void setUp() throws IOException { |
37 | | - redisServer = new RedisServer(6379); |
38 | | - redisServer.start(); |
39 | | - } |
| 35 | + @BeforeClass |
| 36 | + public static void setUp() throws IOException { |
| 37 | + redisServer = new RedisServer(6379); |
| 38 | + redisServer.start(); |
| 39 | + } |
40 | 40 |
|
41 | | - @AfterClass |
42 | | - public static void destroy() { |
43 | | - redisServer.stop(); |
44 | | - } |
| 41 | + @AfterClass |
| 42 | + public static void destroy() { |
| 43 | + redisServer.stop(); |
| 44 | + } |
45 | 45 |
|
46 | | - @After |
47 | | - public void flush() { |
48 | | - jedis.flushAll(); |
49 | | - } |
| 46 | + @After |
| 47 | + public void flush() { |
| 48 | + jedis.flushAll(); |
| 49 | + } |
50 | 50 |
|
51 | | - @Test |
52 | | - public void givenAStringThenSaveItAsRedisStrings() { |
53 | | - String key = "key"; |
54 | | - String value = "value"; |
| 51 | + @Test |
| 52 | + public void givenAStringThenSaveItAsRedisStrings() { |
| 53 | + String key = "key"; |
| 54 | + String value = "value"; |
55 | 55 |
|
56 | | - jedis.set(key, value); |
57 | | - String value2 = jedis.get(key); |
| 56 | + jedis.set(key, value); |
| 57 | + String value2 = jedis.get(key); |
58 | 58 |
|
59 | | - Assert.assertEquals(value, value2); |
60 | | - } |
| 59 | + Assert.assertEquals(value, value2); |
| 60 | + } |
61 | 61 |
|
62 | | - @Test |
63 | | - public void givenListElementsThenSaveThemInRedisList() { |
64 | | - String queue = "queue#tasks"; |
| 62 | + @Test |
| 63 | + public void givenListElementsThenSaveThemInRedisList() { |
| 64 | + String queue = "queue#tasks"; |
65 | 65 |
|
66 | | - String taskOne = "firstTask"; |
67 | | - String taskTwo = "secondTask"; |
68 | | - String taskThree = "thirdTask"; |
| 66 | + String taskOne = "firstTask"; |
| 67 | + String taskTwo = "secondTask"; |
| 68 | + String taskThree = "thirdTask"; |
69 | 69 |
|
70 | | - jedis.lpush(queue, taskOne, taskTwo); |
| 70 | + jedis.lpush(queue, taskOne, taskTwo); |
71 | 71 |
|
72 | | - String taskReturnedOne = jedis.rpop(queue); |
| 72 | + String taskReturnedOne = jedis.rpop(queue); |
73 | 73 |
|
74 | | - jedis.lpush(queue, taskThree); |
75 | | - Assert.assertEquals(taskOne, taskReturnedOne); |
| 74 | + jedis.lpush(queue, taskThree); |
| 75 | + Assert.assertEquals(taskOne, taskReturnedOne); |
76 | 76 |
|
77 | | - String taskReturnedTwo = jedis.rpop(queue); |
78 | | - String taskReturnedThree = jedis.rpop(queue); |
| 77 | + String taskReturnedTwo = jedis.rpop(queue); |
| 78 | + String taskReturnedThree = jedis.rpop(queue); |
79 | 79 |
|
80 | | - Assert.assertEquals(taskTwo, taskReturnedTwo); |
81 | | - Assert.assertEquals(taskThree, taskReturnedThree); |
| 80 | + Assert.assertEquals(taskTwo, taskReturnedTwo); |
| 81 | + Assert.assertEquals(taskThree, taskReturnedThree); |
82 | 82 |
|
83 | | - String taskReturnedFour = jedis.rpop(queue); |
84 | | - Assert.assertNull(taskReturnedFour); |
85 | | - } |
| 83 | + String taskReturnedFour = jedis.rpop(queue); |
| 84 | + Assert.assertNull(taskReturnedFour); |
| 85 | + } |
86 | 86 |
|
87 | | - @Test |
88 | | - public void givenSetElementsThenSaveThemInRedisSet() { |
89 | | - String countries = "countries"; |
| 87 | + @Test |
| 88 | + public void givenSetElementsThenSaveThemInRedisSet() { |
| 89 | + String countries = "countries"; |
90 | 90 |
|
91 | | - String countryOne = "Spain"; |
92 | | - String countryTwo = "Ireland"; |
93 | | - String countryThree = "Ireland"; |
| 91 | + String countryOne = "Spain"; |
| 92 | + String countryTwo = "Ireland"; |
| 93 | + String countryThree = "Ireland"; |
94 | 94 |
|
95 | | - jedis.sadd(countries, countryOne); |
| 95 | + jedis.sadd(countries, countryOne); |
96 | 96 |
|
97 | | - Set<String> countriesSet = jedis.smembers(countries); |
98 | | - Assert.assertEquals(1, countriesSet.size()); |
| 97 | + Set<String> countriesSet = jedis.smembers(countries); |
| 98 | + Assert.assertEquals(1, countriesSet.size()); |
99 | 99 |
|
100 | | - jedis.sadd(countries, countryTwo); |
101 | | - countriesSet = jedis.smembers(countries); |
102 | | - Assert.assertEquals(2, countriesSet.size()); |
| 100 | + jedis.sadd(countries, countryTwo); |
| 101 | + countriesSet = jedis.smembers(countries); |
| 102 | + Assert.assertEquals(2, countriesSet.size()); |
103 | 103 |
|
104 | | - jedis.sadd(countries, countryThree); |
105 | | - countriesSet = jedis.smembers(countries); |
106 | | - Assert.assertEquals(2, countriesSet.size()); |
| 104 | + jedis.sadd(countries, countryThree); |
| 105 | + countriesSet = jedis.smembers(countries); |
| 106 | + Assert.assertEquals(2, countriesSet.size()); |
107 | 107 |
|
108 | | - boolean exists = jedis.sismember(countries, countryThree); |
109 | | - Assert.assertTrue(exists); |
110 | | - } |
| 108 | + boolean exists = jedis.sismember(countries, countryThree); |
| 109 | + Assert.assertTrue(exists); |
| 110 | + } |
111 | 111 |
|
112 | | - @Test |
113 | | - public void givenObjectFieldsThenSaveThemInRedisHash() { |
114 | | - String key = "user#1"; |
| 112 | + @Test |
| 113 | + public void givenObjectFieldsThenSaveThemInRedisHash() { |
| 114 | + String key = "user#1"; |
115 | 115 |
|
116 | | - String field = "name"; |
117 | | - String value = "William"; |
| 116 | + String field = "name"; |
| 117 | + String value = "William"; |
118 | 118 |
|
119 | | - String field2 = "job"; |
120 | | - String value2 = "politician"; |
| 119 | + String field2 = "job"; |
| 120 | + String value2 = "politician"; |
121 | 121 |
|
122 | | - jedis.hset(key, field, value); |
123 | | - jedis.hset(key, field2, value2); |
| 122 | + jedis.hset(key, field, value); |
| 123 | + jedis.hset(key, field2, value2); |
124 | 124 |
|
125 | | - String value3 = jedis.hget(key, field); |
126 | | - Assert.assertEquals(value, value3); |
| 125 | + String value3 = jedis.hget(key, field); |
| 126 | + Assert.assertEquals(value, value3); |
127 | 127 |
|
128 | | - Map<String, String> fields = jedis.hgetAll(key); |
129 | | - String value4 = fields.get(field2); |
130 | | - Assert.assertEquals(value2, value4); |
131 | | - } |
| 128 | + Map<String, String> fields = jedis.hgetAll(key); |
| 129 | + String value4 = fields.get(field2); |
| 130 | + Assert.assertEquals(value2, value4); |
| 131 | + } |
132 | 132 |
|
133 | | - @Test |
134 | | - public void givenARankingThenSaveItInRedisSortedSet() { |
135 | | - String key = "ranking"; |
| 133 | + @Test |
| 134 | + public void givenARankingThenSaveItInRedisSortedSet() { |
| 135 | + String key = "ranking"; |
136 | 136 |
|
137 | | - Map<String, Double> scores = new HashMap<>(); |
| 137 | + Map<String, Double> scores = new HashMap<>(); |
138 | 138 |
|
139 | | - scores.put("PlayerOne", 3000.0); |
140 | | - scores.put("PlayerTwo", 1500.0); |
141 | | - scores.put("PlayerThree", 8200.0); |
| 139 | + scores.put("PlayerOne", 3000.0); |
| 140 | + scores.put("PlayerTwo", 1500.0); |
| 141 | + scores.put("PlayerThree", 8200.0); |
142 | 142 |
|
143 | | - for (String player : scores.keySet()) { |
144 | | - jedis.zadd(key, scores.get(player), player); |
145 | | - } |
| 143 | + for (String player : scores.keySet()) { |
| 144 | + jedis.zadd(key, scores.get(player), player); |
| 145 | + } |
146 | 146 |
|
147 | | - Set<String> players = jedis.zrevrange(key, 0, 1); |
148 | | - Assert.assertEquals("PlayerThree", players.iterator().next()); |
| 147 | + Set<String> players = jedis.zrevrange(key, 0, 1); |
| 148 | + Assert.assertEquals("PlayerThree", players.iterator().next()); |
149 | 149 |
|
150 | | - long rank = jedis.zrevrank(key, "PlayerOne"); |
151 | | - Assert.assertEquals(1, rank); |
152 | | - } |
| 150 | + long rank = jedis.zrevrank(key, "PlayerOne"); |
| 151 | + Assert.assertEquals(1, rank); |
| 152 | + } |
153 | 153 |
|
154 | | - @Test |
155 | | - public void givenMultipleOperationsThatNeedToBeExecutedAtomicallyThenWrapThemInATransaction() { |
156 | | - String friendsPrefix = "friends#"; |
| 154 | + @Test |
| 155 | + public void givenMultipleOperationsThatNeedToBeExecutedAtomicallyThenWrapThemInATransaction() { |
| 156 | + String friendsPrefix = "friends#"; |
157 | 157 |
|
158 | | - String userOneId = "4352523"; |
159 | | - String userTwoId = "5552321"; |
| 158 | + String userOneId = "4352523"; |
| 159 | + String userTwoId = "5552321"; |
160 | 160 |
|
161 | | - Transaction t = jedis.multi(); |
162 | | - t.sadd(friendsPrefix + userOneId, userTwoId); |
163 | | - t.sadd(friendsPrefix + userTwoId, userOneId); |
164 | | - t.exec(); |
| 161 | + Transaction t = jedis.multi(); |
| 162 | + t.sadd(friendsPrefix + userOneId, userTwoId); |
| 163 | + t.sadd(friendsPrefix + userTwoId, userOneId); |
| 164 | + t.exec(); |
165 | 165 |
|
166 | | - boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId); |
167 | | - Assert.assertTrue(exists); |
| 166 | + boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId); |
| 167 | + Assert.assertTrue(exists); |
168 | 168 |
|
169 | | - exists = jedis.sismember(friendsPrefix + userTwoId, userOneId); |
170 | | - Assert.assertTrue(exists); |
171 | | - } |
| 169 | + exists = jedis.sismember(friendsPrefix + userTwoId, userOneId); |
| 170 | + Assert.assertTrue(exists); |
| 171 | + } |
172 | 172 |
|
173 | | - @Test |
174 | | - public void givenMultipleIndependentOperationsWhenNetworkOptimizationIsImportantThenWrapThemInAPipeline() { |
175 | | - String userOneId = "4352523"; |
176 | | - String userTwoId = "4849888"; |
177 | | - |
178 | | - Pipeline p = jedis.pipelined(); |
179 | | - p.sadd("searched#" + userOneId, "paris"); |
180 | | - p.zadd("ranking", 126, userOneId); |
181 | | - p.zadd("ranking", 325, userTwoId); |
182 | | - Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris"); |
183 | | - Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1); |
184 | | - p.sync(); |
185 | | - |
186 | | - Assert.assertTrue(pipeExists.get()); |
187 | | - Assert.assertEquals(2, pipeRanking.get().size()); |
188 | | - } |
189 | | - |
190 | | - @Test |
191 | | - public void givenAPoolConfigurationThenCreateAJedisPool() { |
192 | | - final JedisPoolConfig poolConfig = buildPoolConfig(); |
193 | | - |
194 | | - try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) { |
195 | | - |
196 | | - // do simple operation to verify that the Jedis resource is working |
197 | | - // properly |
198 | | - String key = "key"; |
199 | | - String value = "value"; |
200 | | - |
201 | | - jedis.set(key, value); |
202 | | - String value2 = jedis.get(key); |
203 | | - |
204 | | - Assert.assertEquals(value, value2); |
205 | | - |
206 | | - // flush Redis |
207 | | - jedis.flushAll(); |
208 | | - } |
209 | | - } |
210 | | - |
211 | | - private JedisPoolConfig buildPoolConfig() { |
212 | | - final JedisPoolConfig poolConfig = new JedisPoolConfig(); |
213 | | - poolConfig.setMaxTotal(128); |
214 | | - poolConfig.setMaxIdle(128); |
215 | | - poolConfig.setMinIdle(16); |
216 | | - poolConfig.setTestOnBorrow(true); |
217 | | - poolConfig.setTestOnReturn(true); |
218 | | - poolConfig.setTestWhileIdle(true); |
219 | | - poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis()); |
220 | | - poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis()); |
221 | | - poolConfig.setNumTestsPerEvictionRun(3); |
222 | | - poolConfig.setBlockWhenExhausted(true); |
223 | | - return poolConfig; |
224 | | - } |
| 173 | + @Test |
| 174 | + public void givenMultipleIndependentOperationsWhenNetworkOptimizationIsImportantThenWrapThemInAPipeline() { |
| 175 | + String userOneId = "4352523"; |
| 176 | + String userTwoId = "4849888"; |
| 177 | + |
| 178 | + Pipeline p = jedis.pipelined(); |
| 179 | + p.sadd("searched#" + userOneId, "paris"); |
| 180 | + p.zadd("ranking", 126, userOneId); |
| 181 | + p.zadd("ranking", 325, userTwoId); |
| 182 | + Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris"); |
| 183 | + Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1); |
| 184 | + p.sync(); |
| 185 | + |
| 186 | + Assert.assertTrue(pipeExists.get()); |
| 187 | + Assert.assertEquals(2, pipeRanking.get().size()); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void givenAPoolConfigurationThenCreateAJedisPool() { |
| 192 | + final JedisPoolConfig poolConfig = buildPoolConfig(); |
| 193 | + |
| 194 | + try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) { |
| 195 | + |
| 196 | + // do simple operation to verify that the Jedis resource is working |
| 197 | + // properly |
| 198 | + String key = "key"; |
| 199 | + String value = "value"; |
| 200 | + |
| 201 | + jedis.set(key, value); |
| 202 | + String value2 = jedis.get(key); |
| 203 | + |
| 204 | + Assert.assertEquals(value, value2); |
| 205 | + |
| 206 | + // flush Redis |
| 207 | + jedis.flushAll(); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private JedisPoolConfig buildPoolConfig() { |
| 212 | + final JedisPoolConfig poolConfig = new JedisPoolConfig(); |
| 213 | + poolConfig.setMaxTotal(128); |
| 214 | + poolConfig.setMaxIdle(128); |
| 215 | + poolConfig.setMinIdle(16); |
| 216 | + poolConfig.setTestOnBorrow(true); |
| 217 | + poolConfig.setTestOnReturn(true); |
| 218 | + poolConfig.setTestWhileIdle(true); |
| 219 | + poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis()); |
| 220 | + poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis()); |
| 221 | + poolConfig.setNumTestsPerEvictionRun(3); |
| 222 | + poolConfig.setBlockWhenExhausted(true); |
| 223 | + return poolConfig; |
| 224 | + } |
225 | 225 | } |
0 commit comments