NTest_tmr.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. local N = ...
  2. N = (N or require "NTest")("tmr")
  3. N.testasync('SINGLE alarm', function(next)
  4. local t = tmr.create();
  5. local count = 0
  6. t:alarm(200, tmr.ALARM_SINGLE,
  7. function()
  8. count = count + 1
  9. ok(count <= 1, "only 1 invocation")
  10. next()
  11. end)
  12. ok(true, "sync end")
  13. end)
  14. N.testasync('SEMI alarm', function(next)
  15. local t = tmr.create();
  16. local count = 0
  17. t:alarm(200, tmr.ALARM_SEMI,
  18. function(tp)
  19. count = count + 1
  20. if count <= 1 then
  21. tp:start()
  22. return
  23. end
  24. ok(eq(count, 2), "only 2 invocations")
  25. next()
  26. end)
  27. ok(true, "sync end")
  28. end)
  29. N.testasync('AUTO alarm', function(next)
  30. local t = tmr.create();
  31. local count = 0
  32. t:alarm(200, tmr.ALARM_AUTO,
  33. function(tp)
  34. count = count + 1
  35. if count == 2 then
  36. tp:stop()
  37. return next()
  38. end
  39. ok(count < 2, "only 2 invocations")
  40. end)
  41. ok(true, "sync end")
  42. end)
  43. N.testco('SINGLE alarm coroutine', function(getCB, waitCB)
  44. local t = tmr.create();
  45. t:alarm(200, tmr.ALARM_SINGLE, getCB("timer"))
  46. local name, timer = waitCB()
  47. ok(eq("timer", name), "CB name matches")
  48. ok(eq(t, timer), "CB tmr instance matches")
  49. ok(true, "coroutine end")
  50. end)
  51. N.testco('SEMI alarm coroutine', function(getCB, waitCB)
  52. local t = tmr.create();
  53. t:alarm(200, tmr.ALARM_SEMI, getCB("timer"))
  54. local name, timer = waitCB()
  55. ok(eq("timer", name), "CB name matches")
  56. ok(eq(t, timer), "CB tmr instance matches")
  57. timer:start()
  58. name, timer = waitCB()
  59. ok(eq("timer", name), "CB name matches again")
  60. ok(eq(t, timer), "CB tmr instance matches again")
  61. ok(true, "coroutine end")
  62. end)
  63. N.testco('AUTO alarm coroutine', function(getCB, waitCB)
  64. local t = tmr.create();
  65. t:alarm(200, tmr.ALARM_AUTO, getCB("timer"))
  66. local name, timer = waitCB()
  67. ok(eq("timer", name), "CB name matches")
  68. ok(eq(t, timer), "CB tmr instance matches")
  69. name, timer = waitCB()
  70. ok(eq("timer", name), "CB name matches again")
  71. ok(eq(t, timer), "CB tmr instance matches again")
  72. timer:stop()
  73. ok(true, "coroutine end")
  74. end)
  75. N.test('softwd set positive and negative values', function()
  76. tmr.softwd(22)
  77. tmr.softwd(0)
  78. tmr.softwd(-1) -- disable it again
  79. tmr.softwd(-22) -- disable it again
  80. end)