Skip to content
Permalink
Browse files

Make `DefaultThreadFactory` name prefix consistent with `IoThreadFact…

…ory` (#925)

Motivation:

`DefaultThreadFactory` has inconsistent name prefix calculations in
different constructors and with `IoThreadFactory`. Some ctors it adds
the `factoryCount` value and the last dash, some ctors don't.

Modifications:

- Calculate the final `namePrefix` in one ctor of `DefaultThreadFactory`;
- Make the `namePrefix` calculation consistent between
`DefaultThreadFactory` and `IoThreadFactory`;
- Remove redundant `-` symbol from existing name prefixes;

Result:

Name format of created threads is consistent between
`DefaultThreadFactory` and `IoThreadFactory`.
  • Loading branch information
idelpivnitskiy committed Jan 17, 2020
1 parent 6bb2722 commit c72d0800314bab021af028353de042ad60550bc7
Showing with 29 additions and 33 deletions.
  1. +1 −1 servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/DefaultExecutor.java
  2. +6 −10 servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/DefaultThreadFactory.java
  3. +1 −1 ...ncurrent-api/src/test/java/io/servicetalk/concurrent/api/CompletableExecutorPreservationTest.java
  4. +1 −1 ...concurrent-api/src/test/java/io/servicetalk/concurrent/api/PublisherExecutorPreservationTest.java
  5. +1 −1 ...lk-concurrent-api/src/test/java/io/servicetalk/concurrent/api/SingleExecutorPreservationTest.java
  6. +3 −3 ...rrent-api/src/test/java/io/servicetalk/concurrent/api/single/CompletionStageAsyncContextTest.java
  7. +3 −3 ...oncurrent-api/src/test/java/io/servicetalk/concurrent/api/single/SingleToCompletionStageTest.java
  8. +1 −1 servicetalk-http-netty/src/test/java/io/servicetalk/http/netty/BasicAuthStrategyInfluencerTest.java
  9. +1 −1 servicetalk-http-netty/src/test/java/io/servicetalk/http/netty/ClientEffectiveStrategyTest.java
  10. +1 −1 servicetalk-http-netty/src/test/java/io/servicetalk/http/netty/HttpClientOverrideOffloadingTest.java
  11. +1 −1 ...http-netty/src/test/java/io/servicetalk/http/netty/HttpStreamingClientOverrideOffloadingTest.java
  12. +2 −2 servicetalk-http-netty/src/test/java/io/servicetalk/http/netty/ServerEffectiveStrategyTest.java
  13. +1 −1 ...stFixtures/java/io/servicetalk/http/router/jersey/ExecutionStrategyConfigurationFailuresTest.java
  14. +2 −2 ...-router-jersey/src/testFixtures/java/io/servicetalk/http/router/jersey/ExecutionStrategyTest.java
  15. +1 −1 ...redicate/src/test/java/io/servicetalk/http/router/predicate/HttpServerOverrideOffloadingTest.java
  16. +2 −2 ...ternal/src/test/java/io/servicetalk/transport/netty/internal/RequestResponseCloseHandlerTest.java
  17. +1 −1 ...-internal/src/testFixtures/java/io/servicetalk/transport/netty/internal/ExecutionContextRule.java
@@ -52,7 +52,7 @@
* scheduler.
*/
private static final ScheduledExecutorService GLOBAL_SINGLE_THREADED_SCHEDULED_EXECUTOR =
newSingleThreadScheduledExecutor(new DefaultThreadFactory("servicetalk-global-scheduler-",
newSingleThreadScheduledExecutor(new DefaultThreadFactory("servicetalk-global-scheduler",
true, NORM_PRIORITY));
private static final RejectedExecutionHandler DEFAULT_REJECTION_HANDLER = new AbortPolicy();

@@ -28,13 +28,13 @@
public final class DefaultThreadFactory implements ThreadFactory {

private static final AtomicInteger factoryCount = new AtomicInteger();
public static final String DEFAULT_NAME_PREFIX = "servicetalk-executor-";
public static final String DEFAULT_NAME_PREFIX = "servicetalk-executor";

private final String namePrefix;
private final boolean daemon;
private final int priority;

@SuppressWarnings("unused")
private final AtomicInteger threadCount = new AtomicInteger();
private final String namePrefix;

/**
* New instance that creates daemon threads with {@link Thread#NORM_PRIORITY} priority.
@@ -67,7 +67,7 @@ public DefaultThreadFactory(int priority) {
* @param namePrefix for all created threads.
*/
public DefaultThreadFactory(String namePrefix) {
this(calculateName(namePrefix), true, NORM_PRIORITY);
this(namePrefix, true, NORM_PRIORITY);
}

/**
@@ -77,7 +77,7 @@ public DefaultThreadFactory(String namePrefix) {
* @param priority for the created threads.
*/
public DefaultThreadFactory(boolean daemon, int priority) {
this(calculateName(DEFAULT_NAME_PREFIX), daemon, priority);
this(DEFAULT_NAME_PREFIX, daemon, priority);
}

/**
@@ -88,13 +88,9 @@ public DefaultThreadFactory(boolean daemon, int priority) {
* @param priority for the created threads.
*/
public DefaultThreadFactory(String namePrefix, boolean daemon, int priority) {
this.namePrefix = requireNonNull(namePrefix) + '-' + factoryCount.incrementAndGet() + '-';
this.daemon = daemon;
this.priority = priority;
this.namePrefix = requireNonNull(namePrefix);
}

private static String calculateName(String prefix) {
return prefix + factoryCount.incrementAndGet() + '-';
}

@Override
@@ -27,7 +27,7 @@

public class CompletableExecutorPreservationTest {
@ClassRule
public static final ExecutorRule EXEC = ExecutorRule.withNamePrefix("test-");
public static final ExecutorRule EXEC = ExecutorRule.withNamePrefix("test");

private Completable completable;

@@ -23,7 +23,7 @@

public class PublisherExecutorPreservationTest {
@ClassRule
public static final ExecutorRule EXEC = ExecutorRule.withNamePrefix("test-");
public static final ExecutorRule EXEC = ExecutorRule.withNamePrefix("test");

private Publisher<String> publisher;

@@ -26,7 +26,7 @@

public class SingleExecutorPreservationTest {
@ClassRule
public static final ExecutorRule EXEC = ExecutorRule.withNamePrefix("test-");
public static final ExecutorRule EXEC = ExecutorRule.withNamePrefix("test");

private Single<String> single;

@@ -45,8 +45,8 @@
private static final Key<Integer> K1 = newKey("k1");
@Rule
public final ExecutorRule executorRule = ExecutorRule.withNamePrefix(ST_THREAD_PREFIX_NAME);
private static final String ST_THREAD_PREFIX_NAME = "st-exec-thread-";
private static final String JDK_THREAD_NAME_PREFIX = "jdk-thread-";
private static final String ST_THREAD_PREFIX_NAME = "st-exec-thread";
private static final String JDK_THREAD_NAME_PREFIX = "jdk-thread";
private static final AtomicInteger threadCount = new AtomicInteger();
@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
@@ -58,7 +58,7 @@
@BeforeClass
public static void beforeClass() {
jdkExecutor = java.util.concurrent.Executors.newCachedThreadPool(
r -> new Thread(r, JDK_THREAD_NAME_PREFIX + threadCount.incrementAndGet()));
r -> new Thread(r, JDK_THREAD_NAME_PREFIX + '-' + threadCount.incrementAndGet()));
}

@AfterClass
@@ -68,15 +68,15 @@
private LegacyTestSingle<String> source;
private static ExecutorService jdkExecutor;
private static final AtomicInteger threadCount = new AtomicInteger();
private static final String ST_THREAD_PREFIX_NAME = "st-exec-thread-";
private static final String JDK_THREAD_NAME_PREFIX = "jdk-thread-";
private static final String ST_THREAD_PREFIX_NAME = "st-exec-thread";
private static final String JDK_THREAD_NAME_PREFIX = "jdk-thread";
private static final String JDK_FORK_JOIN_THREAD_NAME_PREFIX = "ForkJoinPool";
private static final String COMPLETABLE_FUTURE_THREAD_PER_TASK_NAME_PREFIX = "Thread-";

@BeforeClass
public static void beforeClass() {
jdkExecutor = java.util.concurrent.Executors.newCachedThreadPool(
r -> new Thread(r, JDK_THREAD_NAME_PREFIX + threadCount.incrementAndGet()));
r -> new Thread(r, JDK_THREAD_NAME_PREFIX + '-' + threadCount.incrementAndGet()));
}

@AfterClass
@@ -72,7 +72,7 @@

@RunWith(MockitoJUnitRunner.class)
public class BasicAuthStrategyInfluencerTest {
public static final String IO_EXECUTOR_NAME_PREFIX = "io-executor-";
public static final String IO_EXECUTOR_NAME_PREFIX = "io-executor";

@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
@@ -399,7 +399,7 @@ public String toString() {
}

private static final class Params {
private static final String USER_STRATEGY_EXECUTOR_NAME_PREFIX = "user-strategy-executor-";
private static final String USER_STRATEGY_EXECUTOR_NAME_PREFIX = "user-strategy-executor";


private final Map<ClientType, List<ClientOffloadPoint>> offloadPoints;
@@ -52,7 +52,7 @@

@RunWith(Parameterized.class)
public class HttpClientOverrideOffloadingTest {
private static final String IO_EXECUTOR_THREAD_NAME_PREFIX = "http-client-io-executor-";
private static final String IO_EXECUTOR_THREAD_NAME_PREFIX = "http-client-io-executor";

@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
@@ -54,7 +54,7 @@

@RunWith(Parameterized.class)
public class HttpStreamingClientOverrideOffloadingTest {
private static final String IO_EXECUTOR_THREAD_NAME_PREFIX = "http-client-io-executor-";
private static final String IO_EXECUTOR_THREAD_NAME_PREFIX = "http-client-io-executor";

@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
@@ -289,8 +289,8 @@ public String toString() {
}

private static final class Params {
private static final String USER_STRATEGY_EXECUTOR_NAME_PREFIX = "user-strategy-executor-";
private static final String SERVICE_EXECUTOR_NAME_PREFIX = "service-executor-";
private static final String USER_STRATEGY_EXECUTOR_NAME_PREFIX = "user-strategy-executor";
private static final String SERVICE_EXECUTOR_NAME_PREFIX = "service-executor";


private final Map<ServiceType, List<ServerOffloadPoint>> offloadPoints;
@@ -41,7 +41,7 @@
public final ExpectedException expected = ExpectedException.none();

@ClassRule
public static final ExecutorRule TEST_EXEC = ExecutorRule.withNamePrefix("test-");
public static final ExecutorRule TEST_EXEC = ExecutorRule.withNamePrefix("test");

@Test
public void invalidStrategies() {
@@ -72,10 +72,10 @@
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

@ClassRule
public static final ExecutorRule<Executor> ROUTER_EXEC = ExecutorRule.withNamePrefix("router-");
public static final ExecutorRule<Executor> ROUTER_EXEC = ExecutorRule.withNamePrefix("router");

@ClassRule
public static final ExecutorRule<Executor> ROUTE_EXEC = ExecutorRule.withNamePrefix("route-");
public static final ExecutorRule<Executor> ROUTE_EXEC = ExecutorRule.withNamePrefix("route");

public static class TestApplication extends Application {
@Override
@@ -59,7 +59,7 @@
import static org.hamcrest.Matchers.is;

public class HttpServerOverrideOffloadingTest {
private static final String IO_EXECUTOR_THREAD_NAME_PREFIX = "http-server-io-executor-";
private static final String IO_EXECUTOR_THREAD_NAME_PREFIX = "http-server-io-executor";

@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
@@ -540,11 +540,11 @@ public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt
@ClassRule
public static final ExecutionContextRule C_CTX = new ExecutionContextRule(() -> DEFAULT_ALLOCATOR,
() -> createIoExecutor(
new DefaultThreadFactory("client-thread-", true, NORM_PRIORITY)), Executors::immediate);
new DefaultThreadFactory("client-thread", true, NORM_PRIORITY)), Executors::immediate);
@ClassRule
public static final ExecutionContextRule S_CTX = new ExecutionContextRule(() -> DEFAULT_ALLOCATOR,
() -> createIoExecutor(
new DefaultThreadFactory("server-thread-", true, NORM_PRIORITY)), Executors::immediate);
new DefaultThreadFactory("server-thread", true, NORM_PRIORITY)), Executors::immediate);

private SocketChannel cChannel;
private volatile SocketChannel sChannel;
@@ -42,7 +42,7 @@
* Can be used with a @{@link Rule} field and a {@code static} field with @{@link ClassRule}.
*/
public final class ExecutionContextRule extends ExternalResource implements ExecutionContext {
private static final String IO_THREAD_PREFIX = "exec-ctx-rule-io-";
private static final String IO_THREAD_PREFIX = "exec-ctx-rule-io";
private final Supplier<Executor> executorSupplier;
private final Supplier<IoExecutor> ioExecutorSupplier;
private final Supplier<BufferAllocator> allocatorSupplier;

0 comments on commit c72d080

Please sign in to comment.
You can’t perform that action at this time.