github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/arenaskl/race_test.go (about)

     1  //go:build race
     2  // +build race
     3  
     4  // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     5  // of this source code is governed by a BSD-style license that can be found in
     6  // the LICENSE file.
     7  
     8  package arenaskl
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  // TestNodeArenaEnd tests allocating a node at the boundary of an arena. In Go
    17  // 1.14 when the race detector is running, Go will also perform some pointer
    18  // alignment checks. It will detect alignment issues, for example #667 where a
    19  // node's memory would straddle the arena boundary, with unused regions of the
    20  // node struct dipping into unallocated memory. This test is only run when the
    21  // race build tag is provided.
    22  func TestNodeArenaEnd(t *testing.T) {
    23  	ikey := makeIkey("a")
    24  	val := []byte("b")
    25  
    26  	// Rather than hardcode an arena size at just the right size, try
    27  	// allocating using successively larger arena sizes until we allocate
    28  	// successfully. The prior attempt will have exercised the right code
    29  	// path.
    30  	for i := uint32(1); i < 256; i++ {
    31  		a := newArena(i)
    32  		_, err := newNode(a, 1, ikey, val)
    33  		if err == nil {
    34  			// We reached an arena size big enough to allocate a node.
    35  			// If there's an issue at the boundary, the race detector would
    36  			// have found it by now.
    37  			t.Log(i)
    38  			break
    39  		}
    40  		require.Equal(t, ErrArenaFull, err)
    41  	}
    42  }