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

Function TestDB_WriteTo_and_Overwrite

db_test.go:800–903  ·  db_test.go::TestDB_WriteTo_and_Overwrite

TestDB_WriteTo_and_Overwrite verifies that `(tx *Tx) WriteTo` can still work even the underlying file is overwritten between the time a read-only transaction is created and the time the file is actually opened

(t *testing.T)

Source from the content-addressed store, hash-verified

798// work even the underlying file is overwritten between the time a read-only
799// transaction is created and the time the file is actually opened
800func TestDB_WriteTo_and_Overwrite(t *testing.T) {
801 testCases := []struct {
802 name string
803 writeFlag int
804 }{
805 {
806 name: "writeFlag not set",
807 writeFlag: 0,
808 },
809 /* syscall.O_DIRECT not supported on some platforms, i.e. Windows and MacOS
810 {
811 name: "writeFlag set",
812 writeFlag: syscall.O_DIRECT,
813 },*/
814 }
815
816 fRead := func(db *bolt.DB, bucketName []byte) map[string]string {
817 data := make(map[string]string)
818 _ = db.View(func(tx *bolt.Tx) error {
819 b := tx.Bucket(bucketName)
820 berr := b.ForEach(func(k, v []byte) error {
821 data[string(k)] = string(v)
822 return nil
823 })
824 require.NoError(t, berr)
825 return nil
826 })
827 return data
828 }
829
830 for _, tc := range testCases {
831 t.Run(tc.name, func(t *testing.T) {
832 db := btesting.MustCreateDBWithOption(t, &bolt.Options{
833 PageSize: 4096,
834 })
835 filePathOfDb := db.Path()
836
837 var (
838 bucketName = []byte("data")
839 dataExpected map[string]string
840 dataActual map[string]string
841 )
842
843 t.Log("Populate some data")
844 err := db.Update(func(tx *bolt.Tx) error {
845 b, berr := tx.CreateBucket(bucketName)
846 if berr != nil {
847 return berr
848 }
849 for k := 0; k < 10; k++ {
850 key, value := fmt.Sprintf("key_%d", rand.Intn(10)), fmt.Sprintf("value_%d", rand.Intn(100))
851 if perr := b.Put([]byte(key), []byte(value)); perr != nil {
852 return perr
853 }
854 }
855 return nil
856 })
857 require.NoError(t, err)

Callers

nothing calls this directly

Calls 14

MustCreateDBWithOptionFunction · 0.92
ViewMethod · 0.80
RunMethod · 0.80
UpdateMethod · 0.80
BeginMethod · 0.80
MustCloseMethod · 0.80
CopyFileMethod · 0.80
RollbackMethod · 0.65
BucketMethod · 0.45
ForEachMethod · 0.45
PathMethod · 0.45
CreateBucketMethod · 0.45

Tested by

no test coverage detected