github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/roachpb/string_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_test 12 13 import ( 14 "fmt" 15 "testing" 16 17 // Hook up the pretty printer. 18 _ "github.com/cockroachdb/cockroach/pkg/keys" 19 "github.com/cockroachdb/cockroach/pkg/roachpb" 20 "github.com/cockroachdb/cockroach/pkg/storage/enginepb" 21 "github.com/cockroachdb/cockroach/pkg/util/hlc" 22 "github.com/cockroachdb/cockroach/pkg/util/uuid" 23 ) 24 25 func TestTransactionString(t *testing.T) { 26 txnID, err := uuid.FromBytes([]byte("ת\x0f^\xe4-Fؽ\xf7\x16\xe4\xf9\xbe^\xbe")) 27 if err != nil { 28 t.Fatal(err) 29 } 30 txn := roachpb.Transaction{ 31 TxnMeta: enginepb.TxnMeta{ 32 Key: roachpb.Key("foo"), 33 ID: txnID, 34 Epoch: 2, 35 WriteTimestamp: hlc.Timestamp{WallTime: 20, Logical: 21}, 36 MinTimestamp: hlc.Timestamp{WallTime: 10, Logical: 11}, 37 Priority: 957356782, 38 Sequence: 15, 39 }, 40 Name: "name", 41 Status: roachpb.COMMITTED, 42 LastHeartbeat: hlc.Timestamp{WallTime: 10, Logical: 11}, 43 ReadTimestamp: hlc.Timestamp{WallTime: 30, Logical: 31}, 44 MaxTimestamp: hlc.Timestamp{WallTime: 40, Logical: 41}, 45 } 46 expStr := `"name" meta={id=d7aa0f5e key="foo" pri=44.58039917 epo=2 ts=0.000000020,21 min=0.000000010,11 seq=15}` + 47 ` lock=true stat=COMMITTED rts=0.000000030,31 wto=false max=0.000000040,41` 48 49 if str := txn.String(); str != expStr { 50 t.Errorf( 51 "expected txn: %s\n"+ 52 "got: %s", 53 expStr, str) 54 } 55 } 56 57 func TestBatchRequestString(t *testing.T) { 58 br := roachpb.BatchRequest{} 59 txn := roachpb.MakeTransaction( 60 "test", 61 nil, /* baseKey */ 62 roachpb.NormalUserPriority, 63 hlc.Timestamp{}, // now 64 0, // maxOffsetNs 65 ) 66 br.Txn = &txn 67 for i := 0; i < 100; i++ { 68 var ru roachpb.RequestUnion 69 ru.MustSetInner(&roachpb.GetRequest{}) 70 br.Requests = append(br.Requests, ru) 71 } 72 var ru roachpb.RequestUnion 73 ru.MustSetInner(&roachpb.EndTxnRequest{}) 74 br.Requests = append(br.Requests, ru) 75 76 e := fmt.Sprintf(`[txn: %s], Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), ... 76 skipped ..., Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), Get [/Min,/Min), EndTxn(commit:false tsflex:false) [/Min] `, 77 br.Txn.Short()) 78 if e != br.String() { 79 t.Fatalf("e = %s\nv = %s", e, br.String()) 80 } 81 }