1616
1717package com .google .cloud .spanner ;
1818
19+ import static com .google .common .truth .Truth .assertThat ;
1920import static org .junit .Assert .assertEquals ;
2021import static org .junit .Assert .assertFalse ;
22+ import static org .junit .Assert .assertThrows ;
2123import static org .junit .Assert .assertTrue ;
2224import static org .junit .Assume .assumeFalse ;
2325import static org .junit .Assume .assumeTrue ;
2729import static org .mockito .Mockito .when ;
2830
2931import com .google .cloud .spanner .SessionClient .SessionConsumer ;
32+ import java .io .PrintWriter ;
33+ import java .io .StringWriter ;
3034import java .lang .reflect .Field ;
3135import java .time .Clock ;
3236import java .time .Duration ;
@@ -110,10 +114,9 @@ public void testMaintainer() {
110114 }
111115
112116 @ Test
113- public void testForceDisableEnvVar () throws Exception {
117+ public void testDisableMultiplexedSessionEnvVar () throws Exception {
114118 assumeTrue (isJava8 () && !isWindows ());
115- assumeFalse (
116- System .getenv ().containsKey ("GOOGLE_CLOUD_SPANNER_FORCE_DISABLE_MULTIPLEXED_SESSIONS" ));
119+ assumeFalse (System .getenv ().containsKey ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" ));
117120
118121 // Assert that the mux sessions setting is respected by default.
119122 assertTrue (
@@ -129,17 +132,116 @@ public void testForceDisableEnvVar() throws Exception {
129132 (Map <String , String >) field .get (System .getenv ());
130133
131134 try {
132- writeableEnvironmentVariables .put (
133- "GOOGLE_CLOUD_SPANNER_FORCE_DISABLE_MULTIPLEXED_SESSIONS" , "true" );
135+ writeableEnvironmentVariables .put ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" , "false" );
134136 // Assert that the env var overrides the mux sessions setting.
135137 assertFalse (
136138 SessionPoolOptions .newBuilder ()
137139 .setUseMultiplexedSession (true )
138140 .build ()
139141 .getUseMultiplexedSession ());
140142 } finally {
141- writeableEnvironmentVariables .remove (
142- "GOOGLE_CLOUD_SPANNER_FORCE_DISABLE_MULTIPLEXED_SESSIONS" );
143+ writeableEnvironmentVariables .remove ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" );
144+ }
145+ }
146+
147+ @ Test
148+ public void testEnableMultiplexedSessionEnvVar () throws Exception {
149+ assumeTrue (isJava8 () && !isWindows ());
150+ assumeFalse (System .getenv ().containsKey ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" ));
151+
152+ // Assert that the mux sessions setting is respected by default.
153+ assertFalse (
154+ SessionPoolOptions .newBuilder ()
155+ .setUseMultiplexedSession (false )
156+ .build ()
157+ .getUseMultiplexedSession ());
158+
159+ Class <?> classOfMap = System .getenv ().getClass ();
160+ Field field = classOfMap .getDeclaredField ("m" );
161+ field .setAccessible (true );
162+ Map <String , String > writeableEnvironmentVariables =
163+ (Map <String , String >) field .get (System .getenv ());
164+
165+ try {
166+ writeableEnvironmentVariables .put ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" , "true" );
167+ // Assert that the env var overrides the mux sessions setting.
168+ assertTrue (
169+ SessionPoolOptions .newBuilder ()
170+ .setUseMultiplexedSession (false )
171+ .build ()
172+ .getUseMultiplexedSession ());
173+ } finally {
174+ writeableEnvironmentVariables .remove ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" );
175+ }
176+ }
177+
178+ @ Test
179+ public void testIgnoreMultiplexedSessionEnvVar () throws Exception {
180+ assumeTrue (isJava8 () && !isWindows ());
181+ assumeFalse (System .getenv ().containsKey ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" ));
182+
183+ // Assert that the mux sessions setting is respected by default.
184+ assertFalse (
185+ SessionPoolOptions .newBuilder ()
186+ .setUseMultiplexedSession (false )
187+ .build ()
188+ .getUseMultiplexedSession ());
189+
190+ Class <?> classOfMap = System .getenv ().getClass ();
191+ Field field = classOfMap .getDeclaredField ("m" );
192+ field .setAccessible (true );
193+ Map <String , String > writeableEnvironmentVariables =
194+ (Map <String , String >) field .get (System .getenv ());
195+
196+ try {
197+ writeableEnvironmentVariables .put ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" , "" );
198+ // Assert that the env var overrides the mux sessions setting.
199+ assertFalse (
200+ SessionPoolOptions .newBuilder ()
201+ .setUseMultiplexedSession (false )
202+ .build ()
203+ .getUseMultiplexedSession ());
204+ } finally {
205+ writeableEnvironmentVariables .remove ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" );
206+ }
207+ }
208+
209+ @ Test
210+ public void testThrowExceptionMultiplexedSessionEnvVarInvalidValues () throws Exception {
211+ assumeTrue (isJava8 () && !isWindows ());
212+ assumeFalse (System .getenv ().containsKey ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" ));
213+
214+ // Assert that the mux sessions setting is respected by default.
215+ assertFalse (
216+ SessionPoolOptions .newBuilder ()
217+ .setUseMultiplexedSession (false )
218+ .build ()
219+ .getUseMultiplexedSession ());
220+
221+ Class <?> classOfMap = System .getenv ().getClass ();
222+ Field field = classOfMap .getDeclaredField ("m" );
223+ field .setAccessible (true );
224+ Map <String , String > writeableEnvironmentVariables =
225+ (Map <String , String >) field .get (System .getenv ());
226+
227+ try {
228+ writeableEnvironmentVariables .put ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" , "test" );
229+
230+ // setting an invalid GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS value throws error.
231+ IllegalArgumentException e =
232+ assertThrows (
233+ IllegalArgumentException .class ,
234+ () ->
235+ SessionPoolOptions .newBuilder ()
236+ .setUseMultiplexedSession (false )
237+ .build ()
238+ .getUseMultiplexedSession ());
239+ StringWriter sw = new StringWriter ();
240+ e .printStackTrace (new PrintWriter (sw ));
241+ assertThat (sw .toString ())
242+ .contains ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS should be either true or false" );
243+ } finally {
244+ writeableEnvironmentVariables .remove ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" );
143245 }
144246 }
145247
0 commit comments