/
SpannerPoolTest.java
400 lines (352 loc) · 15.7 KB
/
SpannerPoolTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.jdbc;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.jdbc.ConnectionImpl.LeakedConnectionException;
import com.google.cloud.spanner.jdbc.SpannerPool.CheckAndCloseSpannersMode;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class SpannerPoolTest {
private static final String URI =
"cloudspanner:/projects/test-project-123/instances/test-instance/databases/test-database";
private ConnectionImpl connection1 = mock(ConnectionImpl.class);
private ConnectionImpl connection2 = mock(ConnectionImpl.class);
private ConnectionImpl connection3 = mock(ConnectionImpl.class);
private GoogleCredentials credentials1 = mock(GoogleCredentials.class);
private GoogleCredentials credentials2 = mock(GoogleCredentials.class);
private ConnectionOptions options1 = mock(ConnectionOptions.class);
private ConnectionOptions options2 = mock(ConnectionOptions.class);
private ConnectionOptions options3 = mock(ConnectionOptions.class);
private ConnectionOptions options4 = mock(ConnectionOptions.class);
private SpannerPool createSubjectAndMocks() {
return createSubjectAndMocks(0L);
}
private SpannerPool createSubjectAndMocks(long closeSpannerAfterMillisecondsUnused) {
SpannerPool pool =
new SpannerPool(closeSpannerAfterMillisecondsUnused) {
@Override
Spanner createSpanner(SpannerPoolKey key) {
return mock(Spanner.class);
}
};
when(options1.getCredentials()).thenReturn(credentials1);
when(options1.getProjectId()).thenReturn("test-project-1");
when(options2.getCredentials()).thenReturn(credentials2);
when(options2.getProjectId()).thenReturn("test-project-1");
when(options3.getCredentials()).thenReturn(credentials1);
when(options3.getProjectId()).thenReturn("test-project-2");
when(options4.getCredentials()).thenReturn(credentials2);
when(options4.getProjectId()).thenReturn("test-project-2");
return pool;
}
@AfterClass
public static void closeSpannerPool() {
SpannerPool.closeSpannerPool();
}
@Test
public void testGetSpanner() {
SpannerPool pool = createSubjectAndMocks();
Spanner spanner1;
Spanner spanner2;
// assert equal
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options1, connection2);
assertThat(spanner1, is(equalTo(spanner2)));
spanner1 = pool.getSpanner(options2, connection1);
spanner2 = pool.getSpanner(options2, connection2);
assertThat(spanner1, is(equalTo(spanner2)));
spanner1 = pool.getSpanner(options3, connection1);
spanner2 = pool.getSpanner(options3, connection2);
assertThat(spanner1, is(equalTo(spanner2)));
spanner1 = pool.getSpanner(options4, connection1);
spanner2 = pool.getSpanner(options4, connection2);
assertThat(spanner1, is(equalTo(spanner2)));
// assert not equal
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options2, connection2);
assertThat(spanner1, not(equalTo(spanner2)));
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options3, connection2);
assertThat(spanner1, not(equalTo(spanner2)));
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options4, connection2);
assertThat(spanner1, not(equalTo(spanner2)));
spanner1 = pool.getSpanner(options2, connection1);
spanner2 = pool.getSpanner(options3, connection2);
assertThat(spanner1, not(equalTo(spanner2)));
spanner1 = pool.getSpanner(options2, connection1);
spanner2 = pool.getSpanner(options4, connection2);
assertThat(spanner1, not(equalTo(spanner2)));
spanner1 = pool.getSpanner(options3, connection1);
spanner2 = pool.getSpanner(options4, connection2);
assertThat(spanner1, not(equalTo(spanner2)));
}
@Test
public void testRemoveConnection() {
SpannerPool pool = createSubjectAndMocks();
Spanner spanner1;
Spanner spanner2;
// assert equal
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options1, connection2);
assertThat(spanner1, is(equalTo(spanner2)));
// one connection removed, assert that we would still get the same Spanner
pool.removeConnection(options1, connection1);
spanner1 = pool.getSpanner(options1, connection1);
assertThat(spanner1, is(equalTo(spanner2)));
// remove two connections, assert that we would still get the same Spanner, as Spanners are not
// directly closed and removed.
pool.removeConnection(options1, connection1);
pool.removeConnection(options1, connection2);
spanner1 = pool.getSpanner(options1, connection1);
assertThat(spanner1, is(equalTo(spanner2)));
// remove the last connection again
pool.removeConnection(options1, connection1);
}
private static Logger log = Logger.getLogger(SpannerPool.class.getName());
private static OutputStream logCapturingStream;
private static StreamHandler customLogHandler;
private void attachLogCapturer() {
logCapturingStream = new ByteArrayOutputStream();
Logger currentLogger = log;
Handler[] handlers = new Handler[0];
while (handlers.length == 0 && currentLogger != null) {
handlers = currentLogger.getHandlers();
currentLogger = currentLogger.getParent();
}
if (handlers.length == 0) {
throw new IllegalStateException("no handlers found for logger");
}
customLogHandler = new StreamHandler(logCapturingStream, handlers[0].getFormatter());
log.addHandler(customLogHandler);
}
public String getTestCapturedLog() throws IOException {
customLogHandler.flush();
return logCapturingStream.toString();
}
@Test
public void testRemoveConnectionOptionsNotRegistered() throws IOException {
attachLogCapturer();
final String expectedLogPart = "There is no Spanner registered for ConnectionOptions";
SpannerPool pool = createSubjectAndMocks();
pool.getSpanner(options1, connection1);
pool.removeConnection(options2, connection1);
String capturedLog = getTestCapturedLog();
assertThat(capturedLog.contains(expectedLogPart), is(true));
}
@Test
public void testRemoveConnectionConnectionNotRegistered() throws IOException {
attachLogCapturer();
final String expectedLogPart = "There are no connections registered for ConnectionOptions";
SpannerPool pool = createSubjectAndMocks();
pool.getSpanner(options1, connection1);
pool.removeConnection(options1, connection2);
String capturedLog = getTestCapturedLog();
assertThat(capturedLog.contains(expectedLogPart), is(true));
}
@Test
public void testRemoveConnectionConnectionAlreadyRemoved() throws IOException {
attachLogCapturer();
final String expectedLogPart = "There are no connections registered for ConnectionOptions";
SpannerPool pool = createSubjectAndMocks();
pool.getSpanner(options1, connection1);
pool.removeConnection(options1, connection1);
pool.removeConnection(options1, connection1);
String capturedLog = getTestCapturedLog();
assertThat(capturedLog.contains(expectedLogPart), is(true));
}
@Test
public void testCloseSpanner() throws IOException {
SpannerPool pool = createSubjectAndMocks();
Spanner spanner = pool.getSpanner(options1, connection1);
// verify that closing is not possible until all connections have been removed
boolean exception = false;
try {
pool.checkAndCloseSpanners();
} catch (SpannerException e) {
exception = e.getErrorCode() == ErrorCode.FAILED_PRECONDITION;
}
assertThat(exception, is(true));
// remove the connection and verify that it is possible to close
pool.removeConnection(options1, connection1);
pool.checkAndCloseSpanners();
verify(spanner).close();
final String expectedLogPart =
"WARNING: There is/are 1 connection(s) still open. Close all connections before stopping the application";
Spanner spanner2 = pool.getSpanner(options1, connection1);
pool.checkAndCloseSpanners(CheckAndCloseSpannersMode.WARN);
String capturedLog = getTestCapturedLog();
assertThat(capturedLog.contains(expectedLogPart), is(true));
verify(spanner2, never()).close();
// remove the connection and verify that it is possible to close
pool.removeConnection(options1, connection1);
pool.checkAndCloseSpanners(CheckAndCloseSpannersMode.WARN);
verify(spanner2).close();
}
@Test
public void testLeakedConnection() throws IOException {
ConnectionOptions options =
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI)
.build();
// create an actual connection object but not in a try-with-resources block
Connection connection = options.getConnection();
// try to close the application which should fail
try {
ConnectionOptions.closeSpanner();
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode(), is(equalTo(ErrorCode.FAILED_PRECONDITION)));
}
String capturedLog = getTestCapturedLog();
assertThat(capturedLog.contains(LeakedConnectionException.class.getName()), is(true));
assertThat(capturedLog.contains("testLeakedConnection"), is(true));
// Now close the connection to avoid trouble with other test cases.
connection.close();
}
@Test
public void testCloseUnusedSpanners() {
SpannerPool pool = createSubjectAndMocks();
Spanner spanner1;
Spanner spanner2;
Spanner spanner3;
// create two connections that use the same Spanner
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options1, connection2);
assertThat(spanner1, is(equalTo(spanner2)));
// all spanners are in use, this should have no effect
pool.closeUnusedSpanners(-1L);
verify(spanner1, never()).close();
// close one connection. This should also have no effect.
pool.removeConnection(options1, connection1);
pool.closeUnusedSpanners(-1L);
verify(spanner1, never()).close();
// close the other connection as well, the Spanner object should now be closed.
pool.removeConnection(options1, connection2);
pool.closeUnusedSpanners(-1L);
verify(spanner1).close();
// create three connections that use two different Spanners
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options2, connection2);
spanner3 = pool.getSpanner(options2, connection3);
assertThat(spanner1, not(equalTo(spanner2)));
assertThat(spanner2, is(equalTo(spanner3)));
// all spanners are in use, this should have no effect
pool.closeUnusedSpanners(-1L);
verify(spanner1, never()).close();
verify(spanner2, never()).close();
verify(spanner3, never()).close();
// close connection1. That should also mark spanner1 as no longer in use
pool.removeConnection(options1, connection1);
pool.closeUnusedSpanners(-1L);
verify(spanner1).close();
verify(spanner2, never()).close();
verify(spanner3, never()).close();
// close connection2. That should have no effect, as connection3 is still using spanner2
pool.removeConnection(options2, connection2);
pool.closeUnusedSpanners(-1L);
verify(spanner1).close();
verify(spanner2, never()).close();
verify(spanner3, never()).close();
// close connection3. Now all should be closed.
pool.removeConnection(options2, connection3);
pool.closeUnusedSpanners(-1L);
verify(spanner1).close();
verify(spanner2).close();
verify(spanner3).close();
}
/** Allow the automatic close test to be run multiple times to ensure it is stable */
private static final int NUMBER_OF_AUTOMATIC_CLOSE_TEST_RUNS = 1;
private static final long TEST_AUTOMATIC_CLOSE_TIMEOUT = 2L;
private static final long SLEEP_BEFORE_VERIFICATION = 100L;
@Test
public void testAutomaticCloser() throws InterruptedException {
for (int testRun = 0; testRun < NUMBER_OF_AUTOMATIC_CLOSE_TEST_RUNS; testRun++) {
// create a pool that will close unused spanners after 5 milliseconds
SpannerPool pool = createSubjectAndMocks(TEST_AUTOMATIC_CLOSE_TIMEOUT);
Spanner spanner1;
Spanner spanner2;
Spanner spanner3;
// create two connections that use the same Spanner
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options1, connection2);
assertThat(spanner1, is(equalTo(spanner2)));
// all spanners are in use, this should have no effect
Thread.sleep(SLEEP_BEFORE_VERIFICATION);
verify(spanner1, never()).close();
// close one connection. This should also have no effect.
pool.removeConnection(options1, connection1);
Thread.sleep(SLEEP_BEFORE_VERIFICATION);
verify(spanner1, never()).close();
// close the other connection as well, the Spanner object should now be closed.
pool.removeConnection(options1, connection2);
Thread.sleep(SLEEP_BEFORE_VERIFICATION);
verify(spanner1).close();
// create three connections that use two different Spanners
spanner1 = pool.getSpanner(options1, connection1);
spanner2 = pool.getSpanner(options2, connection2);
spanner3 = pool.getSpanner(options2, connection3);
assertThat(spanner1, not(equalTo(spanner2)));
assertThat(spanner2, is(equalTo(spanner3)));
// all spanners are in use, this should have no effect
Thread.sleep(SLEEP_BEFORE_VERIFICATION);
verify(spanner1, never()).close();
verify(spanner2, never()).close();
verify(spanner3, never()).close();
// close connection1. That should also mark spanner1 as no longer in use
pool.removeConnection(options1, connection1);
Thread.sleep(SLEEP_BEFORE_VERIFICATION);
verify(spanner1).close();
verify(spanner2, never()).close();
verify(spanner3, never()).close();
// close connection2. That should have no effect, as connection3 is still using spanner2
pool.removeConnection(options2, connection2);
Thread.sleep(SLEEP_BEFORE_VERIFICATION);
verify(spanner1).close();
verify(spanner2, never()).close();
verify(spanner3, never()).close();
// close connection3. Now all should be closed.
pool.removeConnection(options2, connection3);
Thread.sleep(SLEEP_BEFORE_VERIFICATION);
verify(spanner1).close();
verify(spanner2).close();
verify(spanner3).close();
}
}
}