Skip to content

Commit

Permalink
sqllivenesstestutils: add FakeSession
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
ajwerner committed Mar 28, 2023
1 parent 60b7440 commit f8dc0db
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 14 additions & 12 deletions pkg/sql/conn_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, `
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/sqlliveness/sqllivenesstestutils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
34 changes: 34 additions & 0 deletions pkg/sql/sqlliveness/sqllivenesstestutils/fake_session.go
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit f8dc0db

Please sign in to comment.