github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/roachpb/errors_test.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package roachpb 12 13 import ( 14 "strings" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/util/hlc" 18 ) 19 20 type testError struct{} 21 22 func (t *testError) Error() string { return "test" } 23 func (t *testError) message(pErr *Error) string { return "test" } 24 25 // TestNewError verifies that a test error that 26 // implements retryable or indexed is converted properly into a generic error. 27 func TestNewError(t *testing.T) { 28 pErr := NewError(&testError{}) 29 if pErr.GoError().Error() != "test" { 30 t.Errorf("unexpected error: %s", pErr) 31 } 32 } 33 34 // TestNewErrorNil verifies that a nil error can be set 35 // and retrieved from a response header. 36 func TestNewErrorNil(t *testing.T) { 37 pErr := NewError(nil) 38 if pErr != nil { 39 t.Errorf("expected nil error; got %s", pErr) 40 } 41 } 42 43 // TestSetTxn vefifies that SetTxn updates the error message. 44 func TestSetTxn(t *testing.T) { 45 e := NewError(NewTransactionAbortedError(ABORT_REASON_ABORTED_RECORD_FOUND)) 46 txn := MakeTransaction("test", Key("a"), 1, hlc.Timestamp{}, 0) 47 e.SetTxn(&txn) 48 if !strings.HasPrefix( 49 e.Message, "TransactionAbortedError(ABORT_REASON_ABORTED_RECORD_FOUND): \"test\"") { 50 t.Errorf("unexpected message: %s", e.Message) 51 } 52 } 53 54 func TestErrorTxn(t *testing.T) { 55 var pErr *Error 56 if txn := pErr.GetTxn(); txn != nil { 57 t.Fatalf("wanted nil, unexpected: %+v", txn) 58 } 59 pErr = &Error{} 60 const name = "X" 61 pErr.SetTxn(&Transaction{Name: name}) 62 if txn := pErr.GetTxn(); txn == nil || txn.Name != name { 63 t.Fatalf("wanted name %s, unexpected: %+v", name, txn) 64 } 65 } 66 67 func TestReadWithinUncertaintyIntervalError(t *testing.T) { 68 { 69 rwueNew := NewReadWithinUncertaintyIntervalError( 70 hlc.Timestamp{WallTime: 1}, hlc.Timestamp{WallTime: 2}, 71 &Transaction{ 72 MaxTimestamp: hlc.Timestamp{WallTime: 3}, 73 ObservedTimestamps: []ObservedTimestamp{{NodeID: 12, Timestamp: hlc.Timestamp{WallTime: 4}}}, 74 }) 75 expNew := "ReadWithinUncertaintyIntervalError: read at time 0.000000001,0 encountered " + 76 "previous write with future timestamp 0.000000002,0 within uncertainty interval " + 77 "`t <= 0.000000003,0`; observed timestamps: [{12 0.000000004,0}]" 78 if a := rwueNew.Error(); a != expNew { 79 t.Fatalf("expected: %s\ngot: %s", a, expNew) 80 } 81 } 82 83 { 84 rwueOld := NewReadWithinUncertaintyIntervalError( 85 hlc.Timestamp{WallTime: 1}, hlc.Timestamp{WallTime: 2}, nil) 86 87 expOld := "ReadWithinUncertaintyIntervalError: read at time 0.000000001,0 encountered " + 88 "previous write with future timestamp 0.000000002,0 within uncertainty interval " + 89 "`t <= <nil>`; observed timestamps: []" 90 if a := rwueOld.Error(); a != expOld { 91 t.Fatalf("expected: %s\ngot: %s", a, expOld) 92 } 93 } 94 }