|
16 | 16 | import org.elasticsearch.cluster.service.ClusterService;
|
17 | 17 | import org.elasticsearch.common.Priority;
|
18 | 18 | import org.elasticsearch.common.Randomness;
|
| 19 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
19 | 20 | import org.elasticsearch.test.ClusterServiceUtils;
|
20 | 21 | import org.elasticsearch.test.ESTestCase;
|
21 | 22 | import org.elasticsearch.threadpool.TestThreadPool;
|
|
34 | 35 | import java.util.concurrent.atomic.AtomicLong;
|
35 | 36 | import java.util.function.Function;
|
36 | 37 |
|
| 38 | +import static org.hamcrest.Matchers.equalTo; |
37 | 39 | import static org.hamcrest.Matchers.lessThan;
|
38 | 40 |
|
39 | 41 | public class BatchedRerouteServiceTests extends ESTestCase {
|
@@ -99,16 +101,24 @@ public void onFailure(String source, Exception e) {
|
99 | 101 | return s;
|
100 | 102 | });
|
101 | 103 |
|
| 104 | + final ThreadContext threadContext = threadPool.getThreadContext(); |
| 105 | + final String contextHeader = "test-context-header"; |
| 106 | + |
102 | 107 | final int iterations = scaledRandomIntBetween(1, 100);
|
103 | 108 | final CountDownLatch tasksSubmittedCountDown = new CountDownLatch(iterations);
|
104 | 109 | final CountDownLatch tasksCompletedCountDown = new CountDownLatch(iterations);
|
105 | 110 | final List<Runnable> actions = new ArrayList<>(iterations);
|
106 | 111 | final Function<Priority, Runnable> rerouteFromPriority = priority -> () -> {
|
107 | 112 | final AtomicBoolean alreadyRun = new AtomicBoolean();
|
108 |
| - batchedRerouteService.reroute("reroute at " + priority, priority, ActionListener.wrap(() -> { |
109 |
| - assertTrue(alreadyRun.compareAndSet(false, true)); |
110 |
| - tasksCompletedCountDown.countDown(); |
111 |
| - })); |
| 113 | + try (ThreadContext.StoredContext ignored = threadContext.stashContext()) { |
| 114 | + final String contextValue = randomAlphaOfLength(10); |
| 115 | + threadContext.putHeader(contextHeader, contextValue); |
| 116 | + batchedRerouteService.reroute("reroute at " + priority, priority, ActionListener.wrap(() -> { |
| 117 | + assertTrue(alreadyRun.compareAndSet(false, true)); |
| 118 | + assertThat(threadContext.getHeader(contextHeader), equalTo(contextValue)); |
| 119 | + tasksCompletedCountDown.countDown(); |
| 120 | + })); |
| 121 | + } |
112 | 122 | tasksSubmittedCountDown.countDown();
|
113 | 123 | };
|
114 | 124 | actions.add(rerouteFromPriority.apply(Priority.URGENT)); // ensure at least one URGENT priority reroute
|
|
0 commit comments