|
17 | 17 | package com.google.cloud.spanner;
|
18 | 18 |
|
19 | 19 | import static org.junit.Assert.assertEquals;
|
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | +import static org.junit.Assume.assumeFalse; |
| 23 | +import static org.junit.Assume.assumeTrue; |
20 | 24 | import static org.mockito.ArgumentMatchers.any;
|
21 | 25 | import static org.mockito.Mockito.doAnswer;
|
22 | 26 | import static org.mockito.Mockito.mock;
|
23 | 27 | import static org.mockito.Mockito.when;
|
24 | 28 |
|
25 | 29 | import com.google.cloud.spanner.SessionClient.SessionConsumer;
|
| 30 | +import java.lang.reflect.Field; |
26 | 31 | import java.time.Clock;
|
27 | 32 | import java.time.Duration;
|
28 | 33 | import java.time.Instant;
|
29 |
| -import org.junit.Ignore; |
| 34 | +import java.util.Map; |
30 | 35 | import org.junit.Test;
|
31 | 36 | import org.junit.runner.RunWith;
|
32 | 37 | import org.junit.runners.JUnit4;
|
|
35 | 40 | @RunWith(JUnit4.class)
|
36 | 41 | public class MultiplexedSessionDatabaseClientTest {
|
37 | 42 |
|
38 |
| - @Ignore("Fails on native builds due to ues of reflection") |
39 | 43 | @Test
|
40 | 44 | public void testMaintainer() {
|
| 45 | + // This fails for the native builds due to the extensive use of reflection. |
| 46 | + assumeTrue(isJava8()); |
| 47 | + |
41 | 48 | Instant now = Instant.now();
|
42 | 49 | Clock clock = mock(Clock.class);
|
43 | 50 | when(clock.instant()).thenReturn(now);
|
@@ -101,4 +108,42 @@ public void testMaintainer() {
|
101 | 108 | client.getMaintainer().maintain();
|
102 | 109 | assertEquals(client.getCurrentSessionReference(), session2.getSessionReference());
|
103 | 110 | }
|
| 111 | + |
| 112 | + @Test |
| 113 | + public void testForceDisableEnvVar() throws Exception { |
| 114 | + assumeTrue(isJava8()); |
| 115 | + assumeFalse( |
| 116 | + System.getenv().containsKey("GOOGLE_CLOUD_SPANNER_FORCE_DISABLE_MULTIPLEXED_SESSIONS")); |
| 117 | + |
| 118 | + // Assert that the mux sessions setting is respected by default. |
| 119 | + assertTrue( |
| 120 | + SessionPoolOptions.newBuilder() |
| 121 | + .setUseMultiplexedSession(true) |
| 122 | + .build() |
| 123 | + .getUseMultiplexedSession()); |
| 124 | + |
| 125 | + Class<?> classOfMap = System.getenv().getClass(); |
| 126 | + Field field = classOfMap.getDeclaredField("m"); |
| 127 | + field.setAccessible(true); |
| 128 | + Map<String, String> writeableEnvironmentVariables = |
| 129 | + (Map<String, String>) field.get(System.getenv()); |
| 130 | + |
| 131 | + try { |
| 132 | + writeableEnvironmentVariables.put( |
| 133 | + "GOOGLE_CLOUD_SPANNER_FORCE_DISABLE_MULTIPLEXED_SESSIONS", "true"); |
| 134 | + // Assert that the env var overrides the mux sessions setting. |
| 135 | + assertFalse( |
| 136 | + SessionPoolOptions.newBuilder() |
| 137 | + .setUseMultiplexedSession(true) |
| 138 | + .build() |
| 139 | + .getUseMultiplexedSession()); |
| 140 | + } finally { |
| 141 | + writeableEnvironmentVariables.remove( |
| 142 | + "GOOGLE_CLOUD_SPANNER_FORCE_DISABLE_MULTIPLEXED_SESSIONS"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private boolean isJava8() { |
| 147 | + return JavaVersionUtil.getJavaMajorVersion() == 8; |
| 148 | + } |
104 | 149 | }
|
0 commit comments