@@ -84,6 +84,101 @@ describe("channel", function()
8484 end )
8585 end )
8686
87+ describe (" mpsc" , function ()
88+ a .it (" should wait multiple recv before any send" , function ()
89+ local sender , receiver = channel .mpsc ()
90+
91+ local expected_count = 10
92+
93+ a .run (function ()
94+ for i = 1 , expected_count do
95+ a .util .sleep (250 )
96+ sender .send (i )
97+ end
98+ end )
99+
100+ local receive_count = 0
101+ while receive_count < expected_count do
102+ receive_count = receive_count + 1
103+ local i = receiver .recv ()
104+ eq (receive_count , i )
105+ end
106+ end )
107+
108+ a .it (" should queues multiple sends before any read" , function ()
109+ local sender , receiver = channel .mpsc ()
110+
111+ local counter = 0
112+
113+ a .run (function ()
114+ counter = counter + 1
115+ sender .send (10 )
116+
117+ counter = counter + 1
118+ sender .send (20 )
119+ end )
120+
121+ a .util .sleep (1000 )
122+
123+ eq (10 , receiver .recv ())
124+ eq (20 , receiver .recv ())
125+ eq (2 , counter )
126+ end )
127+
128+ a .it (" should queues multiple sends from multiple producers before any read" , function ()
129+ local sender , receiver = channel .mpsc ()
130+
131+ local counter = 0
132+
133+ a .run (function ()
134+ counter = counter + 1
135+ sender .send (10 )
136+
137+ counter = counter + 1
138+ sender .send (20 )
139+ end )
140+
141+ a .run (function ()
142+ counter = counter + 1
143+ sender .send (30 )
144+
145+ counter = counter + 1
146+ sender .send (40 )
147+ end )
148+
149+ a .util .sleep (1000 )
150+
151+ local read_counter = 0
152+ a .util .block_on (function ()
153+ for _ = 1 , 4 do
154+ receiver .recv ()
155+ read_counter = read_counter + 1
156+ end
157+ end , 1000 )
158+ eq (4 , counter )
159+ eq (4 , read_counter )
160+ end )
161+
162+ a .it (" should read only the last value" , function ()
163+ local sender , receiver = channel .mpsc ()
164+
165+ local counter = 0
166+
167+ a .run (function ()
168+ counter = counter + 1
169+ sender .send (10 )
170+
171+ counter = counter + 1
172+ sender .send (20 )
173+ end )
174+
175+ a .util .sleep (1000 )
176+
177+ eq (20 , receiver .last ())
178+ eq (2 , counter )
179+ end )
180+ end )
181+
87182 describe (" counter" , function ()
88183 a .it (" should work" , function ()
89184 local tx , rx = channel .counter ()
0 commit comments