tx_batch_test.go 3.0 KB

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