From b09984ea1624940e38b5db1aac2035f934b3e339 Mon Sep 17 00:00:00 2001 From: Piotr Tabor Date: Fri, 16 Dec 2022 18:53:23 +0100 Subject: [PATCH] Fix bbolt test for 16KB block page (e.g. Mac with M1 CPU). The tests were failing when doing development on M1 mac. This was due to page-size difference (4KB assumed in tests) vs. the OS defaults. Signed-off-by: Piotr Tabor --- bucket_test.go | 165 +++++++++++++++++++++++++------------------------ go.mod | 11 +++- go.sum | 17 +++++ 3 files changed, 112 insertions(+), 81 deletions(-) diff --git a/bucket_test.go b/bucket_test.go index 2ac9263..2c53dba 100644 --- a/bucket_test.go +++ b/bucket_test.go @@ -13,6 +13,7 @@ import ( "testing" "testing/quick" + "github.com/stretchr/testify/assert" bolt "go.etcd.io/bbolt" ) @@ -390,7 +391,8 @@ func TestBucket_Delete_FreelistOverflow(t *testing.T) { defer db.MustClose() k := make([]byte, 16) - for i := uint64(0); i < 10000; i++ { + // The bigger the pages - the more values we need to write. + for i := uint64(0); i < 2*uint64(db.Info().PageSize); i++ { if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("0")) if err != nil { @@ -1209,8 +1211,9 @@ func TestBucket_Stats(t *testing.T) { t.Fatal(err) } } + longKeyLength := 10*db.Info().PageSize + 17 if err := db.Update(func(tx *bolt.Tx) error { - if err := tx.Bucket([]byte("woojits")).Put(bigKey, []byte(strings.Repeat("*", 10000))); err != nil { + if err := tx.Bucket([]byte("woojits")).Put(bigKey, []byte(strings.Repeat("*", longKeyLength))); err != nil { t.Fatal(err) } return nil @@ -1220,54 +1223,53 @@ func TestBucket_Stats(t *testing.T) { db.MustCheck() + pageSize2stats := map[int]bolt.BucketStats{ + 4096: { + BranchPageN: 1, + BranchOverflowN: 0, + LeafPageN: 7, + LeafOverflowN: 10, + KeyN: 501, + Depth: 2, + BranchAlloc: 4096, + BranchInuse: 149, + LeafAlloc: 69632, + LeafInuse: 0 + + 7*16 + // leaf page header (x LeafPageN) + 501*16 + // leaf elements + 500*3 + len(bigKey) + // leaf keys + 1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ... + BucketN: 1, + InlineBucketN: 0, + InlineBucketInuse: 0}, + 16384: { + BranchPageN: 1, + BranchOverflowN: 0, + LeafPageN: 3, + LeafOverflowN: 10, + KeyN: 501, + Depth: 2, + BranchAlloc: 16384, + BranchInuse: 73, + LeafAlloc: 212992, + LeafInuse: 0 + + 3*16 + // leaf page header (x LeafPageN) + 501*16 + // leaf elements + 500*3 + len(bigKey) + // leaf keys + 1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ... + BucketN: 1, + InlineBucketN: 0, + InlineBucketInuse: 0}, + } + if err := db.View(func(tx *bolt.Tx) error { stats := tx.Bucket([]byte("woojits")).Stats() - if stats.BranchPageN != 1 { - t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN) - } else if stats.BranchOverflowN != 0 { - t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN) - } else if stats.LeafPageN != 7 { - t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN) - } else if stats.LeafOverflowN != 2 { - t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN) - } else if stats.KeyN != 501 { - t.Fatalf("unexpected KeyN: %d", stats.KeyN) - } else if stats.Depth != 2 { - t.Fatalf("unexpected Depth: %d", stats.Depth) + t.Logf("Stats: %#v", stats) + if expected, ok := pageSize2stats[db.Info().PageSize]; ok { + assert.EqualValues(t, expected, stats, "stats differs from expectations") + } else { + t.Skipf("No expectations for page size: %d", db.Info().PageSize) } - - branchInuse := 16 // branch page header - branchInuse += 7 * 16 // branch elements - branchInuse += 7 * 3 // branch keys (6 3-byte keys) - if stats.BranchInuse != branchInuse { - t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse) - } - - leafInuse := 7 * 16 // leaf page header - leafInuse += 501 * 16 // leaf elements - leafInuse += 500*3 + len(bigKey) // leaf keys - leafInuse += 1*10 + 2*90 + 3*400 + 10000 // leaf values - if stats.LeafInuse != leafInuse { - t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse) - } - - // Only check allocations for 4KB pages. - if db.Info().PageSize == 4096 { - if stats.BranchAlloc != 4096 { - t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc) - } else if stats.LeafAlloc != 36864 { - t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc) - } - } - - if stats.BucketN != 1 { - t.Fatalf("unexpected BucketN: %d", stats.BucketN) - } else if stats.InlineBucketN != 0 { - t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN) - } else if stats.InlineBucketInuse != 0 { - t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse) - } - return nil }); err != nil { t.Fatal(err) @@ -1599,42 +1601,45 @@ func TestBucket_Stats_Large(t *testing.T) { db.MustCheck() + pageSize2stats := map[int]bolt.BucketStats{ + 4096: { + BranchPageN: 13, + BranchOverflowN: 0, + LeafPageN: 1196, + LeafOverflowN: 0, + KeyN: 100000, + Depth: 3, + BranchAlloc: 53248, + BranchInuse: 25257, + LeafAlloc: 4898816, + LeafInuse: 2596916, + BucketN: 1, + InlineBucketN: 0, + InlineBucketInuse: 0}, + 16384: { + BranchPageN: 1, + BranchOverflowN: 0, + LeafPageN: 292, + LeafOverflowN: 0, + KeyN: 100000, + Depth: 2, + BranchAlloc: 16384, + BranchInuse: 6094, + LeafAlloc: 4784128, + LeafInuse: 2582452, + BucketN: 1, + InlineBucketN: 0, + InlineBucketInuse: 0}, + } + if err := db.View(func(tx *bolt.Tx) error { stats := tx.Bucket([]byte("widgets")).Stats() - if stats.BranchPageN != 13 { - t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN) - } else if stats.BranchOverflowN != 0 { - t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN) - } else if stats.LeafPageN != 1196 { - t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN) - } else if stats.LeafOverflowN != 0 { - t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN) - } else if stats.KeyN != 100000 { - t.Fatalf("unexpected KeyN: %d", stats.KeyN) - } else if stats.Depth != 3 { - t.Fatalf("unexpected Depth: %d", stats.Depth) - } else if stats.BranchInuse != 25257 { - t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse) - } else if stats.LeafInuse != 2596916 { - t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse) + t.Logf("Stats: %#v", stats) + if expected, ok := pageSize2stats[db.Info().PageSize]; ok { + assert.EqualValues(t, expected, stats, "stats differs from expectations") + } else { + t.Skipf("No expectations for page size: %d", db.Info().PageSize) } - - if db.Info().PageSize == 4096 { - if stats.BranchAlloc != 53248 { - t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc) - } else if stats.LeafAlloc != 4898816 { - t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc) - } - } - - if stats.BucketN != 1 { - t.Fatalf("unexpected BucketN: %d", stats.BucketN) - } else if stats.InlineBucketN != 0 { - t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN) - } else if stats.InlineBucketInuse != 0 { - t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse) - } - return nil }); err != nil { t.Fatal(err) diff --git a/go.mod b/go.mod index cbafe64..0bdc35f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,13 @@ module go.etcd.io/bbolt go 1.17 -require golang.org/x/sys v0.2.0 +require ( + github.com/stretchr/testify v1.8.1 + golang.org/x/sys v0.2.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index beac707..5873546 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,19 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=