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

Function TestTx_Check_WithNestBucket

tx_check_test.go:63–121  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

61}
62
63func TestTx_Check_WithNestBucket(t *testing.T) {
64 parentBucketName := []byte("parentBucket")
65
66 t.Log("Creating db file.")
67 db := btesting.MustCreateDBWithOption(t, &bbolt.Options{PageSize: 4096})
68
69 err := db.Update(func(tx *bbolt.Tx) error {
70 pb, bErr := tx.CreateBucket(parentBucketName)
71 if bErr != nil {
72 return bErr
73 }
74
75 t.Log("put some key/values under the parent bucket directly")
76 for i := 0; i < 10; i++ {
77 k, v := fmt.Sprintf("%04d", i), fmt.Sprintf("value_%4d", i)
78 if pErr := pb.Put([]byte(k), []byte(v)); pErr != nil {
79 return pErr
80 }
81 }
82
83 t.Log("create a nested bucket and put some key/values under the nested bucket")
84 cb, bErr := pb.CreateBucket([]byte("nestedBucket"))
85 if bErr != nil {
86 return bErr
87 }
88
89 for i := 0; i < 2000; i++ {
90 k, v := fmt.Sprintf("%04d", i), fmt.Sprintf("value_%4d", i)
91 if pErr := cb.Put([]byte(k), []byte(v)); pErr != nil {
92 return pErr
93 }
94 }
95
96 return nil
97 })
98 require.NoError(t, err)
99
100 // Get the bucket's root page.
101 bucketRootPageId := mustGetBucketRootPage(t, db.DB, parentBucketName)
102
103 t.Logf("Running consistency check starting from pageId: %d", bucketRootPageId)
104 vErr := db.View(func(tx *bbolt.Tx) error {
105 var cErrs []error
106
107 errChan := tx.Check(bbolt.WithPageId(uint64(bucketRootPageId)))
108 for cErr := range errChan {
109 cErrs = append(cErrs, cErr)
110 }
111 require.Equal(t, 0, len(cErrs))
112
113 return nil
114 })
115 require.NoError(t, vErr)
116 t.Log("All check passed")
117
118 // Manually close the db, otherwise the PostTestCleanup will
119 // check the db again and accordingly fail the test.
120 db.MustClose()

Callers

nothing calls this directly

Calls 9

MustCreateDBWithOptionFunction · 0.92
WithPageIdFunction · 0.92
mustGetBucketRootPageFunction · 0.85
UpdateMethod · 0.80
ViewMethod · 0.80
CheckMethod · 0.80
MustCloseMethod · 0.80
CreateBucketMethod · 0.45
PutMethod · 0.45

Tested by

no test coverage detected