Golang implementation of the Reliable UDP transport, revised from an original documented mostly in chinese.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rudp_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package rudp
  2. import (
  3. "net"
  4. "testing"
  5. )
  6. func Test_bitShow(t *testing.T) {
  7. if show := bitShow(1023); show != "1023 b" {
  8. t.Errorf("byte show error %v,show %v", 1023, show)
  9. }
  10. if show := bitShow(1025); show != "1 Kb" {
  11. t.Errorf("byte show error %v,show %v", 1025, show)
  12. }
  13. if show := bitShow(1024*1024 + 1); show != "1 Mb" {
  14. t.Errorf("byte show error %v,show %v", 1024*1024+1, show)
  15. }
  16. }
  17. func Test_Rudp(t *testing.T) {
  18. udp := New()
  19. t1 := []byte{1, 2, 3, 4}
  20. t2 := []byte{5, 6, 7, 8}
  21. t3 := []byte{
  22. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  23. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  24. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  25. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  26. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  27. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  28. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  29. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  30. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  31. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  32. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  33. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  34. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  35. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  36. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 3,
  37. 2, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 10, 11, 12, 13,
  38. }
  39. t4 := []byte{4, 3, 2, 1}
  40. send := func(bts []byte) {
  41. n, err := udp.Send(bts)
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. if n != len(bts) {
  46. t.Errorf("send length error,send %v,realy %v", n, len(bts))
  47. }
  48. }
  49. send(t1)
  50. send(t2)
  51. if udp.Update(0) != nil {
  52. t.Errorf("update 0 return error")
  53. }
  54. pkg := udp.Update(sendDelayTick)
  55. sendLen := func(p *Package) (sz int) {
  56. for p != nil {
  57. sz += len(p.Bts)
  58. p = p.Next
  59. }
  60. return
  61. }
  62. if sendLen(pkg) != len(t1)+3+len(t2)+3 {
  63. t.Errorf("out pkg t1,t2 length error,out %v,realy %v",
  64. sendLen(pkg), len(t1)+3+len(t2)+3)
  65. }
  66. pkg = udp.Update(sendDelayTick)
  67. if pkg == nil || len(pkg.Bts) != 1 {
  68. t.Errorf("ping error,pkg %v", pkg)
  69. }
  70. send(t3)
  71. send(t4)
  72. pkg = udp.Update(sendDelayTick)
  73. if sendLen(pkg) != len(t3)+4+len(t4)+3 {
  74. t.Errorf("out pkg t3,t4 length error,out %v,realy %v",
  75. sendLen(pkg), len(t3)+4+len(t4)+3)
  76. }
  77. r1 := []byte{TYPE_REQUEST, 00, 00, 00, 00, TYPE_REQUEST, 00, 03, 00, 03}
  78. udp.Input(r1)
  79. pkg = udp.Update(sendDelayTick)
  80. if sendLen(pkg) != len(t1)+3+len(t4)+3 {
  81. t.Errorf("miss out pkg t1,t4 length error,out %v,realy %v",
  82. sendLen(pkg), len(t1)+3+len(t4)+3)
  83. }
  84. }
  85. func Test_RudpConn(t *testing.T) {
  86. addr := &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: 9981}
  87. sconn, err := net.ListenUDP("udp", addr)
  88. if err != nil {
  89. return
  90. }
  91. listener := NewListener(sconn)
  92. var send = []byte{'h', 'e', 'l', 'l', 'o'}
  93. go func() {
  94. rconn, err := listener.AcceptRudp()
  95. if err != nil {
  96. t.Error(err)
  97. return
  98. }
  99. data := make([]byte, MAX_PACKAGE)
  100. n, err := rconn.Read(data)
  101. if err != nil {
  102. t.Error(err)
  103. return
  104. }
  105. if string(data[:n]) != string(send) {
  106. t.Error(err)
  107. return
  108. }
  109. rconn.Write(send)
  110. if err := rconn.Close(); err != nil {
  111. t.Error(err)
  112. }
  113. }()
  114. raddr := net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9981}
  115. laddr := net.UDPAddr{IP: net.IPv4zero, Port: 0}
  116. cconn, err := net.DialUDP("udp", &laddr, &raddr)
  117. if err != nil {
  118. t.Error(err)
  119. return
  120. }
  121. rconn := NewConn(cconn, New())
  122. rconn.Write(send)
  123. data := make([]byte, MAX_PACKAGE)
  124. n, err := rconn.Read(data)
  125. if err != nil {
  126. return
  127. }
  128. if string(data[:n]) != string(send) {
  129. t.Error(err)
  130. return
  131. }
  132. if err := rconn.Close(); err != nil {
  133. t.Error(err)
  134. }
  135. if err := listener.Close(); err != nil {
  136. t.Error(err)
  137. }
  138. }