tx_batch_test.go 3.1 KB

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