Skip to content

Commit

Permalink
Use a dedicated test executor in MockTransportService (elastic#112748)
Browse files Browse the repository at this point in the history
Instead of using the generic executor for delayed transport actions,
this PR adds a new executor to schedule these actions. It helps avoid
sharing executors with the node which may lead to unexpected CI failures
due to unsafe future assertion.
  • Loading branch information
ywangd authored Sep 12, 2024
1 parent ed41445 commit 6a8ac53
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.common.util.MockPageCacheRecycler;
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor;
import org.elasticsearch.common.util.concurrent.RunOnce;
import org.elasticsearch.core.AbstractRefCounted;
import org.elasticsearch.core.IOUtils;
Expand Down Expand Up @@ -81,6 +83,7 @@
import java.util.function.Supplier;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;

/**
Expand Down Expand Up @@ -206,6 +209,7 @@ public static MockTransportService getInstance(String nodeName) {
}

private final Transport original;
private final EsThreadPoolExecutor testExecutor;

/**
* Build the service.
Expand Down Expand Up @@ -302,6 +306,16 @@ private MockTransportService(
Tracer.NOOP
);
this.original = transport.getDelegate();
this.testExecutor = EsExecutors.newScaling(
"mock-transport",
0,
4,
30,
TimeUnit.SECONDS,
true,
EsExecutors.daemonThreadFactory("mock-transport"),
threadPool.getThreadContext()
);
}

private static TransportAddress[] extractTransportAddresses(TransportService transportService) {
Expand Down Expand Up @@ -617,7 +631,7 @@ protected void doRun() throws IOException {
delay
)
);
threadPool.schedule(runnable, delay, threadPool.generic());
threadPool.schedule(runnable, delay, testExecutor);
}
}
}
Expand Down Expand Up @@ -795,6 +809,8 @@ protected void doClose() throws IOException {
}
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} finally {
assertTrue(ThreadPool.terminate(testExecutor, 10, TimeUnit.SECONDS));
}
}

Expand Down

0 comments on commit 6a8ac53

Please sign in to comment.