File: surgeon_test.go

package info (click to toggle)
golang-github-coreos-bbolt 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,300 kB
  • sloc: makefile: 87; sh: 57
file content (57 lines) | stat: -rw-r--r-- 1,525 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package surgeon_test

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"

	bolt "go.etcd.io/bbolt"
	"go.etcd.io/bbolt/internal/btesting"
	"go.etcd.io/bbolt/internal/surgeon"
)

func TestRevertMetaPage(t *testing.T) {
	db := btesting.MustCreateDB(t)
	assert.NoError(t,
		db.Fill([]byte("data"), 1, 500,
			func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
			func(tx int, k int) []byte { return make([]byte, 100) },
		))
	assert.NoError(t,
		db.Update(
			func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte("data"))
				assert.NoError(t, b.Put([]byte("0123"), []byte("new Value for 123")))
				assert.NoError(t, b.Put([]byte("1234b"), []byte("additional object")))
				assert.NoError(t, b.Delete([]byte("0246")))
				return nil
			}))

	assert.NoError(t,
		db.View(
			func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte("data"))
				assert.Equal(t, []byte("new Value for 123"), b.Get([]byte("0123")))
				assert.Equal(t, []byte("additional object"), b.Get([]byte("1234b")))
				assert.Nil(t, b.Get([]byte("0246")))
				return nil
			}))

	db.Close()

	// This causes the whole tree to be linked to the previous state
	assert.NoError(t, surgeon.RevertMetaPage(db.Path()))

	db.MustReopen()
	db.MustCheck()
	assert.NoError(t,
		db.View(
			func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte("data"))
				assert.Equal(t, make([]byte, 100), b.Get([]byte("0123")))
				assert.Nil(t, b.Get([]byte("1234b")))
				assert.Equal(t, make([]byte, 100), b.Get([]byte("0246")))
				return nil
			}))
}