diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 04f43093b1b9..709deda57a47 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -139,6 +139,7 @@ ALL_TESTS = [ "//pkg/kv/kvserver/closedts/sidetransport:sidetransport_test", "//pkg/kv/kvserver/closedts/tracker:tracker_test", "//pkg/kv/kvserver/closedts:closedts_test", + "//pkg/kv/kvserver/concurrency/poison:poison_test", "//pkg/kv/kvserver/concurrency:concurrency_test", "//pkg/kv/kvserver/gc:gc_test", "//pkg/kv/kvserver/idalloc:idalloc_test", diff --git a/pkg/kv/kvserver/concurrency/poison/BUILD.bazel b/pkg/kv/kvserver/concurrency/poison/BUILD.bazel index fa267c099677..cf282b9e15d4 100644 --- a/pkg/kv/kvserver/concurrency/poison/BUILD.bazel +++ b/pkg/kv/kvserver/concurrency/poison/BUILD.bazel @@ -1,5 +1,5 @@ load("@rules_proto//proto:defs.bzl", "proto_library") -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") proto_library( @@ -42,3 +42,20 @@ go_library( "@com_github_cockroachdb_errors//:errors", ], ) + +go_test( + name = "poison_test", + srcs = ["error_test.go"], + data = glob(["testdata/**"]), + deps = [ + ":poison", + "//pkg/keys", + "//pkg/roachpb", + "//pkg/testutils/echotest", + "//pkg/util/hlc", + "//pkg/util/leaktest", + "@com_github_cockroachdb_errors//:errors", + "@com_github_cockroachdb_redact//:redact", + "@com_github_stretchr_testify//require", + ], +) diff --git a/pkg/kv/kvserver/concurrency/poison/error.go b/pkg/kv/kvserver/concurrency/poison/error.go index ec9cb6900a5f..3c473fc40a50 100644 --- a/pkg/kv/kvserver/concurrency/poison/error.go +++ b/pkg/kv/kvserver/concurrency/poison/error.go @@ -40,6 +40,3 @@ func (e *PoisonedError) Format(s fmt.State, verb rune) { errors.FormatError(e, s func (e *PoisonedError) Error() string { return fmt.Sprint(e) } - -// TODO(tbg): need similar init() function that ReplicaUnavailableError has, -// or PoisonError will not survive network round-trips. diff --git a/pkg/kv/kvserver/concurrency/poison/error_test.go b/pkg/kv/kvserver/concurrency/poison/error_test.go new file mode 100644 index 000000000000..360501d4e68e --- /dev/null +++ b/pkg/kv/kvserver/concurrency/poison/error_test.go @@ -0,0 +1,39 @@ +// 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 poison_test + +import ( + "context" + "path/filepath" + "testing" + + _ "github.com/cockroachdb/cockroach/pkg/keys" // to init roachpb.PrettyPrintRange + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/poison" + "github.com/cockroachdb/cockroach/pkg/roachpb" + "github.com/cockroachdb/cockroach/pkg/testutils/echotest" + "github.com/cockroachdb/cockroach/pkg/util/hlc" + "github.com/cockroachdb/cockroach/pkg/util/leaktest" + "github.com/cockroachdb/errors" + "github.com/cockroachdb/redact" + "github.com/stretchr/testify/require" +) + +func TestPoisonedError(t *testing.T) { + defer leaktest.AfterTest(t)() + ctx := context.Background() + err := errors.DecodeError(ctx, errors.EncodeError(ctx, poison.NewPoisonedError( + roachpb.Span{Key: roachpb.Key("a")}, hlc.Timestamp{WallTime: 1}, + ))) + require.True(t, errors.HasType(err, (*poison.PoisonedError)(nil)), "%+v", err) + var buf redact.StringBuilder + buf.Printf("%s", err) + echotest.Require(t, string(buf.RedactableString()), filepath.Join("testdata", "poisoned_error.txt")) +} diff --git a/pkg/kv/kvserver/concurrency/poison/testdata/poisoned_error.txt b/pkg/kv/kvserver/concurrency/poison/testdata/poisoned_error.txt new file mode 100644 index 000000000000..b4b6bf406111 --- /dev/null +++ b/pkg/kv/kvserver/concurrency/poison/testdata/poisoned_error.txt @@ -0,0 +1,3 @@ +echo +---- +encountered poisoned latch ‹a›@0.000000001,0