tx_batch_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package batch
  2. import (
  3. "net/netip"
  4. "testing"
  5. )
  6. type fakeBatchWriter struct {
  7. bufs [][]byte
  8. addrs []netip.AddrPort
  9. ecns []byte
  10. }
  11. func (w *fakeBatchWriter) WriteBatch(bufs [][]byte, addrs []netip.AddrPort, ecns []byte) error {
  12. // Snapshot — SendBatch.Flush nils its slot pointers right after WriteBatch
  13. // returns, so tests must capture data before that happens.
  14. w.bufs = make([][]byte, len(bufs))
  15. for i, b := range bufs {
  16. cp := make([]byte, len(b))
  17. copy(cp, b)
  18. w.bufs[i] = cp
  19. }
  20. w.addrs = append(w.addrs[:0], addrs...)
  21. w.ecns = append(w.ecns[:0], ecns...)
  22. return nil
  23. }
  24. func TestSendBatchReserveCommitFlush(t *testing.T) {
  25. fw := &fakeBatchWriter{}
  26. b := NewSendBatch(fw, 4, 32)
  27. ap := netip.MustParseAddrPort("10.0.0.1:4242")
  28. for i := 0; i < 4; i++ {
  29. slot := b.Reserve(32)
  30. if cap(slot) != 32 {
  31. t.Fatalf("slot %d: cap=%d want 32", i, cap(slot))
  32. }
  33. pkt := append(slot[:0], byte(i), byte(i+1), byte(i+2))
  34. b.Commit(pkt, ap, 0)
  35. }
  36. if err := b.Flush(); err != nil {
  37. t.Fatalf("Flush: %v", err)
  38. }
  39. if len(fw.bufs) != 4 {
  40. t.Fatalf("WriteBatch got %d bufs want 4", len(fw.bufs))
  41. }
  42. for i, buf := range fw.bufs {
  43. if len(buf) != 3 || buf[0] != byte(i) {
  44. t.Errorf("buf %d: %x", i, buf)
  45. }
  46. if fw.addrs[i] != ap {
  47. t.Errorf("addr %d: got %v want %v", i, fw.addrs[i], ap)
  48. }
  49. }
  50. // Flush again with nothing committed — should be a no-op.
  51. fw.bufs = nil
  52. if err := b.Flush(); err != nil {
  53. t.Fatalf("empty Flush: %v", err)
  54. }
  55. if fw.bufs != nil {
  56. t.Fatalf("empty Flush triggered WriteBatch")
  57. }
  58. // Reuse after Flush.
  59. slot := b.Reserve(32)
  60. if cap(slot) != 32 {
  61. t.Fatalf("after Flush Reserve wrong cap: %d", cap(slot))
  62. }
  63. }
  64. func TestSendBatchSlotsDoNotOverlap(t *testing.T) {
  65. fw := &fakeBatchWriter{}
  66. b := NewSendBatch(fw, 3, 8)
  67. ap := netip.MustParseAddrPort("10.0.0.1:80")
  68. for i := 0; i < 3; i++ {
  69. s := b.Reserve(8)
  70. pkt := append(s[:0], byte(0xA0+i), byte(0xB0+i))
  71. b.Commit(pkt, ap, 0)
  72. }
  73. if err := b.Flush(); err != nil {
  74. t.Fatalf("Flush: %v", err)
  75. }
  76. for i, buf := range fw.bufs {
  77. if buf[0] != byte(0xA0+i) || buf[1] != byte(0xB0+i) {
  78. t.Errorf("slot %d corrupted: %x", i, buf)
  79. }
  80. }
  81. }
  82. func TestSendBatchGrowPreservesCommitted(t *testing.T) {
  83. fw := &fakeBatchWriter{}
  84. // Tiny initial backing forces a grow on the second Reserve.
  85. b := NewSendBatch(fw, 1, 4)
  86. ap := netip.MustParseAddrPort("10.0.0.1:80")
  87. s1 := b.Reserve(4)
  88. pkt1 := append(s1[:0], 0x11, 0x22, 0x33, 0x44)
  89. b.Commit(pkt1, ap, 0)
  90. s2 := b.Reserve(8) // exceeds remaining cap, triggers grow
  91. pkt2 := append(s2[:0], 0xA, 0xB, 0xC, 0xD, 0xE)
  92. b.Commit(pkt2, ap, 0)
  93. // pkt1 must still be intact even though backing reallocated.
  94. if pkt1[0] != 0x11 || pkt1[3] != 0x44 {
  95. t.Fatalf("first packet corrupted by grow: %x", pkt1)
  96. }
  97. if err := b.Flush(); err != nil {
  98. t.Fatalf("Flush: %v", err)
  99. }
  100. if len(fw.bufs) != 2 {
  101. t.Fatalf("got %d bufs want 2", len(fw.bufs))
  102. }
  103. if fw.bufs[0][0] != 0x11 || fw.bufs[0][3] != 0x44 {
  104. t.Errorf("first packet on the wire: %x", fw.bufs[0])
  105. }
  106. if fw.bufs[1][0] != 0xA || fw.bufs[1][4] != 0xE {
  107. t.Errorf("second packet on the wire: %x", fw.bufs[1])
  108. }
  109. }