github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitree/bdb/unix_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package bdb_test 19 20 import ( 21 "fmt" 22 "testing" 23 24 "github.com/zuoyebang/bitalosdb/bitree/bdb" 25 "github.com/zuoyebang/bitalosdb/internal/options" 26 27 "golang.org/x/sys/unix" 28 ) 29 30 func TestMlock_DbOpen(t *testing.T) { 31 skipOnMemlockLimitBelow(t, 32*1024) 32 33 db := MustOpenWithOption(&options.BdbOptions{Mlock: true}) 34 defer db.MustClose() 35 } 36 37 func TestMlock_DbCanGrow_Small(t *testing.T) { 38 skipOnMemlockLimitBelow(t, 32*1024) 39 40 db := MustOpenWithOption(&options.BdbOptions{Mlock: true}) 41 defer db.MustClose() 42 43 if err := db.Update(func(tx *bdb.Tx) error { 44 b, err := tx.CreateBucketIfNotExists([]byte("bucket")) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 key := []byte("key") 50 value := []byte("value") 51 if err := b.Put(key, value); err != nil { 52 t.Fatal(err) 53 } 54 55 return nil 56 }); err != nil { 57 t.Fatal(err) 58 } 59 60 } 61 62 func TestMlock_DbCanGrow_Big(t *testing.T) { 63 if testing.Short() { 64 t.Skip("skipping test in short mode") 65 } 66 67 skipOnMemlockLimitBelow(t, 32*1024*1024) 68 69 chunksBefore := 64 70 chunksAfter := 64 71 72 db := MustOpenWithOption(&options.BdbOptions{Mlock: true}) 73 defer db.MustClose() 74 75 for chunk := 0; chunk < chunksBefore; chunk++ { 76 insertChunk(t, db, chunk) 77 } 78 dbSize := fileSize(db.f) 79 80 for chunk := 0; chunk < chunksAfter; chunk++ { 81 insertChunk(t, db, chunksBefore+chunk) 82 } 83 newDbSize := fileSize(db.f) 84 85 if newDbSize <= dbSize { 86 t.Errorf("db didn't grow: %v <= %v", newDbSize, dbSize) 87 } 88 } 89 90 func insertChunk(t *testing.T, db *DB, chunkId int) { 91 chunkSize := 1024 92 93 if err := db.Update(func(tx *bdb.Tx) error { 94 b, err := tx.CreateBucketIfNotExists([]byte("bucket")) 95 if err != nil { 96 t.Fatal(err) 97 } 98 99 for i := 0; i < chunkSize; i++ { 100 key := []byte(fmt.Sprintf("key-%d-%d", chunkId, i)) 101 value := []byte("value") 102 if err := b.Put(key, value); err != nil { 103 t.Fatal(err) 104 } 105 } 106 107 return nil 108 }); err != nil { 109 t.Fatal(err) 110 } 111 } 112 113 func skipOnMemlockLimitBelow(t *testing.T, memlockLimitRequest uint64) { 114 var info unix.Rlimit 115 if err := unix.Getrlimit(unix.RLIMIT_MEMLOCK, &info); err != nil { 116 t.Fatal(err) 117 } 118 119 if info.Cur < memlockLimitRequest { 120 t.Skip(fmt.Sprintf( 121 "skipping as RLIMIT_MEMLOCK is unsufficient: %v < %v", 122 info.Cur, 123 memlockLimitRequest, 124 )) 125 } 126 }