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)
| 87 | // |
| 88 | // https://github.com/boltdb/bolt/issues/544 |
| 89 | func 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. |
| 126 | func TestBucket_Put(t *testing.T) { |
nothing calls this directly
no test coverage detected