multiqueue_spec.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. local t = require('test.unit.testutil')
  2. local itp = t.gen_itp(it)
  3. local child_call_once = t.child_call_once
  4. local cimport = t.cimport
  5. local ffi = t.ffi
  6. local eq = t.eq
  7. local multiqueue = cimport('./test/unit/fixtures/multiqueue.h')
  8. describe('multiqueue (multi-level event-queue)', function()
  9. local parent, child1, child2, child3
  10. local function put(q, str)
  11. multiqueue.ut_multiqueue_put(q, str)
  12. end
  13. local function get(q)
  14. return ffi.string(multiqueue.ut_multiqueue_get(q))
  15. end
  16. local function free(q)
  17. multiqueue.multiqueue_free(q)
  18. end
  19. before_each(function()
  20. child_call_once(function()
  21. parent = multiqueue.multiqueue_new_parent(ffi.NULL, ffi.NULL)
  22. child1 = multiqueue.multiqueue_new_child(parent)
  23. child2 = multiqueue.multiqueue_new_child(parent)
  24. child3 = multiqueue.multiqueue_new_child(parent)
  25. put(child1, 'c1i1')
  26. put(child1, 'c1i2')
  27. put(child2, 'c2i1')
  28. put(child1, 'c1i3')
  29. put(child2, 'c2i2')
  30. put(child2, 'c2i3')
  31. put(child2, 'c2i4')
  32. put(child3, 'c3i1')
  33. put(child3, 'c3i2')
  34. end)
  35. end)
  36. itp('keeps count of added events', function()
  37. eq(3, multiqueue.multiqueue_size(child1))
  38. eq(4, multiqueue.multiqueue_size(child2))
  39. eq(2, multiqueue.multiqueue_size(child3))
  40. end)
  41. itp('keeps count of removed events', function()
  42. multiqueue.multiqueue_get(child1)
  43. eq(2, multiqueue.multiqueue_size(child1))
  44. multiqueue.multiqueue_get(child1)
  45. eq(1, multiqueue.multiqueue_size(child1))
  46. multiqueue.multiqueue_get(child1)
  47. eq(0, multiqueue.multiqueue_size(child1))
  48. put(child1, 'c2ixx')
  49. eq(1, multiqueue.multiqueue_size(child1))
  50. multiqueue.multiqueue_get(child1)
  51. eq(0, multiqueue.multiqueue_size(child1))
  52. multiqueue.multiqueue_get(child1)
  53. eq(0, multiqueue.multiqueue_size(child1))
  54. end)
  55. itp('removing from parent removes from child', function()
  56. eq('c1i1', get(parent))
  57. eq('c1i2', get(parent))
  58. eq('c2i1', get(parent))
  59. eq('c1i3', get(parent))
  60. eq('c2i2', get(parent))
  61. eq('c2i3', get(parent))
  62. eq('c2i4', get(parent))
  63. end)
  64. itp('removing from child removes from parent', function()
  65. eq('c2i1', get(child2))
  66. eq('c2i2', get(child2))
  67. eq('c1i1', get(child1))
  68. eq('c1i2', get(parent))
  69. eq('c1i3', get(parent))
  70. eq('c2i3', get(parent))
  71. eq('c2i4', get(parent))
  72. end)
  73. itp('removing from child at the beginning of parent', function()
  74. eq('c1i1', get(child1))
  75. eq('c1i2', get(child1))
  76. eq('c2i1', get(parent))
  77. end)
  78. itp('removing from parent after get from parent and put to child', function()
  79. eq('c1i1', get(parent))
  80. eq('c1i2', get(parent))
  81. eq('c2i1', get(parent))
  82. eq('c1i3', get(parent))
  83. eq('c2i2', get(parent))
  84. eq('c2i3', get(parent))
  85. eq('c2i4', get(parent))
  86. eq('c3i1', get(parent))
  87. put(child1, 'c1i11')
  88. put(child1, 'c1i22')
  89. eq('c3i2', get(parent))
  90. eq('c1i11', get(parent))
  91. eq('c1i22', get(parent))
  92. end)
  93. itp('removing from parent after get and put to child', function()
  94. eq('c1i1', get(child1))
  95. eq('c1i2', get(child1))
  96. eq('c2i1', get(child2))
  97. eq('c1i3', get(child1))
  98. eq('c2i2', get(child2))
  99. eq('c2i3', get(child2))
  100. eq('c2i4', get(child2))
  101. eq('c3i1', get(child3))
  102. eq('c3i2', get(parent))
  103. put(child1, 'c1i11')
  104. put(child2, 'c2i11')
  105. put(child1, 'c1i12')
  106. eq('c2i11', get(child2))
  107. eq('c1i11', get(parent))
  108. eq('c1i12', get(parent))
  109. end)
  110. itp('put after removing from child at the end of parent', function()
  111. eq('c3i1', get(child3))
  112. eq('c3i2', get(child3))
  113. put(child1, 'c1i11')
  114. put(child2, 'c2i11')
  115. eq('c1i1', get(parent))
  116. eq('c1i2', get(parent))
  117. eq('c2i1', get(parent))
  118. eq('c1i3', get(parent))
  119. eq('c2i2', get(parent))
  120. eq('c2i3', get(parent))
  121. eq('c2i4', get(parent))
  122. eq('c1i11', get(parent))
  123. eq('c2i11', get(parent))
  124. end)
  125. itp('removes from parent queue when child is freed', function()
  126. free(child2)
  127. eq('c1i1', get(parent))
  128. eq('c1i2', get(parent))
  129. eq('c1i3', get(parent))
  130. eq('c3i1', get(child3))
  131. eq('c3i2', get(child3))
  132. end)
  133. end)