16
16
17
17
package com .google .cloud .spanner ;
18
18
19
+ import static com .google .common .truth .Truth .assertThat ;
19
20
import static org .junit .Assert .assertEquals ;
20
21
import static org .junit .Assert .assertFalse ;
22
+ import static org .junit .Assert .assertThrows ;
21
23
import static org .junit .Assert .assertTrue ;
22
24
import static org .junit .Assume .assumeFalse ;
23
25
import static org .junit .Assume .assumeTrue ;
27
29
import static org .mockito .Mockito .when ;
28
30
29
31
import com .google .cloud .spanner .SessionClient .SessionConsumer ;
32
+ import java .io .PrintWriter ;
33
+ import java .io .StringWriter ;
30
34
import java .lang .reflect .Field ;
31
35
import java .time .Clock ;
32
36
import java .time .Duration ;
@@ -110,10 +114,9 @@ public void testMaintainer() {
110
114
}
111
115
112
116
@ Test
113
- public void testForceDisableEnvVar () throws Exception {
117
+ public void testDisableMultiplexedSessionEnvVar () throws Exception {
114
118
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" ));
117
120
118
121
// Assert that the mux sessions setting is respected by default.
119
122
assertTrue (
@@ -129,17 +132,116 @@ public void testForceDisableEnvVar() throws Exception {
129
132
(Map <String , String >) field .get (System .getenv ());
130
133
131
134
try {
132
- writeableEnvironmentVariables .put (
133
- "GOOGLE_CLOUD_SPANNER_FORCE_DISABLE_MULTIPLEXED_SESSIONS" , "true" );
135
+ writeableEnvironmentVariables .put ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" , "false" );
134
136
// Assert that the env var overrides the mux sessions setting.
135
137
assertFalse (
136
138
SessionPoolOptions .newBuilder ()
137
139
.setUseMultiplexedSession (true )
138
140
.build ()
139
141
.getUseMultiplexedSession ());
140
142
} 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" );
143
245
}
144
246
}
145
247
0 commit comments