|
63 | 63 | import java.util.HashMap;
|
64 | 64 | import java.util.List;
|
65 | 65 | import java.util.Map;
|
| 66 | +import java.util.concurrent.Callable; |
66 | 67 | import java.util.concurrent.ScheduledExecutorService;
|
67 | 68 | import javax.annotation.Nonnull;
|
68 | 69 | import org.junit.Test;
|
@@ -913,6 +914,62 @@ public void testCustomAsyncExecutorProvider() {
|
913 | 914 | assertSame(service, options.getAsyncExecutorProvider().getExecutor());
|
914 | 915 | }
|
915 | 916 |
|
| 917 | + @Test |
| 918 | + public void testAsyncExecutorProviderCoreThreadCount() throws Exception { |
| 919 | + assertEquals(8, SpannerOptions.getDefaultAsyncExecutorProviderCoreThreadCount()); |
| 920 | + String propertyName = "SPANNER_ASYNC_NUM_CORE_THREADS"; |
| 921 | + assertEquals( |
| 922 | + Integer.valueOf(8), |
| 923 | + runWithSystemProperty( |
| 924 | + propertyName, null, SpannerOptions::getDefaultAsyncExecutorProviderCoreThreadCount)); |
| 925 | + assertEquals( |
| 926 | + Integer.valueOf(16), |
| 927 | + runWithSystemProperty( |
| 928 | + propertyName, "16", SpannerOptions::getDefaultAsyncExecutorProviderCoreThreadCount)); |
| 929 | + assertEquals( |
| 930 | + Integer.valueOf(1), |
| 931 | + runWithSystemProperty( |
| 932 | + propertyName, "1", SpannerOptions::getDefaultAsyncExecutorProviderCoreThreadCount)); |
| 933 | + assertThrows( |
| 934 | + SpannerException.class, |
| 935 | + () -> |
| 936 | + runWithSystemProperty( |
| 937 | + propertyName, |
| 938 | + "foo", |
| 939 | + SpannerOptions::getDefaultAsyncExecutorProviderCoreThreadCount)); |
| 940 | + assertThrows( |
| 941 | + SpannerException.class, |
| 942 | + () -> |
| 943 | + runWithSystemProperty( |
| 944 | + propertyName, |
| 945 | + "-1", |
| 946 | + SpannerOptions::getDefaultAsyncExecutorProviderCoreThreadCount)); |
| 947 | + assertThrows( |
| 948 | + SpannerException.class, |
| 949 | + () -> |
| 950 | + runWithSystemProperty( |
| 951 | + propertyName, "", SpannerOptions::getDefaultAsyncExecutorProviderCoreThreadCount)); |
| 952 | + } |
| 953 | + |
| 954 | + static <V> V runWithSystemProperty( |
| 955 | + String propertyName, String propertyValue, Callable<V> callable) throws Exception { |
| 956 | + String currentValue = System.getProperty(propertyName); |
| 957 | + if (propertyValue == null) { |
| 958 | + System.clearProperty(propertyName); |
| 959 | + } else { |
| 960 | + System.setProperty(propertyName, propertyValue); |
| 961 | + } |
| 962 | + try { |
| 963 | + return callable.call(); |
| 964 | + } finally { |
| 965 | + if (currentValue == null) { |
| 966 | + System.clearProperty(propertyName); |
| 967 | + } else { |
| 968 | + System.setProperty(propertyName, currentValue); |
| 969 | + } |
| 970 | + } |
| 971 | + } |
| 972 | + |
916 | 973 | @Test
|
917 | 974 | public void testDefaultNumChannelsWithGrpcGcpExtensionEnabled() {
|
918 | 975 | SpannerOptions options =
|
|
0 commit comments