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

     1  // Copyright 2020 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 kvnemesis
    12  
    13  import (
    14  	"strings"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    18  	"github.com/cockroachdb/cockroach/pkg/storage"
    19  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    20  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestEngine(t *testing.T) {
    26  	defer leaktest.AfterTest(t)()
    27  
    28  	k := func(s string, ts hlc.Timestamp) storage.MVCCKey {
    29  		return storage.MVCCKey{Key: []byte(s), Timestamp: ts}
    30  	}
    31  	var missing roachpb.Value
    32  	v := func(s string, ts hlc.Timestamp) roachpb.Value {
    33  		v := roachpb.MakeValueFromString(s)
    34  		v.Timestamp = ts
    35  		return v
    36  	}
    37  	ts := func(i int) hlc.Timestamp {
    38  		return hlc.Timestamp{WallTime: int64(i)}
    39  	}
    40  
    41  	e, err := MakeEngine()
    42  	require.NoError(t, err)
    43  	defer e.Close()
    44  	assert.Equal(t, missing, e.Get(roachpb.Key(`a`), ts(1)))
    45  	e.Put(k(`a`, ts(1)), roachpb.MakeValueFromString(`a-1`).RawBytes)
    46  	e.Put(k(`a`, ts(2)), roachpb.MakeValueFromString(`a-2`).RawBytes)
    47  	e.Put(k(`b`, ts(2)), roachpb.MakeValueFromString(`b-2`).RawBytes)
    48  	assert.Equal(t, v(`a-2`, ts(2)), e.Get(roachpb.Key(`a`), ts(3)))
    49  	assert.Equal(t, v(`a-2`, ts(2)), e.Get(roachpb.Key(`a`), ts(2)))
    50  	assert.Equal(t, v(`a-1`, ts(1)), e.Get(roachpb.Key(`a`), ts(2).Prev()))
    51  	assert.Equal(t, v(`a-1`, ts(1)), e.Get(roachpb.Key(`a`), ts(1)))
    52  	assert.Equal(t, missing, e.Get(roachpb.Key(`a`), ts(1).Prev()))
    53  	assert.Equal(t, v(`b-2`, ts(2)), e.Get(roachpb.Key(`b`), ts(3)))
    54  	assert.Equal(t, v(`b-2`, ts(2)), e.Get(roachpb.Key(`b`), ts(2)))
    55  	assert.Equal(t, missing, e.Get(roachpb.Key(`b`), ts(1)))
    56  
    57  	assert.Equal(t, strings.TrimSpace(`
    58  "a" 0.000000002,0 -> /BYTES/a-2
    59  "a" 0.000000001,0 -> /BYTES/a-1
    60  "b" 0.000000002,0 -> /BYTES/b-2
    61  	`), e.DebugPrint(""))
    62  }