/** * Create a session pool with the given options and for the given database. It will also start * eagerly creating sessions if {@link SessionPoolOptions#getMinSessions()} is greater than 0. * Return pool is immediately ready for use, though getting a session might block for sessions to * be created. */ static SessionPool createPool(SpannerOptions spannerOptions, DatabaseId db, SpannerImpl spanner) { return createPool( spannerOptions.getSessionPoolOptions(), ((GrpcTransportOptions) spannerOptions.getTransportOptions()).getExecutorFactory(), db, spanner); }
@Test public void getDbclientAgainGivesSame() { Map<String, String> labels = new HashMap<>(); labels.put("env", "dev"); Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels); String dbName = "projects/p1/instances/i1/databases/d1"; DatabaseId db = DatabaseId.of(dbName); Mockito.when(spannerOptions.getTransportOptions()) .thenReturn(GrpcTransportOptions.newBuilder().build()); Mockito.when(spannerOptions.getSessionPoolOptions()) .thenReturn(SessionPoolOptions.newBuilder().build()); DatabaseClient databaseClient = impl.getDatabaseClient(db); // Get db client again DatabaseClient databaseClient1 = impl.getDatabaseClient(db); assertThat(databaseClient1).isSameAs(databaseClient); }
@Test public void getDbclientAfterCloseThrows() { SpannerImpl imp = new SpannerImpl(rpc, 1, spannerOptions); Map<String, String> labels = new HashMap<>(); labels.put("env", "dev"); Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels); String dbName = "projects/p1/instances/i1/databases/d1"; DatabaseId db = DatabaseId.of(dbName); Mockito.when(spannerOptions.getTransportOptions()) .thenReturn(GrpcTransportOptions.newBuilder().build()); Mockito.when(spannerOptions.getSessionPoolOptions()) .thenReturn(SessionPoolOptions.newBuilder().build()); imp.close(); try { imp.getDatabaseClient(db); fail("Expected exception"); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("Cloud Spanner client has been closed"); } }
/** * Create a session pool with the given options and for the given database. It will also start * eagerly creating sessions if {@link SessionPoolOptions#getMinSessions()} is greater than 0. * Return pool is immediately ready for use, though getting a session might block for sessions to * be created. */ static SessionPool createPool(SpannerOptions spannerOptions, DatabaseId db, SpannerImpl spanner) { return createPool( spannerOptions.getSessionPoolOptions(), ((GrpcTransportOptions) spannerOptions.getTransportOptions()).getExecutorFactory(), db, spanner); }