github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/record/rotation_test.go (about)

     1  // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package record
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  	"testing"
    11  
    12  	"github.com/cockroachdb/datadriven"
    13  )
    14  
    15  func TestRotation(t *testing.T) {
    16  	var rh RotationHelper
    17  	datadriven.RunTest(t, "testdata/rotation", func(t *testing.T, td *datadriven.TestData) string {
    18  		oneIntArg := func() int64 {
    19  			if len(td.CmdArgs) != 1 {
    20  				td.Fatalf(t, "expected one integer argument")
    21  			}
    22  			n, err := strconv.Atoi(td.CmdArgs[0].String())
    23  			if err != nil {
    24  				td.Fatalf(t, "expected one integer argument")
    25  			}
    26  			return int64(n)
    27  		}
    28  		switch td.Cmd {
    29  		case "add":
    30  			size := oneIntArg()
    31  			rh.AddRecord(size)
    32  
    33  		case "should-rotate":
    34  			nextSnapshotSize := oneIntArg()
    35  			return fmt.Sprint(rh.ShouldRotate(nextSnapshotSize))
    36  
    37  		case "rotate":
    38  			snapshotSize := oneIntArg()
    39  			rh.Rotate(snapshotSize)
    40  
    41  		default:
    42  			td.Fatalf(t, "unknown command %s", td.Cmd)
    43  		}
    44  
    45  		// For commands with no output, show the debug info.
    46  		a, b := rh.DebugInfo()
    47  		return fmt.Sprintf("last-snapshot-size: %d\nsize-since-last-snapshot: %d", a, b)
    48  	})
    49  }