|
| 1 | +package redis.clients.jedis; |
| 2 | + |
| 3 | +import junit.framework.TestCase; |
| 4 | +import redis.clients.jedis.util.SafeEncoder; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.concurrent.CountDownLatch; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | + |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | +import static redis.clients.jedis.Protocol.ResponseKeyword.MESSAGE; |
| 14 | +import static redis.clients.jedis.Protocol.ResponseKeyword.SUBSCRIBE; |
| 15 | + |
| 16 | +public class JedisPubSubBaseTest extends TestCase { |
| 17 | + |
| 18 | + public void testProceed_givenThreadInterrupt_exitLoop() throws InterruptedException { |
| 19 | + // setup |
| 20 | + final JedisPubSubBase<String> pubSub = new JedisPubSubBase<String>() { |
| 21 | + |
| 22 | + @Override |
| 23 | + public void onMessage(String channel, String message) { |
| 24 | + fail("this should not happen when thread is interrupted"); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + protected String encode(byte[] raw) { |
| 29 | + return SafeEncoder.encode(raw); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + final Connection mockConnection = mock(Connection.class); |
| 34 | + final List<Object> mockSubscribe = Arrays.asList( |
| 35 | + SUBSCRIBE.getRaw(), "channel".getBytes(), 1L |
| 36 | + ); |
| 37 | + final List<Object> mockResponse = Arrays.asList( |
| 38 | + MESSAGE.getRaw(), "channel".getBytes(), "message".getBytes() |
| 39 | + ); |
| 40 | + |
| 41 | + when(mockConnection.getUnflushedObject()). |
| 42 | + |
| 43 | + thenReturn(mockSubscribe, mockResponse); |
| 44 | + |
| 45 | + |
| 46 | + final CountDownLatch countDownLatch = new CountDownLatch(1); |
| 47 | + // action |
| 48 | + final Thread thread = new Thread(() -> { |
| 49 | + Thread.currentThread().interrupt(); |
| 50 | + pubSub.proceed(mockConnection, "channel"); |
| 51 | + |
| 52 | + countDownLatch.countDown(); |
| 53 | + }); |
| 54 | + thread.start(); |
| 55 | + |
| 56 | + assertTrue(countDownLatch.await(10, TimeUnit.MILLISECONDS)); |
| 57 | + |
| 58 | + } |
| 59 | +} |
0 commit comments