|
38 | 38 | import org.elasticsearch.common.collect.Tuple;
|
39 | 39 | import org.elasticsearch.common.settings.ClusterSettings;
|
40 | 40 | import org.elasticsearch.common.settings.Settings;
|
| 41 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
41 | 42 | import org.elasticsearch.test.ESTestCase;
|
42 | 43 | import org.elasticsearch.threadpool.ThreadPool;
|
43 | 44 |
|
|
61 | 62 | import static org.elasticsearch.test.VersionUtils.randomVersion;
|
62 | 63 | import static org.hamcrest.CoreMatchers.nullValue;
|
63 | 64 | import static org.hamcrest.CoreMatchers.startsWith;
|
| 65 | +import static org.hamcrest.Matchers.containsString; |
64 | 66 | import static org.hamcrest.Matchers.empty;
|
65 | 67 | import static org.hamcrest.Matchers.equalTo;
|
66 | 68 | import static org.hamcrest.Matchers.hasSize;
|
@@ -188,9 +190,16 @@ public void testUpdateTemplates() {
|
188 | 190 | additions.put("add_template_" + i, new BytesArray("{\"index_patterns\" : \"*\", \"order\" : " + i + "}"));
|
189 | 191 | }
|
190 | 192 |
|
191 |
| - TemplateUpgradeService service = new TemplateUpgradeService(Settings.EMPTY, mockClient, clusterService, null, |
| 193 | + ThreadPool threadPool = mock(ThreadPool.class); |
| 194 | + ThreadContext threadContext = new ThreadContext(Settings.EMPTY); |
| 195 | + when(threadPool.getThreadContext()).thenReturn(threadContext); |
| 196 | + TemplateUpgradeService service = new TemplateUpgradeService(Settings.EMPTY, mockClient, clusterService, threadPool, |
192 | 197 | Collections.emptyList());
|
193 | 198 |
|
| 199 | + IllegalStateException ise = expectThrows(IllegalStateException.class, () -> service.updateTemplates(additions, deletions)); |
| 200 | + assertThat(ise.getMessage(), containsString("template upgrade service should always happen in a system context")); |
| 201 | + |
| 202 | + threadContext.markAsSystemContext(); |
194 | 203 | service.updateTemplates(additions, deletions);
|
195 | 204 | int updatesInProgress = service.getUpdatesInProgress();
|
196 | 205 |
|
@@ -241,11 +250,14 @@ public void testClusterStateUpdate() {
|
241 | 250 | );
|
242 | 251 |
|
243 | 252 | ThreadPool threadPool = mock(ThreadPool.class);
|
| 253 | + ThreadContext threadContext = new ThreadContext(Settings.EMPTY); |
| 254 | + when(threadPool.getThreadContext()).thenReturn(threadContext); |
244 | 255 | ExecutorService executorService = mock(ExecutorService.class);
|
245 | 256 | when(threadPool.generic()).thenReturn(executorService);
|
246 | 257 | doAnswer(invocation -> {
|
247 | 258 | Object[] args = invocation.getArguments();
|
248 | 259 | assert args.length == 1;
|
| 260 | + assertTrue(threadContext.isSystemContext()); |
249 | 261 | Runnable runnable = (Runnable) args[0];
|
250 | 262 | runnable.run();
|
251 | 263 | updateInvocation.incrementAndGet();
|
|
0 commit comments