github.com/andy-kimball/arenaskl@v0.0.0-20200617143215-f701008588b9/race_test.go (about)

     1  /*
     2   * Copyright 2020 Dgraph Labs, Inc. and Contributors
     3   * Modifications copyright (C) 2020 Andy Kimball and Contributors
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package arenaskl
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  // TestNodeArenaEnd tests allocating a node at the boundary of an arena. In Go
    27  // 1.14 when the race detector is running, Go will also perform some pointer
    28  // alignment checks. It will detect alignment issues where a node's memory would
    29  // straddle the arena boundary, with unused regions of the node struct dipping
    30  // into unallocated memory. This test is only run when the race build tag is
    31  // provided.
    32  func TestNodeArenaEnd(t *testing.T) {
    33  	// Rather than hardcode an arena size at just the right size, try
    34  	// allocating using successively larger arena sizes until we allocate
    35  	// successfully. The prior attempt will have exercised the right code
    36  	// path.
    37  	for i := uint32(1); i < 256; i++ {
    38  		a := NewArena(i)
    39  		_, err := newNode(a, 1)
    40  		if err == nil {
    41  			// We reached an arena size big enough to allocate a node. If
    42  			// there's an issue at the boundary, the race detector would have
    43  			// found it by now.
    44  			t.Log(i)
    45  			break
    46  		}
    47  		require.Equal(t, ErrArenaFull, err)
    48  	}
    49  }