fifosocktest.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. --
  2. -- Set verbose to 0 for quiet output (either the first assertion failure or
  3. -- "All tests OK"), to 1 to see the events ("SEND", "SENT", "CHECK") without
  4. -- the actual bytes, or to 2 to see the events with the bytes.
  5. --
  6. local verbose = 0
  7. local vprint = (verbose > 0) and print or function() end
  8. --
  9. -- Mock up enough of the nodemcu tmr structure, but pretend that nothing
  10. -- happens between ticks. This won't exercise the optimistic corking logic,
  11. -- but that's probably fine.
  12. -- luacheck: push ignore
  13. tmr = {}
  14. tmr.ALARM_SINGLE = 0
  15. function tmr.create()
  16. local r = {}
  17. function r:alarm(_i, _t, cb) vprint("TMR") cb() end
  18. return r
  19. end
  20. -- luacheck: pop
  21. --
  22. -- Mock up enough of the nodemcu net.socket type; have it log all the sends
  23. -- into this "outs" array so that we can later check against it.
  24. --
  25. local outs = {}
  26. local fakesock = {
  27. cb = nil,
  28. on = function(this, _, cb) this.cb = cb end,
  29. send = function(this, s) vprint("SEND", (verbose > 1) and s) table.insert(outs, s) end -- luacheck: no unused
  30. }
  31. local function sent() vprint("SENT") fakesock.cb() end
  32. -- And wrap a fifosock around this fake socket
  33. local fsend = (require "fifosock").wrap(fakesock)
  34. -- Verify that the next unconsumed output is as indicated
  35. local function fcheck(x)
  36. vprint ("CHECK", (verbose > 1) and x)
  37. assert (#outs > 0)
  38. assert (x == outs[1])
  39. table.remove(outs, 1)
  40. end
  41. -- Enqueue an empty function to prevent coalescing within the fifosock
  42. local function nocoal() fsend(function() return nil end) end
  43. -- Send and check, for when the string should be sent exactly as is
  44. local function fsendc(x) fsend(x) fcheck(x) end
  45. -- Check that there are no more outputs
  46. local function fchecke() vprint("CHECKE") assert (#outs == 0) end
  47. --
  48. -- And now for the tests, which start easy and grow in complexity
  49. --
  50. fsendc("abracadabra none")
  51. sent() ; fchecke()
  52. fsendc("abracadabra three")
  53. fsend("short")
  54. fsend("string")
  55. fsend("build")
  56. sent() ; fcheck("shortstringbuild")
  57. sent() ; fchecke()
  58. -- Hit default FSMALLLIM while building up
  59. fsendc("abracadabra lots small")
  60. for i = 1, 32 do fsend("a") end -- luacheck: no unused
  61. nocoal()
  62. for i = 1, 4 do fsend("a") end -- luacheck: no unused
  63. sent() ; fcheck(string.rep("a", 32))
  64. sent() ; fcheck(string.rep("a", 4))
  65. sent() ; fchecke()
  66. -- Hit string length while building up
  67. fsendc("abracadabra overlong")
  68. for i = 1, 10 do fsend(string.rep("a",32)) end -- luacheck: no unused
  69. sent() ; fcheck(string.rep("a", 320))
  70. sent() ; fchecke()
  71. -- Hit neither before sending a big string
  72. fsendc("abracadabra mid long")
  73. for i = 1, 6 do fsend(string.rep("a",32)) end -- luacheck: no unused
  74. fsend(string.rep("b", 256))
  75. nocoal()
  76. for i = 1, 6 do fsend(string.rep("c",32)) end -- luacheck: no unused
  77. sent() ; fcheck(string.rep("a", 192) .. string.rep("b", 256))
  78. sent() ; fcheck(string.rep("c", 192))
  79. sent() ; fchecke()
  80. -- send a huge string, verify that it coalesces
  81. fsendc(string.rep("a",256) .. string.rep("b", 256) .. string.rep("c", 260))
  82. sent() ; fchecke()
  83. -- send a huge string, verify that it coalesces save for the short bit at the end
  84. fsend(string.rep("a",256) .. string.rep("b", 256) .. string.rep("c", 256) .. string.rep("d",256))
  85. fsend("e")
  86. fcheck(string.rep("a",256) .. string.rep("b", 256) .. string.rep("c", 256))
  87. sent() ; fcheck(string.rep("d",256) .. "e")
  88. sent() ; fchecke()
  89. -- send enough that our 4x lookahead still leaves something in the queue
  90. fsend(string.rep("a",512) .. string.rep("b", 512) .. string.rep("c", 512))
  91. fcheck(string.rep("a",512) .. string.rep("b", 512))
  92. sent() ; fcheck(string.rep("c",512))
  93. sent() ; fchecke()
  94. -- test a lazy generator
  95. do
  96. local ix = 0
  97. local function gen() vprint("GEN", ix); ix = ix + 1; return ("a" .. ix), ix < 3 and gen end
  98. fsend(gen)
  99. fsend("b")
  100. fcheck("a1")
  101. sent() ; fcheck("a2")
  102. sent() ; fcheck("a3")
  103. sent() ; fcheck("b")
  104. sent() ; fchecke()
  105. end
  106. -- test a completion-like callback that does send text
  107. do
  108. local ix = 0
  109. local function gen() vprint("GEN"); ix = 1; return "efgh", nil end
  110. fsend("abcd"); fsend(gen); fsend("ijkl")
  111. assert (ix == 0)
  112. fcheck("abcd"); assert (ix == 0)
  113. sent() ; fcheck("efgh"); assert (ix == 1); ix = 0
  114. sent() ; fcheck("ijkl"); assert (ix == 0)
  115. sent() ; fchecke()
  116. end
  117. -- and one that doesn't
  118. do
  119. local ix = 0
  120. local function gen() vprint("GEN"); ix = 1; return nil, nil end
  121. fsend("abcd"); fsend(gen); fsend("ijkl")
  122. assert (ix == 0)
  123. fcheck("abcd"); assert (ix == 0)
  124. sent() ; fcheck("ijkl"); assert (ix == 1); ix = 0
  125. sent() ; fchecke() ; assert (ix == 0)
  126. end
  127. print("All tests OK")