From f8dc0db45364be087236d4b512448d71a49eca2e Mon Sep 17 00:00:00 2001 From: ajwerner Date: Tue, 28 Mar 2023 09:49:23 -0400 Subject: [PATCH] sqllivenesstestutils: add FakeSession Release note: None --- pkg/sql/BUILD.bazel | 1 + pkg/sql/conn_executor_test.go | 26 +++++++------- .../sqllivenesstestutils/BUILD.bazel | 5 ++- .../sqllivenesstestutils/fake_session.go | 34 +++++++++++++++++++ 4 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 pkg/sql/sqlliveness/sqllivenesstestutils/fake_session.go diff --git a/pkg/sql/BUILD.bazel b/pkg/sql/BUILD.bazel index dfafaedccefd..ed72f6450f65 100644 --- a/pkg/sql/BUILD.bazel +++ b/pkg/sql/BUILD.bazel @@ -806,6 +806,7 @@ go_test( "//pkg/sql/sessionphase", "//pkg/sql/sqlinstance", "//pkg/sql/sqlliveness", + "//pkg/sql/sqlliveness/sqllivenesstestutils", "//pkg/sql/sqlstats", "//pkg/sql/sqltestutils", "//pkg/sql/stats", diff --git a/pkg/sql/conn_executor_test.go b/pkg/sql/conn_executor_test.go index 9464b26341ab..7b8a1404307b 100644 --- a/pkg/sql/conn_executor_test.go +++ b/pkg/sql/conn_executor_test.go @@ -42,6 +42,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" + "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/sqllivenesstestutils" "github.com/cockroachdb/cockroach/pkg/sql/tests" "github.com/cockroachdb/cockroach/pkg/testutils" "github.com/cockroachdb/cockroach/pkg/testutils/pgtest" @@ -1211,12 +1212,15 @@ CREATE DATABASE t1; CREATE TABLE t1.test (k INT PRIMARY KEY, v TEXT); `) + type fakeSession = sqllivenesstestutils.FakeSession t.Run("session_expiry_overrides_lease_deadline", func(t *testing.T) { // Deliberately set the sessionDuration to be less than the lease duration // to confirm that the sessionDuration overrides the lease duration while // setting the transaction deadline. sessionDuration := base.DefaultDescriptorLeaseDuration - time.Minute - fs := fakeSession{exp: s.Clock().Now().Add(sessionDuration.Nanoseconds(), 0)} + fs := fakeSession{ + ExpTS: s.Clock().Now().Add(sessionDuration.Nanoseconds(), 0), + } defer setClientSessionOverride(&fs)() txn, err := sqlConn.Begin() @@ -1239,7 +1243,9 @@ CREATE TABLE t1.test (k INT PRIMARY KEY, v TEXT); // to confirm that the lease duration overrides the session duration while // setting the transaction deadline sessionDuration := base.DefaultDescriptorLeaseDuration + time.Minute - fs := fakeSession{exp: s.Clock().Now().Add(sessionDuration.Nanoseconds(), 0)} + fs := fakeSession{ + ExpTS: s.Clock().Now().Add(sessionDuration.Nanoseconds(), 0), + } defer setClientSessionOverride(&fs)() txn, err := sqlConn.Begin() @@ -1263,7 +1269,9 @@ CREATE TABLE t1.test (k INT PRIMARY KEY, v TEXT); // and observe that we get a clear error indicating that the session // was expired. sessionDuration := -time.Nanosecond - fs := fakeSession{exp: s.Clock().Now().Add(sessionDuration.Nanoseconds(), 0)} + fs := fakeSession{ + ExpTS: s.Clock().Now().Add(sessionDuration.Nanoseconds(), 0), + } defer setClientSessionOverride(&fs)() txn, err := sqlConn.Begin() if err != nil { @@ -1293,7 +1301,9 @@ CREATE TABLE t1.test (k INT PRIMARY KEY, v TEXT); } // Inject an already expired session to observe that it has no effect. - fs := &fakeSession{exp: s.Clock().Now().Add(-time.Minute.Nanoseconds(), 0)} + fs := &fakeSession{ + ExpTS: s.Clock().Now().Add(-time.Minute.Nanoseconds(), 0), + } defer setClientSessionOverride(fs)() txn, err := dbConn.Begin() if err != nil { @@ -1855,14 +1865,6 @@ func noopRequestFilter(ctx context.Context, request *kvpb.BatchRequest) *kvpb.Er return nil } -type fakeSession struct{ exp hlc.Timestamp } - -func (f fakeSession) ID() sqlliveness.SessionID { return "foo" } -func (f fakeSession) Expiration() hlc.Timestamp { return f.exp } -func (f fakeSession) Start() hlc.Timestamp { panic("unimplemented") } - -var _ sqlliveness.Session = (*fakeSession)(nil) - func getTxnID(t *testing.T, tx *gosql.Tx) (id string) { t.Helper() sqlutils.MakeSQLRunner(tx).QueryRow(t, ` diff --git a/pkg/sql/sqlliveness/sqllivenesstestutils/BUILD.bazel b/pkg/sql/sqlliveness/sqllivenesstestutils/BUILD.bazel index d60d183bca40..e30a5fb165e0 100644 --- a/pkg/sql/sqlliveness/sqllivenesstestutils/BUILD.bazel +++ b/pkg/sql/sqlliveness/sqllivenesstestutils/BUILD.bazel @@ -3,7 +3,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "sqllivenesstestutils", - srcs = ["alwaysalivesession.go"], + srcs = [ + "alwaysalivesession.go", + "fake_session.go", + ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/sqllivenesstestutils", visibility = ["//visibility:public"], deps = [ diff --git a/pkg/sql/sqlliveness/sqllivenesstestutils/fake_session.go b/pkg/sql/sqlliveness/sqllivenesstestutils/fake_session.go new file mode 100644 index 000000000000..e084f07e2058 --- /dev/null +++ b/pkg/sql/sqlliveness/sqllivenesstestutils/fake_session.go @@ -0,0 +1,34 @@ +// Copyright 2022 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package sqllivenesstestutils + +import ( + "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" + "github.com/cockroachdb/cockroach/pkg/util/hlc" +) + +// FakeSession is an implementation of sqlliveness.Session for testing. +type FakeSession struct { + SessionID sqlliveness.SessionID + ExpTS hlc.Timestamp + StartTS hlc.Timestamp +} + +var _ sqlliveness.Session = (*FakeSession)(nil) + +// ID returns f.SessionID. +func (f *FakeSession) ID() sqlliveness.SessionID { return f.SessionID } + +// Expiration returns f.ExpTS. +func (f *FakeSession) Expiration() hlc.Timestamp { return f.ExpTS } + +// Start return f.StartTS. +func (f *FakeSession) Start() hlc.Timestamp { return f.StartTS }