|
17 | 17 | package com.google.cloud.spanner;
|
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat;
|
| 20 | +import static org.junit.Assert.fail; |
20 | 21 |
|
| 22 | +import com.google.cloud.grpc.GrpcTransportOptions; |
21 | 23 | import com.google.cloud.spanner.spi.v1.SpannerRpc;
|
22 |
| - |
23 | 24 | import java.util.HashMap;
|
24 | 25 | import java.util.Map;
|
25 | 26 | import org.junit.Before;
|
@@ -70,4 +71,49 @@ public void createAndCloseSession() {
|
70 | 71 | // The same channelHint is passed for deleteSession (contained in "options").
|
71 | 72 | Mockito.verify(rpc).deleteSession(sessionName, options.getValue());
|
72 | 73 | }
|
| 74 | + |
| 75 | + @Test |
| 76 | + public void getDbclientAgainGivesSame() { |
| 77 | + Map<String, String> labels = new HashMap<>(); |
| 78 | + labels.put("env", "dev"); |
| 79 | + Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels); |
| 80 | + String dbName = "projects/p1/instances/i1/databases/d1"; |
| 81 | + DatabaseId db = DatabaseId.of(dbName); |
| 82 | + |
| 83 | + Mockito.when(spannerOptions.getTransportOptions()) |
| 84 | + .thenReturn(GrpcTransportOptions.newBuilder().build()); |
| 85 | + Mockito.when(spannerOptions.getSessionPoolOptions()) |
| 86 | + .thenReturn(SessionPoolOptions.newBuilder().build()); |
| 87 | + |
| 88 | + DatabaseClient databaseClient = impl.getDatabaseClient(db); |
| 89 | + |
| 90 | + // Get db client again |
| 91 | + DatabaseClient databaseClient1 = impl.getDatabaseClient(db); |
| 92 | + |
| 93 | + assertThat(databaseClient1).isSameAs(databaseClient); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void getDbclientAfterCloseThrows() { |
| 98 | + SpannerImpl imp = new SpannerImpl(rpc, 1, spannerOptions); |
| 99 | + Map<String, String> labels = new HashMap<>(); |
| 100 | + labels.put("env", "dev"); |
| 101 | + Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels); |
| 102 | + String dbName = "projects/p1/instances/i1/databases/d1"; |
| 103 | + DatabaseId db = DatabaseId.of(dbName); |
| 104 | + |
| 105 | + Mockito.when(spannerOptions.getTransportOptions()) |
| 106 | + .thenReturn(GrpcTransportOptions.newBuilder().build()); |
| 107 | + Mockito.when(spannerOptions.getSessionPoolOptions()) |
| 108 | + .thenReturn(SessionPoolOptions.newBuilder().build()); |
| 109 | + |
| 110 | + imp.close(); |
| 111 | + |
| 112 | + try { |
| 113 | + imp.getDatabaseClient(db); |
| 114 | + fail("Expected exception"); |
| 115 | + } catch (IllegalStateException e) { |
| 116 | + assertThat(e.getMessage()).contains("Cloud Spanner client has been closed"); |
| 117 | + } |
| 118 | + } |
73 | 119 | }
|
0 commit comments