tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/mux/frame/frame_test.go (about) 1 package frame 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestEncodeDecode(t *testing.T) { 9 tests := []struct { 10 in Message 11 id uint32 12 ok bool 13 }{ 14 { 15 in: CloseMessage{ 16 ChannelID: 10, 17 }, 18 id: 10, 19 ok: true, 20 }, 21 { 22 in: DataMessage{ 23 ChannelID: 10, 24 Length: 5, 25 Data: []byte("Hello"), 26 }, 27 id: 10, 28 ok: true, 29 }, 30 { 31 in: EOFMessage{ 32 ChannelID: 10, 33 }, 34 id: 10, 35 ok: true, 36 }, 37 { 38 in: OpenMessage{ 39 SenderID: 10, 40 WindowSize: 1024, 41 MaxPacketSize: 1 << 31, 42 }, 43 id: 0, 44 ok: false, 45 }, 46 { 47 in: OpenConfirmMessage{ 48 ChannelID: 20, 49 SenderID: 10, 50 WindowSize: 1024, 51 MaxPacketSize: 1 << 31, 52 }, 53 id: 20, 54 ok: true, 55 }, 56 { 57 in: OpenFailureMessage{ 58 ChannelID: 20, 59 }, 60 id: 20, 61 ok: true, 62 }, 63 { 64 in: WindowAdjustMessage{ 65 ChannelID: 20, 66 AdditionalBytes: 1024, 67 }, 68 id: 20, 69 ok: true, 70 }, 71 } 72 for _, test := range tests { 73 var buf bytes.Buffer 74 enc := NewEncoder(&buf) 75 if err := enc.Encode(test.in); err != nil { 76 t.Fatal(err) 77 } 78 dec := NewDecoder(&buf) 79 m, err := dec.Decode() 80 if err != nil { 81 t.Fatal(err) 82 } 83 id, ok := m.Channel() 84 if id != test.id { 85 t.Fatal("id not equal") 86 } 87 if ok != test.ok { 88 t.Fatal("ok not equal") 89 } 90 if m.String() == "" { 91 t.Fatal("empty string representation") 92 } 93 } 94 95 }