Skip to content

Commit 44b2d59

Browse files
authored
Improve #917 not to rely on #toString() method behavior (#921)
1 parent caaa7ca commit 44b2d59

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

slack-api-client/src/main/java/com/slack/api/audit/impl/ThreadPools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static ExecutorService getDefault(AuditConfig config) {
2222
}
2323

2424
public static ExecutorService getOrCreate(AuditConfig config, String enterpriseId) {
25-
String providerInstanceId = config.getExecutorServiceProvider().toString();
25+
String providerInstanceId = config.getExecutorServiceProvider().getInstanceId();
2626
Integer orgCustomPoolSize = enterpriseId != null ? config.getCustomThreadPoolSizes().get(enterpriseId) : null;
2727
if (orgCustomPoolSize != null) {
2828
return ENTERPRISE_CUSTOM

slack-api-client/src/main/java/com/slack/api/methods/impl/ThreadPools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static ExecutorService getDefault(MethodsConfig config) {
2121
}
2222

2323
public static ExecutorService getOrCreate(MethodsConfig config, String teamId) {
24-
String providerInstanceId = config.getExecutorServiceProvider().toString();
24+
String providerInstanceId = config.getExecutorServiceProvider().getInstanceId();
2525
Integer teamCustomPoolSize = teamId != null ? config.getCustomThreadPoolSizes().get(teamId) : null;
2626
if (teamCustomPoolSize != null) {
2727
return TEAM_CUSTOM

slack-api-client/src/main/java/com/slack/api/scim/impl/ThreadPools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static ExecutorService getDefault(SCIMConfig config) {
2222
}
2323

2424
public static ExecutorService getOrCreate(SCIMConfig config, String enterpriseId) {
25-
String providerInstanceId = config.getExecutorServiceProvider().toString();
25+
String providerInstanceId = config.getExecutorServiceProvider().getInstanceId();
2626
Integer orgCustomPoolSize = enterpriseId != null ? config.getCustomThreadPoolSizes().get(enterpriseId) : null;
2727
if (orgCustomPoolSize != null) {
2828
return ENTERPRISE_CUSTOM

slack-api-model/src/main/java/com/slack/api/util/thread/ExecutorServiceProvider.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,15 @@ public interface ExecutorServiceProvider {
2323
*/
2424
ScheduledExecutorService createThreadScheduledExecutor(String threadGroupName);
2525

26+
/**
27+
* Returns the unique identifier for this instance. The value must be unique among different implementations.
28+
* The default implementation is exactly the same with the default #toString() method.
29+
* The reason why we have this method is to avoid unexpected side effect
30+
* in the case where a developer customizes the #toString() method behavior.
31+
* @return an instance ID
32+
*/
33+
default String getInstanceId() {
34+
return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
35+
}
36+
2637
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test_locally.util;
2+
3+
import com.slack.api.util.thread.ExecutorServiceProvider;
4+
import org.junit.Test;
5+
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.ScheduledExecutorService;
8+
9+
import static org.junit.Assert.assertTrue;
10+
11+
public class ExecutorServiceProviderTest {
12+
13+
@Test
14+
public void getInstanceId() {
15+
ExecutorServiceProvider provider = new ExecutorServiceProvider() {
16+
@Override
17+
public ExecutorService createThreadPoolExecutor(String threadGroupName, int poolSize) {
18+
return null;
19+
}
20+
21+
@Override
22+
public ScheduledExecutorService createThreadScheduledExecutor(String threadGroupName) {
23+
return null;
24+
}
25+
};
26+
assertTrue(provider.getInstanceId().matches("test_locally.util.ExecutorServiceProviderTest\\$1\\@\\w+$"));
27+
}
28+
}

0 commit comments

Comments
 (0)