Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Aug 6, 2024
1 parent 526cd43 commit 7458969
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ protected <T> Future<T> doExecute(
FutureCallback<T> callback
) {
try {
// Throw away the current thread context to simulate running async httpclient's thread pool
threadPool.getThreadContext().stashContext();
ClassicHttpRequest request = getRequest(requestProducer);
URL resource = resources[responseCount];
String path = paths[responseCount++];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,11 @@ protected List<String> featureValueOf(JsonLogLine actual) {

private void withThreadContext(CheckedConsumer<ThreadContext, Exception> consumer) throws Exception {
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
HeaderWarning.setThreadContext(threadContext);
consumer.accept(threadContext);
HeaderWarning.removeThreadContext(threadContext);
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
HeaderWarning.setThreadContext(threadContext);
consumer.accept(threadContext);
} finally {
HeaderWarning.removeThreadContext(threadContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public RefreshListeners(
final IntSupplier getMaxRefreshListeners,
final Runnable forceRefresh,
final Logger logger,
ThreadContext threadContext,
final ThreadContext threadContext,
final MeanMetric refreshMetric
) {
this.getMaxRefreshListeners = getMaxRefreshListeners;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.opensearch.plugins;

import org.opensearch.client.node.NodeClient;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.annotation.ExperimentalApi;
Expand Down Expand Up @@ -63,7 +63,7 @@ public interface TelemetryAwarePlugin {
* @param contextSwitcher A client for switching to plugin system context
*/
default Collection<Object> createComponents(
NodeClient client,
Client client,
ClusterService clusterService,
ThreadPool threadPool,
ResourceWatcherService resourceWatcherService,
Expand Down
4 changes: 2 additions & 2 deletions server/src/test/java/org/opensearch/node/NodeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.apache.lucene.tests.util.LuceneTestCase;
import org.opensearch.bootstrap.BootstrapCheck;
import org.opensearch.bootstrap.BootstrapContext;
import org.opensearch.client.node.NodeClient;
import org.opensearch.client.Client;
import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodeRole;
Expand Down Expand Up @@ -467,7 +467,7 @@ public MetricsRegistry getMetricsRegistry() {
public static class MockTelemetryAwarePlugin extends Plugin implements TelemetryAwarePlugin {
@Override
public Collection<Object> createComponents(
NodeClient client,
Client client,
ClusterService clusterService,
ThreadPool threadPool,
ResourceWatcherService resourceWatcherService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.usage.UsageService;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import java.util.Collections;
Expand Down Expand Up @@ -112,6 +113,12 @@ protected void doExecute(Task task, ActionRequest request, ActionListener listen
controller.registerHandler(action);
}

@Before
public void ensureCleanContext() {
// Make sure we have a clean context for each test
threadPool.getThreadContext().stashContext();
}

@AfterClass
public static void terminateThreadPool() throws InterruptedException {
terminate(threadPool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

package org.opensearch.telemetry.tracing;

import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;

import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
Expand All @@ -21,7 +19,6 @@
import org.opensearch.telemetry.tracing.noop.NoopTracer;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.telemetry.tracing.MockTracingTelemetry;
import org.opensearch.threadpool.ThreadPool;
import org.junit.After;
import org.junit.Before;

Expand All @@ -41,10 +38,8 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;

@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
public class ThreadContextBasedTracerContextStorageTests extends OpenSearchTestCase {
private Tracer tracer;
private ThreadPool threadPool;
private ThreadContext threadContext;
private TracerContextStorage<String, Span> threadContextStorage;
private ExecutorService executorService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ private void assertWarnings(boolean stripXContentPosition, List<String> actualWa
*/
private void resetDeprecationLogger() {
// "clear" context by stashing current values and dropping the returned StoredContext
// threadContext.stashContext();
threadContext.stashContext();
}

private static final List<StatusData> statusData = new ArrayList<>();
Expand Down

0 comments on commit 7458969

Please sign in to comment.