MCPcopy
hub / github.com/etcd-io/bbolt / TestTx_TruncateBeforeWrite

Function TestTx_TruncateBeforeWrite

tx_test.go:1017–1056  ·  view source on GitHub ↗

TestTx_TruncateBeforeWrite ensures the file is truncated ahead whether we sync freelist or not.

(t *testing.T)

Source from the content-addressed store, hash-verified

1015
1016// TestTx_TruncateBeforeWrite ensures the file is truncated ahead whether we sync freelist or not.
1017func TestTx_TruncateBeforeWrite(t *testing.T) {
1018 if runtime.GOOS == "windows" {
1019 return
1020 }
1021 for _, isSyncFreelist := range []bool{false, true} {
1022 t.Run(fmt.Sprintf("isSyncFreelist:%v", isSyncFreelist), func(t *testing.T) {
1023 // Open the database.
1024 db := btesting.MustCreateDBWithOption(t, &bolt.Options{
1025 NoFreelistSync: isSyncFreelist,
1026 })
1027
1028 bigvalue := make([]byte, db.AllocSize/100)
1029 count := 0
1030 for {
1031 count++
1032 tx, err := db.Begin(true)
1033 require.NoError(t, err)
1034 b, err := tx.CreateBucketIfNotExists([]byte("bucket"))
1035 require.NoError(t, err)
1036 err = b.Put([]byte{byte(count)}, bigvalue)
1037 require.NoError(t, err)
1038 err = tx.Commit()
1039 require.NoError(t, err)
1040
1041 size := fileSize(db.Path())
1042
1043 if size > int64(db.AllocSize) && size < int64(db.AllocSize)*2 {
1044 // db.grow expands the file aggresively, that double the size while smaller than db.AllocSize,
1045 // or increase with a step of db.AllocSize if larger, by which we can test if db.grow has run.
1046 t.Fatalf("db.grow doesn't run when file size changes. file size: %d", size)
1047 }
1048 if size > int64(db.AllocSize) {
1049 break
1050 }
1051 }
1052 db.MustClose()
1053 db.MustDeleteFile()
1054 })
1055 }
1056}

Callers

nothing calls this directly

Calls 11

MustCreateDBWithOptionFunction · 0.92
fileSizeFunction · 0.85
RunMethod · 0.80
BeginMethod · 0.80
CommitMethod · 0.80
MustCloseMethod · 0.80
MustDeleteFileMethod · 0.80
FatalfMethod · 0.65
PutMethod · 0.45
PathMethod · 0.45

Tested by

no test coverage detected