View Javadoc

1   package com.atlassian.messagequeue.internal.sqs;
2   
3   import static org.hamcrest.CoreMatchers.is;
4   import static org.junit.Assert.assertThat;
5   import static org.mockito.Mockito.*;
6   
7   import com.amazonaws.services.sqs.AmazonSQSClient;
8   import com.amazonaws.services.sqs.model.MessageNotInflightException;
9   import org.hamcrest.CoreMatchers;
10  import org.hamcrest.Matchers;
11  import org.junit.After;
12  import org.junit.Assert;
13  import org.junit.Before;
14  import org.junit.Test;
15  import org.mockito.Mock;
16  import org.junit.Rule;
17  import org.mockito.junit.MockitoJUnit;
18  import org.mockito.junit.MockitoRule;
19  
20  import java.util.concurrent.Future;
21  
22  public class SQSMessageVisibilityTimeoutManagerTest {
23      @Rule
24      public MockitoRule rule = MockitoJUnit.rule();
25  
26      @Mock
27      AmazonSQSClient sqsClient;
28      private SQSMessageVisibilityTimeoutManager manager;
29      private int baseVisibilityTimeoutSeconds;
30  
31      @Before
32      public void setUp() throws Exception {
33          baseVisibilityTimeoutSeconds = 1;
34          manager = new SQSMessageVisibilityTimeoutManager(sqsClient, "queueUrl", baseVisibilityTimeoutSeconds, 1, 1);
35      }
36  
37      @Test
38      public void stopExtendingVisibilityTimeoutOnException() throws Exception {
39          doThrow(new MessageNotInflightException("message not in flight")).when(sqsClient).changeMessageVisibility(anyString(), anyString(), anyInt());
40  
41          final Future<?> future = manager.scheduleVisibilityTimeoutExtension("receiptHandle");
42  
43          verify(sqsClient, after(baseVisibilityTimeoutSeconds * 1000 * 2).times(1)).changeMessageVisibility(anyString(), anyString(), anyInt());
44          assertThat(future.isDone(), is(true));
45          assertThat(manager.visibilityTimeoutExtensionWorkerPool.getQueue(), Matchers.empty());
46      }
47  
48      @Test
49      public void visibilityTimeoutExtensionRecurs() throws Exception {
50          manager.scheduleVisibilityTimeoutExtension("receiptHandle");
51  
52          // Within 2500ms, the extension task is enabled after a delay of 1000ms and then again at 2000ms producing 2 wanted invocations
53          final int wantedNumberOfInvocations = 2;
54          verify(sqsClient, after(2500).times(wantedNumberOfInvocations)).changeMessageVisibility(anyString(), anyString(), anyInt());
55      }
56  
57      @After
58      public void tearDown() throws Exception {
59          manager.shutdown();
60      }
61  }