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

     1  // Copyright 2019 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 kvserver
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  // TestReplicatedCmdBuf verifies the replicatedCmdBuf behavior.
    21  func TestReplicatedCmdBuf(t *testing.T) {
    22  	defer leaktest.AfterTest(t)()
    23  	var buf replicatedCmdBuf
    24  	// numStates is chosen arbitrarily.
    25  	const numStates = 5*replicatedCmdBufNodeSize + 1
    26  	// Test that the len field is properly updated.
    27  	var states []*replicatedCmd
    28  	for i := 0; i < numStates; i++ {
    29  		assert.Equal(t, i, int(buf.len))
    30  		states = append(states, buf.allocate())
    31  		assert.Equal(t, i+1, int(buf.len))
    32  	}
    33  	// Test the iterator.
    34  	var it replicatedCmdBufSlice
    35  	i := 0
    36  	for it.init(&buf); it.Valid(); it.Next() {
    37  		assert.Equal(t, states[i], it.cur())
    38  		i++
    39  	}
    40  	assert.Equal(t, i, numStates) // make sure we saw them all
    41  	// Test clear.
    42  	buf.clear()
    43  	assert.EqualValues(t, buf, replicatedCmdBuf{})
    44  	assert.Equal(t, 0, int(buf.len))
    45  	it.init(&buf)
    46  	assert.False(t, it.Valid())
    47  	// Test clear on an empty buffer.
    48  	buf.clear()
    49  	assert.EqualValues(t, buf, replicatedCmdBuf{})
    50  }