github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/gc/gc_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 gc
    12  
    13  import (
    14  	"bytes"
    15  	"context"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/cockroachdb/cockroach/pkg/config/zonepb"
    20  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    21  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    22  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestCalculateThreshold(t *testing.T) {
    27  	for _, c := range []struct {
    28  		ttlSeconds int32
    29  		ts         hlc.Timestamp
    30  	}{
    31  		{
    32  			ts:         hlc.Timestamp{WallTime: time.Hour.Nanoseconds(), Logical: 0},
    33  			ttlSeconds: 1,
    34  		},
    35  	} {
    36  		policy := zonepb.GCPolicy{TTLSeconds: c.ttlSeconds}
    37  		require.Equal(t, c.ts, TimestampForThreshold(CalculateThreshold(c.ts, policy), policy))
    38  	}
    39  }
    40  
    41  type collectingGCer struct {
    42  	keys [][]roachpb.GCRequest_GCKey
    43  }
    44  
    45  func (c *collectingGCer) GC(_ context.Context, keys []roachpb.GCRequest_GCKey) error {
    46  	c.keys = append(c.keys, keys)
    47  	return nil
    48  }
    49  
    50  func TestBatchingInlineGCer(t *testing.T) {
    51  	defer leaktest.AfterTest(t)()
    52  
    53  	ctx := context.Background()
    54  	c := &collectingGCer{}
    55  	m := makeBatchingInlineGCer(c, func(err error) { t.Error(err) })
    56  	if m.max == 0 {
    57  		t.Fatal("did not init max")
    58  	}
    59  	m.max = 10 // something reasonable for this unit test
    60  
    61  	long := roachpb.GCRequest_GCKey{
    62  		Key: bytes.Repeat([]byte("x"), m.max-1),
    63  	}
    64  	short := roachpb.GCRequest_GCKey{
    65  		Key: roachpb.Key("q"),
    66  	}
    67  
    68  	m.FlushingAdd(ctx, long.Key)
    69  	require.Nil(t, c.keys) // no flush
    70  
    71  	m.FlushingAdd(ctx, short.Key)
    72  	// Flushed long and short.
    73  	require.Len(t, c.keys, 1)
    74  	require.Len(t, c.keys[0], 2)
    75  	require.Equal(t, long, c.keys[0][0])
    76  	require.Equal(t, short, c.keys[0][1])
    77  	// Reset itself properly.
    78  	require.Nil(t, m.gcKeys)
    79  	require.Zero(t, m.size)
    80  
    81  	m.FlushingAdd(ctx, short.Key)
    82  	require.Len(t, c.keys, 1) // no flush
    83  
    84  	m.Flush(ctx)
    85  	require.Len(t, c.keys, 2) // flushed
    86  	require.Len(t, c.keys[1], 1)
    87  	require.Equal(t, short, c.keys[1][0])
    88  	// Reset itself properly.
    89  	require.Nil(t, m.gcKeys)
    90  	require.Zero(t, m.size)
    91  }