github.com/koko1123/flow-go-1@v0.29.6/model/flow/service_event_test.go (about) 1 package flow_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/fxamacker/cbor/v2" 8 "github.com/google/go-cmp/cmp" 9 "github.com/stretchr/testify/require" 10 "github.com/vmihailenco/msgpack" 11 "gotest.tools/assert" 12 13 cborcodec "github.com/koko1123/flow-go-1/model/encoding/cbor" 14 "github.com/koko1123/flow-go-1/model/flow" 15 "github.com/koko1123/flow-go-1/utils/unittest" 16 "github.com/onflow/flow-go/crypto" 17 ) 18 19 func TestEncodeDecode(t *testing.T) { 20 21 setup := unittest.EpochSetupFixture() 22 commit := unittest.EpochCommitFixture() 23 24 comparePubKey := cmp.FilterValues(func(a, b crypto.PublicKey) bool { 25 return true 26 }, cmp.Comparer(func(a, b crypto.PublicKey) bool { 27 if a == nil { 28 return b == nil 29 } 30 return a.Equals(b) 31 })) 32 33 t.Run("json", func(t *testing.T) { 34 t.Run("specific event types", func(t *testing.T) { 35 b, err := json.Marshal(setup) 36 require.NoError(t, err) 37 38 gotSetup := new(flow.EpochSetup) 39 err = json.Unmarshal(b, gotSetup) 40 require.NoError(t, err) 41 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 42 43 b, err = json.Marshal(commit) 44 require.NoError(t, err) 45 46 gotCommit := new(flow.EpochCommit) 47 err = json.Unmarshal(b, gotCommit) 48 require.NoError(t, err) 49 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 50 }) 51 52 t.Run("generic type", func(t *testing.T) { 53 b, err := json.Marshal(setup.ServiceEvent()) 54 require.NoError(t, err) 55 56 outer := new(flow.ServiceEvent) 57 err = json.Unmarshal(b, outer) 58 require.NoError(t, err) 59 gotSetup, ok := outer.Event.(*flow.EpochSetup) 60 require.True(t, ok) 61 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 62 63 t.Logf("- debug: setup.ServiceEvent()=%+v\n", setup.ServiceEvent()) 64 b, err = json.Marshal(commit.ServiceEvent()) 65 require.NoError(t, err) 66 67 outer = new(flow.ServiceEvent) 68 t.Logf("- debug: outer=%+v <-- before .Unmarshal()\n", outer) 69 err = json.Unmarshal(b, outer) 70 t.Logf("- debug: outer=%+v <-- after .Unmarshal()\n", outer) 71 require.NoError(t, err) 72 gotCommit, ok := outer.Event.(*flow.EpochCommit) 73 require.True(t, ok) 74 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 75 }) 76 }) 77 78 t.Run("msgpack", func(t *testing.T) { 79 t.Run("specific event types", func(t *testing.T) { 80 b, err := msgpack.Marshal(setup) 81 require.NoError(t, err) 82 83 gotSetup := new(flow.EpochSetup) 84 err = msgpack.Unmarshal(b, gotSetup) 85 require.NoError(t, err) 86 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 87 88 b, err = msgpack.Marshal(commit) 89 require.NoError(t, err) 90 91 gotCommit := new(flow.EpochCommit) 92 err = msgpack.Unmarshal(b, gotCommit) 93 require.NoError(t, err) 94 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 95 }) 96 97 t.Run("generic type", func(t *testing.T) { 98 b, err := msgpack.Marshal(setup.ServiceEvent()) 99 require.NoError(t, err) 100 101 outer := new(flow.ServiceEvent) 102 err = msgpack.Unmarshal(b, outer) 103 require.NoError(t, err) 104 gotSetup, ok := outer.Event.(*flow.EpochSetup) 105 require.True(t, ok) 106 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 107 108 t.Logf("- debug: setup.ServiceEvent()=%+v\n", setup.ServiceEvent()) 109 b, err = msgpack.Marshal(commit.ServiceEvent()) 110 require.NoError(t, err) 111 112 outer = new(flow.ServiceEvent) 113 t.Logf("- debug: outer=%+v <-- before .Unmarshal()\n", outer) 114 err = msgpack.Unmarshal(b, outer) 115 t.Logf("- debug: outer=%+v <-- after .Unmarshal()\n", outer) 116 require.NoError(t, err) 117 gotCommit, ok := outer.Event.(*flow.EpochCommit) 118 require.True(t, ok) 119 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 120 }) 121 }) 122 123 t.Run("cbor", func(t *testing.T) { 124 t.Run("specific event types", func(t *testing.T) { 125 b, err := cborcodec.EncMode.Marshal(setup) 126 require.NoError(t, err) 127 128 gotSetup := new(flow.EpochSetup) 129 err = cbor.Unmarshal(b, gotSetup) 130 require.NoError(t, err) 131 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 132 133 b, err = cborcodec.EncMode.Marshal(commit) 134 require.NoError(t, err) 135 136 gotCommit := new(flow.EpochCommit) 137 err = cbor.Unmarshal(b, gotCommit) 138 require.NoError(t, err) 139 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 140 }) 141 142 t.Run("generic type", func(t *testing.T) { 143 t.Logf("- debug: setup.ServiceEvent()=%+v\n", setup.ServiceEvent()) 144 b, err := cborcodec.EncMode.Marshal(setup.ServiceEvent()) 145 require.NoError(t, err) 146 147 outer := new(flow.ServiceEvent) 148 t.Logf("- debug: outer=%+v <-- before .Unmarshal()\n", outer) 149 err = cbor.Unmarshal(b, outer) 150 t.Logf("- debug: outer=%+v <-- after .Unmarshal()\n", outer) 151 require.NoError(t, err) 152 gotSetup, ok := outer.Event.(*flow.EpochSetup) 153 require.True(t, ok) 154 assert.DeepEqual(t, setup, gotSetup, comparePubKey) 155 156 b, err = cborcodec.EncMode.Marshal(commit.ServiceEvent()) 157 require.NoError(t, err) 158 159 outer = new(flow.ServiceEvent) 160 err = cbor.Unmarshal(b, outer) 161 require.NoError(t, err) 162 gotCommit, ok := outer.Event.(*flow.EpochCommit) 163 require.True(t, ok) 164 assert.DeepEqual(t, commit, gotCommit, comparePubKey) 165 }) 166 }) 167 }