github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/logbuffer/logbuffers_test_helper.go (about)

     1  // Copyright 2016 Stanislav Liberman
     2  // Copyright 2022 Steven Stern
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  // http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package logbuffer
    17  
    18  import (
    19  	"github.com/lirm/aeron-go/aeron/atomic"
    20  	"github.com/lirm/aeron-go/aeron/util/memmap"
    21  	"os"
    22  	"unsafe"
    23  )
    24  
    25  const Filename string = "logbuffers.bin"
    26  
    27  // NewTestingLogbuffer is a helper function to create a new memmap file and logbuffer to wrap it.
    28  func NewTestingLogbuffer() (*LogBuffers, error) {
    29  	var logLength = (TermMinLength * PartitionCount) + LogMetaDataLength
    30  	mmap, err := memmap.NewFile(Filename, 0, int(logLength))
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	basePtr := uintptr(mmap.GetMemoryPtr())
    35  	ptr := unsafe.Pointer(basePtr + uintptr(int64(logLength-LogMetaDataLength)))
    36  	buf := atomic.MakeBuffer(ptr, LogMetaDataLength)
    37  
    38  	var meta LogBufferMetaData
    39  	meta.Wrap(buf, 0)
    40  
    41  	meta.TermLen.Set(1024)
    42  
    43  	if err := mmap.Close(); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return Wrap(Filename), nil
    48  }
    49  
    50  // RemoveTestingLogbufferFile is meant to be called by defer immediately after creating it above.
    51  func RemoveTestingLogbufferFile() error {
    52  	return os.Remove(Filename)
    53  }