github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/xudp/xudp_test.go (about)

     1  package xudp
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/xmplusdev/xmcore/common"
     7  	"github.com/xmplusdev/xmcore/common/buf"
     8  	"github.com/xmplusdev/xmcore/common/net"
     9  )
    10  
    11  func TestXudpReadWrite(t *testing.T) {
    12  	addr, _ := net.ParseDestination("tcp:127.0.0.1:1345")
    13  	mb := make(buf.MultiBuffer, 0, 16)
    14  	m := buf.MultiBufferContainer{
    15  		MultiBuffer: mb,
    16  	}
    17  	var arr [8]byte
    18  	writer := NewPacketWriter(&m, addr, arr)
    19  
    20  	source := make(buf.MultiBuffer, 0, 16)
    21  	b := buf.New()
    22  	b.WriteByte('a')
    23  	b.UDP = &addr
    24  	source = append(source, b)
    25  	writer.WriteMultiBuffer(source)
    26  
    27  	reader := NewPacketReader(&m)
    28  	dest, err := reader.ReadMultiBuffer()
    29  	common.Must(err)
    30  	if dest[0].Byte(0) != 'a' {
    31  		t.Error("failed to parse xudp buffer")
    32  	}
    33  	if dest[0].UDP.Port != 1345 {
    34  		t.Error("failed to parse xudp buffer")
    35  	}
    36  }