1313// limitations under the License.
1414
1515#include " google/cloud/spanner/internal/session_pool.h"
16+ #include " google/cloud/spanner/internal/clock.h"
1617#include " google/cloud/spanner/internal/session.h"
18+ #include " google/cloud/spanner/testing/fake_clock.h"
1719#include " google/cloud/spanner/testing/mock_spanner_stub.h"
1820#include " google/cloud/internal/background_threads_impl.h"
1921#include " google/cloud/internal/make_unique.h"
@@ -35,6 +37,7 @@ inline namespace SPANNER_CLIENT_NS {
3537namespace internal {
3638namespace {
3739
40+ using ::google::cloud::spanner_testing::FakeSteadyClock;
3841using ::google::cloud::testing_util::MockAsyncResponseReader;
3942using ::google::cloud::testing_util::MockCompletionQueue;
4043using ::testing::_;
@@ -71,13 +74,15 @@ spanner_proto::BatchCreateSessionsResponse MakeSessionsResponse(
7174
7275std::shared_ptr<SessionPool> MakeSessionPool (
7376 Database db, std::vector<std::shared_ptr<SpannerStub>> stubs,
74- SessionPoolOptions options, CompletionQueue cq) {
77+ SessionPoolOptions options, CompletionQueue cq,
78+ std::shared_ptr<SteadyClock> clock = std::make_shared<SteadyClock>()) {
7579 return MakeSessionPool (
7680 std::move (db), std::move (stubs), std::move (options), std::move (cq),
7781 google::cloud::internal::make_unique<LimitedTimeRetryPolicy>(
7882 std::chrono::minutes (10 )),
7983 google::cloud::internal::make_unique<ExponentialBackoffPolicy>(
80- std::chrono::milliseconds (100 ), std::chrono::minutes (1 ), 2.0 ));
84+ std::chrono::milliseconds (100 ), std::chrono::minutes (1 ), 2.0 ),
85+ std::move (clock));
8186}
8287
8388TEST (SessionPool, Allocate) {
@@ -388,9 +393,6 @@ TEST(SessionPool, GetStubForStublessSession) {
388393 EXPECT_EQ (pool->GetStub (*session), mock);
389394}
390395
391- // TODO(#1428): This test has a real-time component (see sleep_for() below)
392- // as SessionPool does not currently provide a mechanism to inject a clock
393- // source to control whether sessions require refreshing.
394396TEST (SessionPool, SessionRefresh) {
395397 auto mock = std::make_shared<StrictMock<spanner_testing::MockSpannerStub>>();
396398 EXPECT_CALL (*mock, BatchCreateSessions (_, _))
@@ -421,7 +423,9 @@ TEST(SessionPool, SessionRefresh) {
421423 SessionPoolOptions options;
422424 options.set_keep_alive_interval (std::chrono::seconds (1 ));
423425 auto impl = std::make_shared<MockCompletionQueue>();
424- auto pool = MakeSessionPool (db, {mock}, options, CompletionQueue (impl));
426+ auto clock = std::make_shared<FakeSteadyClock>();
427+ auto pool =
428+ MakeSessionPool (db, {mock}, options, CompletionQueue (impl), clock);
425429
426430 // Allocate and release two session, "s1" and "s2". This will satisfy the
427431 // BatchCreateSessions() expectations.
@@ -435,7 +439,7 @@ TEST(SessionPool, SessionRefresh) {
435439 EXPECT_EQ (" s2" , (*s2)->session_name ());
436440 }
437441 // Wait for "s2" to need refreshing before releasing "s1".
438- std::this_thread::sleep_for (options.keep_alive_interval () * 2 );
442+ clock-> AdvanceTime (options.keep_alive_interval () * 2 );
439443 }
440444
441445 // Simulate completion of pending operations, which will result in
0 commit comments