1 package com.atlassian.vcache.internal.redis;
2
3 import org.junit.After;
4 import org.junit.Rule;
5 import org.junit.Test;
6 import org.junit.rules.ExpectedException;
7 import redis.clients.jedis.Jedis;
8
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors;
11
12 import static org.hamcrest.Matchers.instanceOf;
13 import static org.hamcrest.Matchers.sameInstance;
14 import static org.junit.Assert.assertThat;
15
16 public class ThreadAffinityJedisPoolIT {
17
18 private final ExecutorService executor = Executors.newSingleThreadExecutor();
19
20 @Rule
21 public JedisTester tester = new JedisTester();
22
23 @Rule
24 public ExpectedException expectedException = ExpectedException.none();
25
26 @After
27 public void after() {
28 executor.shutdownNow();
29 }
30
31 @Test
32 public void get_get_close_close() {
33 Jedis obj1 = tester.jedisPool.getResource();
34 Jedis obj2 = tester.jedisPool.getResource();
35
36 assertThat(obj1, sameInstance(obj2));
37
38 tester.jedisPool.returnResource(obj1);
39 tester.jedisPool.returnResource(obj2);
40 }
41
42 @Test
43 public void get_return_return() {
44 Jedis obj = tester.jedisPool.getResource();
45 tester.jedisPool.returnResource(obj);
46
47 expectedException.expect(instanceOf(IllegalStateException.class));
48 tester.jedisPool.returnResource(obj);
49 }
50
51 @Test
52 public void get_return_differentThread() throws Exception {
53 Jedis obj = tester.jedisPool.getResource();
54
55 expectedException.expectCause(instanceOf(IllegalStateException.class));
56 executor.submit(() -> tester.jedisPool.returnResource(obj)).get();
57 }
58
59 @Test
60 public void return_wrongInstance() throws Exception {
61 tester.jedisPool.getResource();
62 Jedis obj2 = new Jedis();
63
64 expectedException.expect(instanceOf(IllegalStateException.class));
65 tester.jedisPool.returnResource(obj2);
66 }
67 }