File: unix_test.go

package info (click to toggle)
golang-github-coreos-bbolt 1.3.10-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 912 kB
  • sloc: makefile: 54; sh: 7
file content (116 lines) | stat: -rw-r--r-- 2,446 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//go:build !windows
// +build !windows

package bbolt_test

import (
	"fmt"
	"testing"

	"golang.org/x/sys/unix"

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

func TestMlock_DbOpen(t *testing.T) {
	// 32KB
	skipOnMemlockLimitBelow(t, 32*1024)

	btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true})
}

// Test change between "empty" (16KB) and "non-empty" db
func TestMlock_DbCanGrow_Small(t *testing.T) {
	// 32KB
	skipOnMemlockLimitBelow(t, 32*1024)

	db := btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true})

	if err := db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte("bucket"))
		if err != nil {
			t.Fatal(err)
		}

		key := []byte("key")
		value := []byte("value")
		if err := b.Put(key, value); err != nil {
			t.Fatal(err)
		}

		return nil
	}); err != nil {
		t.Fatal(err)
	}

}

// Test crossing of 16MB (AllocSize) of db size
func TestMlock_DbCanGrow_Big(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode")
	}

	// 32MB
	skipOnMemlockLimitBelow(t, 32*1024*1024)

	chunksBefore := 64
	chunksAfter := 64

	db := btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true})

	for chunk := 0; chunk < chunksBefore; chunk++ {
		insertChunk(t, db, chunk)
	}
	dbSize := fileSize(db.Path())

	for chunk := 0; chunk < chunksAfter; chunk++ {
		insertChunk(t, db, chunksBefore+chunk)
	}
	newDbSize := fileSize(db.Path())

	if newDbSize <= dbSize {
		t.Errorf("db didn't grow: %v <= %v", newDbSize, dbSize)
	}
}

func insertChunk(t *testing.T, db *btesting.DB, chunkId int) {
	chunkSize := 1024

	if err := db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte("bucket"))
		if err != nil {
			t.Fatal(err)
		}

		for i := 0; i < chunkSize; i++ {
			key := []byte(fmt.Sprintf("key-%d-%d", chunkId, i))
			value := []byte("value")
			if err := b.Put(key, value); err != nil {
				t.Fatal(err)
			}
		}

		return nil
	}); err != nil {
		t.Fatal(err)
	}
}

// Main reason for this check is travis limiting mlockable memory to 64KB
// https://github.com/travis-ci/travis-ci/issues/2462
func skipOnMemlockLimitBelow(t *testing.T, memlockLimitRequest uint64) {
	var info unix.Rlimit
	if err := unix.Getrlimit(unix.RLIMIT_MEMLOCK, &info); err != nil {
		t.Fatal(err)
	}

	if info.Cur < memlockLimitRequest {
		t.Skipf(
			"skipping as RLIMIT_MEMLOCK is insufficient: %v < %v",
			info.Cur,
			memlockLimitRequest,
		)
	}
}