github.com/metaworking/channeld@v0.7.3/pkg/channeld/message_test.go (about) 1 package channeld 2 3 import ( 4 "bufio" 5 "encoding/binary" 6 "testing" 7 "time" 8 9 "github.com/metaworking/channeld/pkg/channeldpb" 10 "github.com/stretchr/testify/assert" 11 "google.golang.org/protobuf/proto" 12 ) 13 14 func TestHandleListChannels(t *testing.T) { 15 InitLogs() 16 InitChannels() 17 InitConnections("../../config/server_conn_fsm_test.json", "../../config/client_non_authoratative_fsm.json") 18 19 c := addTestConnection(channeldpb.ConnectionType_SERVER) 20 ch0 := globalChannel 21 ch1, _ := CreateChannel(channeldpb.ChannelType_PRIVATE, c) 22 ch2, _ := CreateChannel(channeldpb.ChannelType_SUBWORLD, c) 23 ch3, _ := CreateChannel(channeldpb.ChannelType_SUBWORLD, c) 24 ch4, _ := CreateChannel(channeldpb.ChannelType_TEST, c) 25 ch5, _ := CreateChannel(channeldpb.ChannelType_TEST, c) 26 ch6, _ := CreateChannel(channeldpb.ChannelType_TEST, c) 27 28 ch1.metadata = "aaa" 29 ch2.metadata = "bbb" 30 ch3.metadata = "ccc" 31 ch4.metadata = "aab" 32 ch5.metadata = "bbc" 33 ch6.metadata = "abc" 34 35 handleListChannel(MessageContext{ 36 Msg: &channeldpb.ListChannelMessage{}, 37 Connection: c, 38 Channel: ch0, 39 }) 40 // No filter - all channels match 41 assert.Equal(t, 7, len(c.latestMsg().(*channeldpb.ListChannelResultMessage).Channels)) 42 43 handleListChannel(MessageContext{ 44 Msg: &channeldpb.ListChannelMessage{ 45 TypeFilter: channeldpb.ChannelType_SUBWORLD, 46 }, 47 Connection: c, 48 Channel: ch0, 49 }) 50 // 2 matches: ch2 and ch3 51 assert.Equal(t, 2, len(c.latestMsg().(*channeldpb.ListChannelResultMessage).Channels)) 52 53 handleListChannel(MessageContext{ 54 Msg: &channeldpb.ListChannelMessage{ 55 MetadataFilters: []string{"aa"}, 56 }, 57 Connection: c, 58 Channel: ch0, 59 }) 60 // 2 matches: ch1 and ch4 61 assert.Equal(t, 2, len(c.latestMsg().(*channeldpb.ListChannelResultMessage).Channels)) 62 63 handleListChannel(MessageContext{ 64 Msg: &channeldpb.ListChannelMessage{ 65 MetadataFilters: []string{"bb", "cc"}, 66 }, 67 Connection: c, 68 Channel: ch0, 69 }) 70 // 3 matches: ch2, ch3, ch5 71 assert.Equal(t, 3, len(c.latestMsg().(*channeldpb.ListChannelResultMessage).Channels)) 72 73 handleListChannel(MessageContext{ 74 Msg: &channeldpb.ListChannelMessage{ 75 TypeFilter: channeldpb.ChannelType_TEST, 76 MetadataFilters: []string{"a", "b", "c"}, 77 }, 78 Connection: c, 79 Channel: ch0, 80 }) 81 // 3 matches: ch4, ch5, ch6 82 assert.Equal(t, 3, len(c.latestMsg().(*channeldpb.ListChannelResultMessage).Channels)) 83 84 handleListChannel(MessageContext{ 85 Msg: &channeldpb.ListChannelMessage{ 86 MetadataFilters: []string{"z"}, 87 }, 88 Connection: c, 89 Channel: ch0, 90 }) 91 // no match 92 assert.Equal(t, 0, len(c.latestMsg().(*channeldpb.ListChannelResultMessage).Channels)) 93 } 94 95 func TestMessageHandlers(t *testing.T) { 96 for name, value := range channeldpb.MessageType_value { 97 switch msgType := channeldpb.MessageType(value); { 98 case msgType == channeldpb.MessageType_INVALID: 99 case msgType == channeldpb.MessageType_CHANNEL_DATA_HANDOVER: 100 case msgType == channeldpb.MessageType_SPATIAL_REGIONS_UPDATE: 101 case value >= int32(channeldpb.MessageType_USER_SPACE_START): 102 continue 103 default: 104 assert.NotNil(t, MessageMap[msgType], "Missing handler func for message type %s", name) 105 } 106 } 107 } 108 109 func TestMessageTypeConversion(t *testing.T) { 110 var n uint32 = 1 111 msgType1 := channeldpb.MessageType(n) 112 assert.Equal(t, channeldpb.MessageType_AUTH, msgType1) 113 msgType2 := channeldpb.MessageType(uint32(999)) 114 t.Log(msgType2) 115 _, exists := channeldpb.MessageType_name[int32(msgType2)] 116 assert.False(t, exists) 117 } 118 119 func BenchmarkProtobufMessagePack(b *testing.B) { 120 mp := &channeldpb.MessagePack{ 121 Broadcast: uint32(channeldpb.BroadcastType_ALL), 122 StubId: 0, 123 MsgType: 8, 124 //BodySize: 0, 125 MsgBody: []byte{}, 126 } 127 128 var size int = 0 129 for i := 0; i < b.N; i++ { 130 // randomize the channel id between [0, 100) 131 mp.ChannelId = uint32(time.Now().Nanosecond() % 100) 132 bytes, _ := proto.Marshal(mp) 133 size += len(bytes) 134 } 135 b.Logf("Average packet size: %.2f", float64(size)/float64(b.N)) 136 // Result: 137 // BenchmarkProtobufPacket-12 9602565 127.8 ns/op 4 B/op 1 allocs/op 138 // Average packet size: 4.00 139 } 140 141 type mockWriter struct{} 142 143 func (w *mockWriter) Write(p []byte) (n int, err error) { 144 return 0, nil 145 } 146 func BenchmarkRawMessagePack(b *testing.B) { 147 mw := &mockWriter{} 148 w := bufio.NewWriterSize(mw, 20) 149 var size int = 0 150 for i := 0; i < b.N; i++ { 151 binary.Write(w, binary.BigEndian, uint32(0)) 152 binary.Write(w, binary.BigEndian, byte(1)) 153 binary.Write(w, binary.BigEndian, uint32(0)) 154 binary.Write(w, binary.BigEndian, uint32(8)) 155 binary.Write(w, binary.BigEndian, uint32(0)) 156 binary.Write(w, binary.BigEndian, []byte{}) 157 size += w.Buffered() 158 w.Reset(mw) 159 } 160 b.Logf("Average buf size: %.2f", float64(size)/float64(b.N)) 161 // Result: 162 // BenchmarkRawPacket-12 4974171 239.3 ns/op 44 B/op 6 allocs/op 163 // Average buf size: 17.00 164 } 165 166 func TestMessageCopy(t *testing.T) { 167 msg := MessageMap[channeldpb.MessageType_CREATE_CHANNEL].msg 168 msgCopy := proto.Clone(msg).(*channeldpb.CreateChannelMessage) 169 assert.False(t, msg == msgCopy) 170 171 createChannelMsg := &channeldpb.CreateChannelMessage{} 172 assert.IsType(t, msg, createChannelMsg) 173 assert.IsType(t, msgCopy, createChannelMsg) 174 175 msgCopy.ChannelType = channeldpb.ChannelType_GLOBAL 176 proto.Reset(msg) 177 assert.Equal(t, channeldpb.ChannelType_GLOBAL, msgCopy.ChannelType) 178 proto.Reset(msgCopy) 179 assert.Equal(t, channeldpb.ChannelType_UNKNOWN, msgCopy.ChannelType) 180 }