github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/objstorage/objstorageprovider/remoteobjcat/version_edit_test.go (about) 1 // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package remoteobjcat 6 7 import ( 8 "bytes" 9 "strings" 10 "testing" 11 12 "github.com/cockroachdb/errors" 13 "github.com/cockroachdb/pebble/internal/base" 14 "github.com/cockroachdb/pebble/objstorage" 15 "github.com/kr/pretty" 16 ) 17 18 func TestVersionEditRoundTrip(t *testing.T) { 19 for _, ve := range []VersionEdit{ 20 {}, 21 { 22 CreatorID: 12345, 23 }, 24 { 25 NewObjects: []RemoteObjectMetadata{ 26 { 27 FileNum: base.FileNum(1).DiskFileNum(), 28 FileType: base.FileTypeTable, 29 CreatorID: 12, 30 CreatorFileNum: base.FileNum(123).DiskFileNum(), 31 CleanupMethod: objstorage.SharedNoCleanup, 32 Locator: "", 33 CustomObjectName: "foo", 34 }, 35 }, 36 }, 37 { 38 DeletedObjects: []base.DiskFileNum{base.FileNum(1).DiskFileNum()}, 39 }, 40 { 41 CreatorID: 12345, 42 NewObjects: []RemoteObjectMetadata{ 43 { 44 FileNum: base.FileNum(1).DiskFileNum(), 45 FileType: base.FileTypeTable, 46 CreatorID: 12, 47 CreatorFileNum: base.FileNum(123).DiskFileNum(), 48 CleanupMethod: objstorage.SharedRefTracking, 49 Locator: "foo", 50 CustomObjectName: "", 51 }, 52 { 53 FileNum: base.FileNum(2).DiskFileNum(), 54 FileType: base.FileTypeTable, 55 CreatorID: 22, 56 CreatorFileNum: base.FileNum(223).DiskFileNum(), 57 Locator: "bar", 58 CustomObjectName: "obj1", 59 }, 60 { 61 FileNum: base.FileNum(3).DiskFileNum(), 62 FileType: base.FileTypeTable, 63 CreatorID: 32, 64 CreatorFileNum: base.FileNum(323).DiskFileNum(), 65 CleanupMethod: objstorage.SharedRefTracking, 66 Locator: "baz", 67 CustomObjectName: "obj2", 68 }, 69 }, 70 DeletedObjects: []base.DiskFileNum{base.FileNum(4).DiskFileNum(), base.FileNum(5).DiskFileNum()}, 71 }, 72 } { 73 if err := checkRoundTrip(ve); err != nil { 74 t.Fatalf("%+v did not roundtrip: %v", ve, err) 75 } 76 } 77 } 78 79 func checkRoundTrip(e0 VersionEdit) error { 80 var e1 VersionEdit 81 buf := new(bytes.Buffer) 82 if err := e0.Encode(buf); err != nil { 83 return errors.Wrap(err, "encode") 84 } 85 if err := e1.Decode(buf); err != nil { 86 return errors.Wrap(err, "decode") 87 } 88 if diff := pretty.Diff(e0, e1); diff != nil { 89 return errors.Errorf("%s", strings.Join(diff, "\n")) 90 } 91 return nil 92 }