本文整理匯總了Golang中github.com/cockroachdb/cockroach/util/uuid.NewUUID4函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewUUID4函數的具體用法?Golang NewUUID4怎麽用?Golang NewUUID4使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewUUID4函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestTimestampCacheWithTxnID
// TestTimestampCacheWithTxnID verifies that timestamps matching
// the specified txn ID are ignored.
func TestTimestampCacheWithTxnID(t *testing.T) {
defer leaktest.AfterTest(t)
manual := hlc.NewManualClock(0)
clock := hlc.NewClock(manual.UnixNano)
tc := NewTimestampCache(clock)
// Add two successive txn entries.
txn1ID := uuid.NewUUID4()
txn2ID := uuid.NewUUID4()
ts1 := clock.Now()
tc.Add(roachpb.Key("a"), roachpb.Key("c"), ts1, txn1ID, true)
ts2 := clock.Now()
// This entry will remove "a"-"b" from the cache.
tc.Add(roachpb.Key("b"), roachpb.Key("d"), ts2, txn2ID, true)
// Fetching with no transaction gets latest value.
if ts, _ := tc.GetMax(roachpb.Key("b"), nil, nil); !ts.Equal(ts2) {
t.Errorf("expected %s; got %s", ts2, ts)
}
// Fetching with txn ID "1" gets most recent.
if ts, _ := tc.GetMax(roachpb.Key("b"), nil, txn1ID); !ts.Equal(ts2) {
t.Errorf("expected %s; got %s", ts2, ts)
}
// Fetching with txn ID "2" skips most recent.
if ts, _ := tc.GetMax(roachpb.Key("b"), nil, txn2ID); !ts.Equal(ts1) {
t.Errorf("expected %s; got %s", ts1, ts)
}
}
示例2: TestUUID
func TestUUID(t *testing.T) {
uuid1 := uuid.NewUUID4()
uuid2 := uuid.NewUUID4()
if bytes.Equal(uuid1, uuid2) {
t.Errorf("consecutive uuids equal %s", uuid1)
}
}
示例3: TestTimestampCacheReadVsWrite
// TestTimestampCacheReadVsWrite verifies that the timestamp cache
// can differentiate between read and write timestamp.
func TestTimestampCacheReadVsWrite(t *testing.T) {
defer leaktest.AfterTest(t)
manual := hlc.NewManualClock(0)
clock := hlc.NewClock(manual.UnixNano)
tc := NewTimestampCache(clock)
// Add read-only non-txn entry at current time.
ts1 := clock.Now()
tc.Add(roachpb.Key("a"), roachpb.Key("b"), ts1, nil, true)
// Add two successive txn entries; one read-only and one read-write.
txn1ID := uuid.NewUUID4()
txn2ID := uuid.NewUUID4()
ts2 := clock.Now()
tc.Add(roachpb.Key("a"), nil, ts2, txn1ID, true)
ts3 := clock.Now()
tc.Add(roachpb.Key("a"), nil, ts3, txn2ID, false)
// Fetching with no transaction gets latest values.
if rTS, wTS := tc.GetMax(roachpb.Key("a"), nil, nil); !rTS.Equal(ts2) || !wTS.Equal(ts3) {
t.Errorf("expected %s %s; got %s %s", ts2, ts3, rTS, wTS)
}
// Fetching with txn ID "1" gets original for read and most recent for write.
if rTS, wTS := tc.GetMax(roachpb.Key("a"), nil, txn1ID); !rTS.Equal(ts1) || !wTS.Equal(ts3) {
t.Errorf("expected %s %s; got %s %s", ts1, ts3, rTS, wTS)
}
// Fetching with txn ID "2" gets ts2 for read and low water mark for write.
if rTS, wTS := tc.GetMax(roachpb.Key("a"), nil, txn2ID); !rTS.Equal(ts2) || !wTS.Equal(tc.lowWater) {
t.Errorf("expected %s %s; got %s %s", ts2, tc.lowWater, rTS, wTS)
}
}
示例4: TestRocksDBCompaction
// TestRocksDBCompaction verifies that a garbage collector can be
// installed on a RocksDB engine and will properly compact response
// cache and transaction entries.
func TestRocksDBCompaction(t *testing.T) {
defer leaktest.AfterTest(t)
gob.Register(proto.Timestamp{})
rocksdb := newMemRocksDB(proto.Attributes{Attrs: []string{"ssd"}}, testCacheSize)
err := rocksdb.Open()
if err != nil {
t.Fatalf("could not create new in-memory rocksdb db instance: %v", err)
}
rocksdb.SetGCTimeouts(1, 2)
defer rocksdb.Close()
cmdID := &proto.ClientCmdID{WallTime: 1, Random: 1}
// Write two transaction values and two response cache values such
// that exactly one of each should be GC'd based on our GC timeouts.
kvs := []proto.KeyValue{
{
Key: keys.ResponseCacheKey(1, cmdID),
Value: proto.Value{Bytes: encodePutResponse(makeTS(2, 0), t)},
},
{
Key: keys.ResponseCacheKey(2, cmdID),
Value: proto.Value{Bytes: encodePutResponse(makeTS(3, 0), t)},
},
{
Key: keys.TransactionKey(proto.Key("a"), proto.Key(uuid.NewUUID4())),
Value: proto.Value{Bytes: encodeTransaction(makeTS(1, 0), t)},
},
{
Key: keys.TransactionKey(proto.Key("b"), proto.Key(uuid.NewUUID4())),
Value: proto.Value{Bytes: encodeTransaction(makeTS(2, 0), t)},
},
}
for _, kv := range kvs {
if err := MVCCPut(rocksdb, nil, kv.Key, proto.ZeroTimestamp, kv.Value, nil); err != nil {
t.Fatal(err)
}
}
// Compact range and scan remaining values to compare.
rocksdb.CompactRange(nil, nil)
actualKVs, _, err := MVCCScan(rocksdb, proto.KeyMin, proto.KeyMax,
0, proto.ZeroTimestamp, true, nil)
if err != nil {
t.Fatalf("could not run scan: %v", err)
}
var keys []proto.Key
for _, kv := range actualKVs {
keys = append(keys, kv.Key)
}
expKeys := []proto.Key{
kvs[1].Key,
kvs[3].Key,
}
if !reflect.DeepEqual(expKeys, keys) {
t.Errorf("expected keys %+v, got keys %+v", expKeys, keys)
}
}
示例5: TestTimestampCacheReplacements
// TestTimestampCacheReplacements verifies that a newer entry
// in the timestamp cache which completely "covers" an older
// entry will replace it.
func TestTimestampCacheReplacements(t *testing.T) {
defer leaktest.AfterTest(t)
manual := hlc.NewManualClock(0)
clock := hlc.NewClock(manual.UnixNano)
tc := NewTimestampCache(clock)
txn1ID := uuid.NewUUID4()
txn2ID := uuid.NewUUID4()
ts1 := clock.Now()
tc.Add(roachpb.Key("a"), nil, ts1, nil, true)
if ts, _ := tc.GetMax(roachpb.Key("a"), nil, nil); !ts.Equal(ts1) {
t.Errorf("expected %s; got %s", ts1, ts)
}
// Write overlapping value with txn1 and verify with txn1--we should get
// low water mark, not ts1.
ts2 := clock.Now()
tc.Add(roachpb.Key("a"), nil, ts2, txn1ID, true)
if ts, _ := tc.GetMax(roachpb.Key("a"), nil, txn1ID); !ts.Equal(tc.lowWater) {
t.Errorf("expected low water (empty) time; got %s", ts)
}
// Write range which overlaps "a" with txn2 and verify with txn2--we should
// get low water mark, not ts2.
ts3 := clock.Now()
tc.Add(roachpb.Key("a"), roachpb.Key("c"), ts3, txn2ID, true)
if ts, _ := tc.GetMax(roachpb.Key("a"), nil, txn2ID); !ts.Equal(tc.lowWater) {
t.Errorf("expected low water (empty) time; got %s", ts)
}
// Also, verify txn1 sees ts3.
if ts, _ := tc.GetMax(roachpb.Key("a"), nil, txn1ID); !ts.Equal(ts3) {
t.Errorf("expected %s; got %s", ts3, ts)
}
// Now, write to "b" with a higher timestamp and no txn. Should be
// visible to all txns.
ts4 := clock.Now()
tc.Add(roachpb.Key("b"), nil, ts4, nil, true)
if ts, _ := tc.GetMax(roachpb.Key("b"), nil, nil); !ts.Equal(ts4) {
t.Errorf("expected %s; got %s", ts4, ts)
}
if ts, _ := tc.GetMax(roachpb.Key("b"), nil, txn1ID); !ts.Equal(ts4) {
t.Errorf("expected %s; got %s", ts4, ts)
}
// Finally, write an earlier version of "a"; should simply get
// tossed and we should see ts4 still.
tc.Add(roachpb.Key("b"), nil, ts1, nil, true)
if ts, _ := tc.GetMax(roachpb.Key("b"), nil, nil); !ts.Equal(ts4) {
t.Errorf("expected %s; got %s", ts4, ts)
}
}
示例6: newTestSender
func newTestSender(handler func(proto.Call)) SenderFunc {
txnKey := proto.Key("test-txn")
txnID := []byte(uuid.NewUUID4())
return func(_ context.Context, call proto.Call) {
header := call.Args.Header()
header.UserPriority = gogoproto.Int32(-1)
if header.Txn != nil && len(header.Txn.ID) == 0 {
header.Txn.Key = txnKey
header.Txn.ID = txnID
}
call.Reply.Reset()
var writing bool
switch call.Args.(type) {
case *proto.PutRequest:
gogoproto.Merge(call.Reply, testPutResp)
writing = true
case *proto.EndTransactionRequest:
writing = true
default:
// Do nothing.
}
call.Reply.Header().Txn = gogoproto.Clone(call.Args.Header().Txn).(*proto.Transaction)
if txn := call.Reply.Header().Txn; txn != nil {
txn.Writing = writing
}
if handler != nil {
handler(call)
}
}
}
示例7: newBlockWriter
func newBlockWriter(db *sql.DB) blockWriter {
source := rand.NewSource(int64(time.Now().UnixNano()))
return blockWriter{
db: db,
id: uuid.NewUUID4().String(),
rand: rand.New(source),
}
}
示例8: TestRocksDBCompaction
// TestRocksDBCompaction verifies that a garbage collector can be
// installed on a RocksDB engine and will properly compact transaction
// entries.
func TestRocksDBCompaction(t *testing.T) {
defer leaktest.AfterTest(t)
stopper := stop.NewStopper()
defer stopper.Stop()
rocksdb := newMemRocksDB(roachpb.Attributes{}, testCacheSize, stopper)
err := rocksdb.Open()
if err != nil {
t.Fatalf("could not create new in-memory rocksdb db instance: %v", err)
}
rocksdb.SetGCTimeouts(1)
// Write two transaction values such that exactly one should be GC'd based
// on our GC timeouts.
kvs := []roachpb.KeyValue{
{
Key: keys.TransactionKey(roachpb.Key("a"), roachpb.Key(uuid.NewUUID4())),
Value: roachpb.MakeValueFromBytes(encodeTransaction(makeTS(1, 0), t)),
},
{
Key: keys.TransactionKey(roachpb.Key("b"), roachpb.Key(uuid.NewUUID4())),
Value: roachpb.MakeValueFromBytes(encodeTransaction(makeTS(2, 0), t)),
},
}
for _, kv := range kvs {
if err := MVCCPut(rocksdb, nil, kv.Key, roachpb.ZeroTimestamp, kv.Value, nil); err != nil {
t.Fatal(err)
}
}
// Compact range and scan remaining values to compare.
rocksdb.CompactRange(nil, nil)
actualKVs, _, err := MVCCScan(rocksdb, keyMin, keyMax, 0, roachpb.ZeroTimestamp, true, nil)
if err != nil {
t.Fatalf("could not run scan: %v", err)
}
var keys []roachpb.Key
for _, kv := range actualKVs {
keys = append(keys, kv.Key)
}
expKeys := []roachpb.Key{
kvs[1].Key,
}
if !reflect.DeepEqual(expKeys, keys) {
t.Errorf("expected keys %+v, got keys %+v", expKeys, keys)
}
}
示例9: newTestSender
// TestSender mocks out some of the txn coordinator sender's
// functionality. It responds to PutRequests using testPutResp.
func newTestSender(pre, post func(roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error)) SenderFunc {
txnKey := roachpb.Key("test-txn")
txnID := []byte(uuid.NewUUID4())
return func(_ context.Context, ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
ba.UserPriority = 1
if ba.Txn != nil && len(ba.Txn.ID) == 0 {
ba.Txn.Key = txnKey
ba.Txn.ID = txnID
}
var br *roachpb.BatchResponse
var pErr *roachpb.Error
if pre != nil {
br, pErr = pre(ba)
} else {
br = ba.CreateReply()
}
if pErr != nil {
return nil, pErr
}
var writing bool
status := roachpb.PENDING
for i, req := range ba.Requests {
args := req.GetInner()
if _, ok := args.(*roachpb.PutRequest); ok {
if !br.Responses[i].SetValue(proto.Clone(testPutResp).(roachpb.Response)) {
panic("failed to set put response")
}
}
if roachpb.IsTransactionWrite(args) {
writing = true
}
}
if args, ok := ba.GetArg(roachpb.EndTransaction); ok {
et := args.(*roachpb.EndTransactionRequest)
writing = true
if et.Commit {
status = roachpb.COMMITTED
} else {
status = roachpb.ABORTED
}
}
if ba.Txn != nil {
txnClone := ba.Txn.Clone()
br.Txn = &txnClone
if pErr == nil {
br.Txn.Writing = writing
br.Txn.Status = status
}
}
if post != nil {
br, pErr = post(ba)
}
return br, pErr
}
}
示例10: TestTxnIDEqual
func TestTxnIDEqual(t *testing.T) {
txn1, txn2 := uuid.NewUUID4(), uuid.NewUUID4()
txn1Copy := append([]byte(nil), txn1...)
testCases := []struct {
a, b []byte
expEqual bool
}{
{txn1, txn1, true},
{txn1, txn2, false},
{txn1, txn1Copy, true},
}
for i, test := range testCases {
if eq := TxnIDEqual(test.a, test.b); eq != test.expEqual {
t.Errorf("%d: expected %q == %q: %t; got %t", i, test.a, test.b, test.expEqual, eq)
}
}
}
示例11: TestKeyAddress
func TestKeyAddress(t *testing.T) {
defer leaktest.AfterTest(t)
testCases := []struct {
key, expAddress roachpb.Key
}{
{roachpb.Key{}, roachpb.KeyMin},
{roachpb.Key("123"), roachpb.Key("123")},
{RangeDescriptorKey(roachpb.Key("foo")), roachpb.Key("foo")},
{TransactionKey(roachpb.Key("baz"), roachpb.Key(uuid.NewUUID4())), roachpb.Key("baz")},
{TransactionKey(roachpb.KeyMax, roachpb.Key(uuid.NewUUID4())), roachpb.KeyMax},
{nil, nil},
}
for i, test := range testCases {
result := KeyAddress(test.key)
if !result.Equal(test.expAddress) {
t.Errorf("%d: expected address for key %q doesn't match %q", i, test.key, test.expAddress)
}
}
}
示例12: TestKeyAddress
func TestKeyAddress(t *testing.T) {
defer leaktest.AfterTest(t)
testCases := []struct {
key, expAddress proto.Key
}{
{proto.Key{}, proto.KeyMin},
{proto.Key("123"), proto.Key("123")},
{MakeKey(ConfigAccountingPrefix, proto.Key("foo")), proto.Key("\x00acctfoo")},
{RangeDescriptorKey(proto.Key("foo")), proto.Key("foo")},
{TransactionKey(proto.Key("baz"), proto.Key(uuid.NewUUID4())), proto.Key("baz")},
{TransactionKey(proto.KeyMax, proto.Key(uuid.NewUUID4())), proto.KeyMax},
{nil, nil},
}
for i, test := range testCases {
result := KeyAddress(test.key)
if !result.Equal(test.expAddress) {
t.Errorf("%d: expected address for key %q doesn't match %q", i, test.key, test.expAddress)
}
}
}
示例13: newTestSender
func newTestSender(pre, post func(proto.BatchRequest) (*proto.BatchResponse, *proto.Error)) SenderFunc {
txnKey := proto.Key("test-txn")
txnID := []byte(uuid.NewUUID4())
return func(_ context.Context, ba proto.BatchRequest) (*proto.BatchResponse, *proto.Error) {
ba.UserPriority = gogoproto.Int32(-1)
if ba.Txn != nil && len(ba.Txn.ID) == 0 {
ba.Txn.Key = txnKey
ba.Txn.ID = txnID
}
var br *proto.BatchResponse
var pErr *proto.Error
if pre != nil {
br, pErr = pre(ba)
} else {
br = &proto.BatchResponse{}
}
if pErr != nil {
return nil, pErr
}
var writing bool
status := proto.PENDING
if _, ok := ba.GetArg(proto.Put); ok {
br.Add(gogoproto.Clone(testPutResp).(proto.Response))
writing = true
}
if args, ok := ba.GetArg(proto.EndTransaction); ok {
et := args.(*proto.EndTransactionRequest)
writing = true
if et.Commit {
status = proto.COMMITTED
} else {
status = proto.ABORTED
}
}
br.Txn = gogoproto.Clone(ba.Txn).(*proto.Transaction)
if br.Txn != nil && pErr == nil {
br.Txn.Writing = writing
br.Txn.Status = status
}
if post != nil {
br, pErr = post(ba)
}
return br, pErr
}
}
示例14: TestTransactionUpdate
func TestTransactionUpdate(t *testing.T) {
nodes := NodeList{
Nodes: []int32{101, 103, 105},
}
ts := makeTS(10, 11)
txn := Transaction{
Name: "name",
Key: Key("foo"),
ID: uuid.NewUUID4(),
Priority: 957356782,
Isolation: SNAPSHOT,
Status: COMMITTED,
Epoch: 2,
LastHeartbeat: &ts,
Timestamp: makeTS(20, 21),
OrigTimestamp: makeTS(30, 31),
MaxTimestamp: makeTS(40, 41),
CertainNodes: nodes,
Writing: true,
Sequence: 123,
}
noZeroField := func(txn Transaction) error {
ele := reflect.ValueOf(&txn).Elem()
eleT := ele.Type()
for i := 0; i < ele.NumField(); i++ {
f := ele.Field(i)
zero := reflect.Zero(f.Type())
if reflect.DeepEqual(f.Interface(), zero.Interface()) {
return fmt.Errorf("expected %s field to be non-zero", eleT.Field(i).Name)
}
}
return nil
}
if err := noZeroField(txn); err != nil {
t.Fatal(err)
}
var txn2 Transaction
txn2.Update(&txn)
if err := noZeroField(txn2); err != nil {
t.Fatal(err)
}
}
示例15: newTestSender
func newTestSender(pre, post func(proto.Call)) SenderFunc {
txnKey := proto.Key("test-txn")
txnID := []byte(uuid.NewUUID4())
return func(_ context.Context, call proto.Call) {
header := call.Args.Header()
header.UserPriority = gogoproto.Int32(-1)
if header.Txn != nil && len(header.Txn.ID) == 0 {
header.Txn.Key = txnKey
header.Txn.ID = txnID
}
call.Reply.Reset()
if pre != nil {
pre(call)
}
var writing bool
var status proto.TransactionStatus
switch t := call.Args.(type) {
case *proto.PutRequest:
gogoproto.Merge(call.Reply, testPutResp)
writing = true
case *proto.EndTransactionRequest:
writing = true
if t.Commit {
status = proto.COMMITTED
} else {
status = proto.ABORTED
}
default:
// Do nothing.
}
call.Reply.Header().Txn = gogoproto.Clone(call.Args.Header().Txn).(*proto.Transaction)
if txn := call.Reply.Header().Txn; txn != nil && call.Reply.Header().GoError() == nil {
txn.Writing = writing
txn.Status = status
}
if post != nil {
post(call)
}
}
}