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

Function TestBucket_Put_Single

bucket_test.go:1824–1879  ·  bucket_test.go::TestBucket_Put_Single

Ensure that a bucket can write random keys and values across multiple transactions.

(t *testing.T)

Source from the content-addressed store, hash-verified

1822
1823// Ensure that a bucket can write random keys and values across multiple transactions.
1824func TestBucket_Put_Single(t *testing.T) {
1825 if testing.Short() {
1826 t.Skip("skipping test in short mode.")
1827 }
1828
1829 index := 0
1830 if err := quick.Check(func(items testdata) bool {
1831 db := btesting.MustCreateDB(t)
1832 defer db.MustClose()
1833
1834 m := make(map[string][]byte)
1835
1836 if err := db.Update(func(tx *bolt.Tx) error {
1837 if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
1838 t.Fatal(err)
1839 }
1840 return nil
1841 }); err != nil {
1842 t.Fatal(err)
1843 }
1844
1845 for _, item := range items {
1846 if err := db.Update(func(tx *bolt.Tx) error {
1847 if err := tx.Bucket([]byte("widgets")).Put(item.Key, item.Value); err != nil {
1848 panic("put error: " + err.Error())
1849 }
1850 m[string(item.Key)] = item.Value
1851 return nil
1852 }); err != nil {
1853 t.Fatal(err)
1854 }
1855
1856 // Verify all key/values so far.
1857 if err := db.View(func(tx *bolt.Tx) error {
1858 i := 0
1859 for k, v := range m {
1860 value := tx.Bucket([]byte("widgets")).Get([]byte(k))
1861 if !bytes.Equal(value, v) {
1862 t.Logf("value mismatch [run %d] (%d of %d):\nkey: %x\ngot: %x\nexp: %x", index, i, len(m), []byte(k), value, v)
1863 db.CopyTempFile()
1864 t.FailNow()
1865 }
1866 i++
1867 }
1868 return nil
1869 }); err != nil {
1870 t.Fatal(err)
1871 }
1872 }
1873
1874 index++
1875 return true
1876 }, qconfig()); err != nil {
1877 t.Error(err)
1878 }
1879}
1880
1881// Ensure that a transaction can insert multiple key/value pairs at once.

Callers

nothing calls this directly

Calls 13

MustCreateDBFunction · 0.92
qconfigFunction · 0.85
CheckMethod · 0.80
MustCloseMethod · 0.80
UpdateMethod · 0.80
ViewMethod · 0.80
CopyTempFileMethod · 0.80
FatalMethod · 0.65
ErrorMethod · 0.65
CreateBucketMethod · 0.45
PutMethod · 0.45
BucketMethod · 0.45

Tested by

no test coverage detected