1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl.cache;
28
29 import static org.hamcrest.MatcherAssert.assertThat;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.concurrent.RejectedExecutionException;
35
36 import org.apache.hc.client5.http.cache.HeaderConstants;
37 import org.apache.hc.client5.http.schedule.SchedulingStrategy;
38 import org.apache.hc.core5.http.HttpResponse;
39 import org.apache.hc.core5.http.HttpStatus;
40 import org.apache.hc.core5.http.message.BasicHttpResponse;
41 import org.apache.hc.core5.util.TimeValue;
42 import org.apache.hc.core5.util.Timeout;
43 import org.hamcrest.CoreMatchers;
44 import org.junit.jupiter.api.Assertions;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.mockito.ArgumentMatchers;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50
51 public class TestCacheRevalidatorBase {
52
53 @Mock
54 private SchedulingStrategy mockSchedulingStrategy;
55 @Mock
56 private CacheRevalidatorBase.ScheduledExecutor mockScheduledExecutor;
57 @Mock
58 private Runnable mockOperation;
59
60 private CacheRevalidatorBase impl;
61
62
63 @BeforeEach
64 public void setUp() {
65 MockitoAnnotations.openMocks(this);
66 impl = new CacheRevalidatorBase(mockScheduledExecutor, mockSchedulingStrategy);
67 }
68
69 @Test
70 public void testRevalidateCacheEntrySchedulesExecutionAndPopulatesIdentifier() {
71 when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(1));
72
73 final String cacheKey = "blah";
74 impl.scheduleRevalidation(cacheKey, mockOperation);
75
76 verify(mockSchedulingStrategy).schedule(0);
77 verify(mockScheduledExecutor).schedule(ArgumentMatchers.same(mockOperation), ArgumentMatchers.eq(TimeValue.ofSeconds(1)));
78
79 Assertions.assertEquals(1, impl.getScheduledIdentifiers().size());
80 }
81
82 @Test
83 public void testMarkCompleteRemovesIdentifier() {
84 when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(3));
85
86 final String cacheKey = "blah";
87 impl.scheduleRevalidation(cacheKey, mockOperation);
88
89 verify(mockSchedulingStrategy).schedule(0);
90 verify(mockScheduledExecutor).schedule(ArgumentMatchers.any(), ArgumentMatchers.eq(TimeValue.ofSeconds(3)));
91
92 Assertions.assertEquals(1, impl.getScheduledIdentifiers().size());
93 Assertions.assertTrue(impl.getScheduledIdentifiers().contains(cacheKey));
94
95 impl.jobSuccessful(cacheKey);
96
97 Assertions.assertEquals(0, impl.getScheduledIdentifiers().size());
98 }
99
100 @Test
101 public void testRevalidateCacheEntryDoesNotPopulateIdentifierOnRejectedExecutionException() {
102 when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(2));
103 doThrow(new RejectedExecutionException()).when(mockScheduledExecutor).schedule(ArgumentMatchers.any(), ArgumentMatchers.any());
104
105 final String cacheKey = "blah";
106 impl.scheduleRevalidation(cacheKey, mockOperation);
107
108 Assertions.assertEquals(0, impl.getScheduledIdentifiers().size());
109 verify(mockScheduledExecutor).schedule(ArgumentMatchers.any(), ArgumentMatchers.eq(TimeValue.ofSeconds(2)));
110 }
111
112 @Test
113 public void testRevalidateCacheEntryProperlyCollapsesRequest() {
114 when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(2));
115
116 final String cacheKey = "blah";
117 impl.scheduleRevalidation(cacheKey, mockOperation);
118 impl.scheduleRevalidation(cacheKey, mockOperation);
119 impl.scheduleRevalidation(cacheKey, mockOperation);
120
121 verify(mockSchedulingStrategy).schedule(ArgumentMatchers.anyInt());
122 verify(mockScheduledExecutor).schedule(ArgumentMatchers.any(), ArgumentMatchers.eq(TimeValue.ofSeconds(2)));
123
124 Assertions.assertEquals(1, impl.getScheduledIdentifiers().size());
125 }
126
127 @Test
128 public void testStaleResponse() {
129 final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_OK);
130 response1.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\"");
131 assertThat(impl.isStale(response1), CoreMatchers.equalTo(true));
132
133 final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_OK);
134 response2.addHeader(HeaderConstants.WARNING, "111 localhost \"Revalidation failed\"");
135 assertThat(impl.isStale(response2), CoreMatchers.equalTo(true));
136
137 final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_OK);
138 response3.addHeader(HeaderConstants.WARNING, "xxx localhost \"Huh?\"");
139 assertThat(impl.isStale(response3), CoreMatchers.equalTo(false));
140
141 final HttpResponse response4 = new BasicHttpResponse(HttpStatus.SC_OK);
142 assertThat(impl.isStale(response4), CoreMatchers.equalTo(false));
143 }
144
145 @Test
146 public void testShutdown() throws Exception {
147 impl.close();
148 impl.awaitTermination(Timeout.ofMinutes(2));
149
150 verify(mockScheduledExecutor).shutdown();
151 verify(mockScheduledExecutor).awaitTermination(Timeout.ofMinutes(2));
152 }
153
154 }