fifosocktest.lua 4.2 KB

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