conn_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package udptransfer
  2. import (
  3. "math/rand"
  4. "sort"
  5. "testing"
  6. )
  7. var conn *Conn
  8. func init() {
  9. conn = &Conn{
  10. outQ: newLinkedMap(_QModeOut),
  11. inQ: newLinkedMap(_QModeIn),
  12. }
  13. }
  14. func assert(cond bool, t testing.TB, format string, args ...interface{}) {
  15. if !cond {
  16. t.Errorf(format, args...)
  17. panic("last error")
  18. }
  19. }
  20. func Test_ordered_insert(t *testing.T) {
  21. data := []byte{1}
  22. var pk *packet
  23. for i := int32(1); i < 33; i++ {
  24. pk = &packet{
  25. seq: uint32(i),
  26. payload: data,
  27. }
  28. conn.insertData(pk)
  29. assert(conn.inQ.size() == i, t, "len inQ=%d", conn.inQ.size())
  30. assert(conn.inQ.maxCtnSeq == pk.seq, t, "lastCtnIn")
  31. assert(!conn.inQDirty, t, "dirty")
  32. }
  33. }
  34. func Test_unordered_insert(t *testing.T) {
  35. conn.inQ.reset()
  36. data := []byte{1}
  37. var pk *packet
  38. var seqs = make([]int, 0xfff)
  39. // unordered insert, and assert size
  40. for i := 1; i < len(seqs); i++ {
  41. var seq uint32
  42. for conn.inQ.contains(seq) || seq == 0 {
  43. seq = uint32(rand.Int31n(0xFFffff))
  44. }
  45. seqs[i] = int(seq)
  46. pk = &packet{
  47. seq: seq,
  48. payload: data,
  49. }
  50. conn.insertData(pk)
  51. assert(conn.inQ.size() == int32(i), t, "i=%d inQ.len=%d", i, conn.inQ.size())
  52. }
  53. // assert lastCtnSeq
  54. sort.Ints(seqs)
  55. var zero = 0
  56. var last *int
  57. for i := 0; i < len(seqs); i++ {
  58. if i == 0 && seqs[0] != 0 {
  59. last = &zero
  60. break
  61. }
  62. if last != nil && seqs[i]-*last > 1 {
  63. if i == 1 {
  64. last = &zero
  65. }
  66. break
  67. }
  68. last = &seqs[i]
  69. }
  70. if *last != int(conn.inQ.maxCtnSeq) {
  71. for i, j := range seqs {
  72. if i < 10 {
  73. t.Logf("seq %d", j)
  74. }
  75. }
  76. }
  77. assert(*last == int(conn.inQ.maxCtnSeq), t, "lastCtnSeq=%d but expected=%d", conn.inQ.maxCtnSeq, *last)
  78. t.Logf("lastCtnSeq=%d dirty=%v", conn.inQ.maxCtnSeq, conn.inQDirty)
  79. }