github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/abortspan/abortspan_test.go (about)

     1  // Copyright 2014 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 abortspan
    12  
    13  import (
    14  	"context"
    15  	"reflect"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    19  	"github.com/cockroachdb/cockroach/pkg/storage"
    20  	"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
    21  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    22  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    23  	"github.com/cockroachdb/cockroach/pkg/util/stop"
    24  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    25  )
    26  
    27  func uuidFromString(input string) uuid.UUID {
    28  	u, err := uuid.FromString(input)
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	return u
    33  }
    34  
    35  var (
    36  	testTxnID        = uuidFromString("0ce61c17-5eb4-4587-8c36-dcf4062ada4c")
    37  	testTxnKey       = []byte("a")
    38  	testTxnTimestamp = hlc.Timestamp{WallTime: 123, Logical: 456}
    39  	testTxnPriority  = enginepb.TxnPriority(123)
    40  )
    41  
    42  // createTestAbortSpan creates an in-memory engine and
    43  // returns a AbortSpan using the supplied Range ID.
    44  func createTestAbortSpan(
    45  	t *testing.T, rangeID roachpb.RangeID, stopper *stop.Stopper,
    46  ) (*AbortSpan, storage.Engine) {
    47  	eng := storage.NewDefaultInMem()
    48  	stopper.AddCloser(eng)
    49  	return New(rangeID), eng
    50  }
    51  
    52  // TestAbortSpanPutGetClearData tests basic get & put functionality as well as
    53  // clearing the cache.
    54  func TestAbortSpanPutGetClearData(t *testing.T) {
    55  	defer leaktest.AfterTest(t)()
    56  	stopper := stop.NewStopper()
    57  	defer stopper.Stop(context.Background())
    58  	sc, e := createTestAbortSpan(t, 1, stopper)
    59  	// Start with a get for an uncached id.
    60  	entry := roachpb.AbortSpanEntry{}
    61  	if aborted, readErr := sc.Get(context.Background(), e, testTxnID, &entry); aborted {
    62  		t.Errorf("expected not aborted for id %s", testTxnID)
    63  	} else if readErr != nil {
    64  		t.Fatalf("unexpected read error: %s", readErr)
    65  	}
    66  
    67  	entry = roachpb.AbortSpanEntry{
    68  		Key:       testTxnKey,
    69  		Timestamp: testTxnTimestamp,
    70  		Priority:  testTxnPriority,
    71  	}
    72  	if err := sc.Put(context.Background(), e, nil, testTxnID, &entry); err != nil {
    73  		t.Errorf("unexpected error putting response: %+v", err)
    74  	}
    75  
    76  	tryHit := func(expAbort bool, expEntry roachpb.AbortSpanEntry) {
    77  		var actual roachpb.AbortSpanEntry
    78  		if aborted, readErr := sc.Get(context.Background(), e, testTxnID, &actual); readErr != nil {
    79  			t.Errorf("unexpected failure getting response: %s", readErr)
    80  		} else if expAbort != aborted {
    81  			t.Errorf("got aborted: %t; expected %t", aborted, expAbort)
    82  		} else if !reflect.DeepEqual(expEntry, actual) {
    83  			t.Fatalf("wanted %v, got %v", expEntry, actual)
    84  		}
    85  	}
    86  
    87  	tryHit(true, entry)
    88  	if err := sc.ClearData(e); err != nil {
    89  		t.Error(err)
    90  	}
    91  	tryHit(false, roachpb.AbortSpanEntry{})
    92  }
    93  
    94  // TestAbortSpanEmptyParams tests operation with empty parameters.
    95  func TestAbortSpanEmptyParams(t *testing.T) {
    96  	defer leaktest.AfterTest(t)()
    97  	stopper := stop.NewStopper()
    98  	defer stopper.Stop(context.Background())
    99  	sc, e := createTestAbortSpan(t, 1, stopper)
   100  
   101  	entry := roachpb.AbortSpanEntry{
   102  		Key:       testTxnKey,
   103  		Timestamp: testTxnTimestamp,
   104  		Priority:  testTxnPriority,
   105  	}
   106  	// Put value for test response.
   107  	if err := sc.Put(context.Background(), e, nil, testTxnID, &entry); err != nil {
   108  		t.Errorf("unexpected error putting response: %+v", err)
   109  	}
   110  }