robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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