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

Function TestBucket_Get_Capacity

bucket_test.go:89–123  ·  view source on GitHub ↗

Ensure that a slice returned from a bucket has a capacity equal to its length. This also allows slices to be appended to since it will require a realloc by Go. https://github.com/boltdb/bolt/issues/544

(t *testing.T)

Source from the content-addressed store, hash-verified

87//
88// https://github.com/boltdb/bolt/issues/544
89func TestBucket_Get_Capacity(t *testing.T) {
90 db := btesting.MustCreateDB(t)
91
92 // Write key to a bucket.
93 if err := db.Update(func(tx *bolt.Tx) error {
94 b, err := tx.CreateBucket([]byte("bucket"))
95 if err != nil {
96 return err
97 }
98 return b.Put([]byte("key"), []byte("val"))
99 }); err != nil {
100 t.Fatal(err)
101 }
102
103 // Retrieve value and attempt to append to it.
104 if err := db.Update(func(tx *bolt.Tx) error {
105 k, v := tx.Bucket([]byte("bucket")).Cursor().First()
106
107 // Verify capacity.
108 if len(k) != cap(k) {
109 t.Fatalf("unexpected key slice capacity: %d", cap(k))
110 } else if len(v) != cap(v) {
111 t.Fatalf("unexpected value slice capacity: %d", cap(v))
112 }
113
114 // Ensure slice can be appended to without a segfault.
115 k = append(k, []byte("123")...)
116 v = append(v, []byte("123")...)
117 _, _ = k, v // to pass ineffassign
118
119 return nil
120 }); err != nil {
121 t.Fatal(err)
122 }
123}
124
125// Ensure that a bucket can write a key/value.
126func TestBucket_Put(t *testing.T) {

Callers

nothing calls this directly

Calls 9

MustCreateDBFunction · 0.92
UpdateMethod · 0.80
FirstMethod · 0.80
FatalMethod · 0.65
FatalfMethod · 0.65
CreateBucketMethod · 0.45
PutMethod · 0.45
CursorMethod · 0.45
BucketMethod · 0.45

Tested by

no test coverage detected