Comparison

spec/util_async_spec.lua @ 8622:92fee8a6c988

util.async: Trim trailing whitespace in tests [luacheck]
author Kim Alvefur <zash@zash.se>
date Sat, 17 Mar 2018 23:00:04 +0100
parent 8618:680b1caa2dea
child 8623:ab242c513bf4
comparison
equal deleted inserted replaced
8621:e3e9479d526e 8622:92fee8a6c988
19 end; 19 end;
20 end; 20 end;
21 })), log; 21 })), log;
22 end 22 end
23 describe("#runner", function() 23 describe("#runner", function()
24 it("should work", function() 24 it("should work", function()
25 local r, l = new(function (item) assert(type(item) == "number") end); 25 local r, l = new(function (item) assert(type(item) == "number") end);
26 r:run(1); 26 r:run(1);
27 r:run(2); 27 r:run(2);
28 end); 28 end);
29 29
244 async.waiter(); 244 async.waiter();
245 end); 245 end);
246 end); 246 end);
247 it("should work", function () 247 it("should work", function ()
248 local wait, done; 248 local wait, done;
249 249
250 local r, l = new(function (item) 250 local r, l = new(function (item)
251 assert(type(item) == "number") 251 assert(type(item) == "number")
252 if item == 3 then 252 if item == 3 then
253 wait, done = async.waiter(); 253 wait, done = async.waiter();
254 wait(); 254 wait();
255 end 255 end
256 end); 256 end);
257 257
258 r:run(1); 258 r:run(1);
259 assert(r.state == "ready"); 259 assert(r.state == "ready");
260 r:run(2); 260 r:run(2);
261 assert(r.state == "ready"); 261 assert(r.state == "ready");
262 r:run(3); 262 r:run(3);
263 assert(r.state == "waiting"); 263 assert(r.state == "waiting");
264 done(); 264 done();
265 assert(r.state == "ready"); 265 assert(r.state == "ready");
266 --for k, v in ipairs(l) do print(k,v) end 266 --for k, v in ipairs(l) do print(k,v) end
267 end); 267 end);
268 268
269 it("should work", function () 269 it("should work", function ()
270 -------------------- 270 --------------------
271 local wait, done; 271 local wait, done;
272 local last_item = 0; 272 local last_item = 0;
273 local r, l = new(function (item) 273 local r, l = new(function (item)
277 if item == 3 then 277 if item == 3 then
278 wait, done = async.waiter(); 278 wait, done = async.waiter();
279 wait(); 279 wait();
280 end 280 end
281 end); 281 end);
282 282
283 r:run(1); 283 r:run(1);
284 assert(r.state == "ready"); 284 assert(r.state == "ready");
285 r:run(2); 285 r:run(2);
286 assert(r.state == "ready"); 286 assert(r.state == "ready");
287 r:run(3); 287 r:run(3);
303 if item == 3 then 303 if item == 3 then
304 wait, done = async.waiter(); 304 wait, done = async.waiter();
305 wait(); 305 wait();
306 end 306 end
307 end); 307 end);
308 308
309 r:run(1); 309 r:run(1);
310 assert(r.state == "ready"); 310 assert(r.state == "ready");
311 r:run(2); 311 r:run(2);
312 assert(r.state == "ready"); 312 assert(r.state == "ready");
313 313
314 local dones = {}; 314 local dones = {};
315 r:run(3); 315 r:run(3);
316 assert(r.state == "waiting"); 316 assert(r.state == "waiting");
317 r:run(3); 317 r:run(3);
318 assert(r.state == "waiting"); 318 assert(r.state == "waiting");
319 r:run(3); 319 r:run(3);
320 assert(r.state == "waiting"); 320 assert(r.state == "waiting");
321 r:run(4); 321 r:run(4);
322 assert(r.state == "waiting"); 322 assert(r.state == "waiting");
323 323
324 for i = 1, 3 do 324 for i = 1, 3 do
325 done(); 325 done();
326 if i < 3 then 326 if i < 3 then
327 assert(r.state == "waiting"); 327 assert(r.state == "waiting");
328 end 328 end
329 end 329 end
330 330
331 assert(r.state == "ready"); 331 assert(r.state == "ready");
332 --for k, v in ipairs(l) do print(k,v) end 332 --for k, v in ipairs(l) do print(k,v) end
333 end); 333 end);
334 it("should work", function () 334 it("should work", function ()
335 -------------------- 335 --------------------
342 if item == 3 then 342 if item == 3 then
343 wait, done = async.waiter(); 343 wait, done = async.waiter();
344 wait(); 344 wait();
345 end 345 end
346 end); 346 end);
347 347
348 r:run(1); 348 r:run(1);
349 assert(r.state == "ready"); 349 assert(r.state == "ready");
350 r:run(2); 350 r:run(2);
351 assert(r.state == "ready"); 351 assert(r.state == "ready");
352 352
353 local dones = {}; 353 local dones = {};
354 r:run(3); 354 r:run(3);
355 assert(r.state == "waiting"); 355 assert(r.state == "waiting");
356 r:run(3); 356 r:run(3);
357 assert(r.state == "waiting"); 357 assert(r.state == "waiting");
358 358
359 for i = 1, 2 do 359 for i = 1, 2 do
360 done(); 360 done();
361 if i < 2 then 361 if i < 2 then
362 assert(r.state == "waiting"); 362 assert(r.state == "waiting");
363 end 363 end
364 end 364 end
365 365
366 assert(r.state == "ready"); 366 assert(r.state == "ready");
367 r:run(4); 367 r:run(4);
368 assert(r.state == "ready"); 368 assert(r.state == "ready");
369 369
370 assert(r.state == "ready"); 370 assert(r.state == "ready");
371 --for k, v in ipairs(l) do print(k,v) end 371 --for k, v in ipairs(l) do print(k,v) end
372 end); 372 end);
373 it("should work with multiple runners in parallel", function () 373 it("should work with multiple runners in parallel", function ()
374 -- Now with multiple runners 374 -- Now with multiple runners
382 if item == 3 then 382 if item == 3 then
383 wait1, done1 = async.waiter(); 383 wait1, done1 = async.waiter();
384 wait1(); 384 wait1();
385 end 385 end
386 end, "r1"); 386 end, "r1");
387 387
388 local wait2, done2; 388 local wait2, done2;
389 local last_item2 = 0; 389 local last_item2 = 0;
390 local r2, l2 = new(function (item) 390 local r2, l2 = new(function (item)
391 assert(type(item) == "number") 391 assert(type(item) == "number")
392 assert((item == last_item2 + 1) or item == 3); 392 assert((item == last_item2 + 1) or item == 3);
394 if item == 3 then 394 if item == 3 then
395 wait2, done2 = async.waiter(); 395 wait2, done2 = async.waiter();
396 wait2(); 396 wait2();
397 end 397 end
398 end, "r2"); 398 end, "r2");
399 399
400 r1:run(1); 400 r1:run(1);
401 assert(r1.state == "ready"); 401 assert(r1.state == "ready");
402 r1:run(2); 402 r1:run(2);
403 assert(r1.state == "ready"); 403 assert(r1.state == "ready");
404 404
405 local dones = {}; 405 local dones = {};
406 r1:run(3); 406 r1:run(3);
407 assert(r1.state == "waiting"); 407 assert(r1.state == "waiting");
408 r1:run(3); 408 r1:run(3);
409 assert(r1.state == "waiting"); 409 assert(r1.state == "waiting");
410 410
411 r2:run(1); 411 r2:run(1);
412 assert(r1.state == "waiting"); 412 assert(r1.state == "waiting");
413 assert(r2.state == "ready"); 413 assert(r2.state == "ready");
414 414
415 r2:run(2); 415 r2:run(2);
416 assert(r1.state == "waiting"); 416 assert(r1.state == "waiting");
417 assert(r2.state == "ready"); 417 assert(r2.state == "ready");
418 418
419 r2:run(3); 419 r2:run(3);
420 assert(r1.state == "waiting"); 420 assert(r1.state == "waiting");
421 assert(r2.state == "waiting"); 421 assert(r2.state == "waiting");
422 done2(); 422 done2();
423 423
424 r2:run(3); 424 r2:run(3);
425 assert(r1.state == "waiting"); 425 assert(r1.state == "waiting");
426 assert(r2.state == "waiting"); 426 assert(r2.state == "waiting");
427 done2(); 427 done2();
428 428
429 r2:run(4); 429 r2:run(4);
430 assert(r1.state == "waiting"); 430 assert(r1.state == "waiting");
431 assert(r2.state == "ready"); 431 assert(r2.state == "ready");
432 432
433 for i = 1, 2 do 433 for i = 1, 2 do
434 done1(); 434 done1();
435 if i < 2 then 435 if i < 2 then
436 assert(r1.state == "waiting"); 436 assert(r1.state == "waiting");
437 end 437 end
438 end 438 end
439 439
440 assert(r1.state == "ready"); 440 assert(r1.state == "ready");
441 r1:run(4); 441 r1:run(4);
442 assert(r1.state == "ready"); 442 assert(r1.state == "ready");
443 443
444 assert(r1.state == "ready"); 444 assert(r1.state == "ready");
445 --for k, v in ipairs(l1) do print(k,v) end 445 --for k, v in ipairs(l1) do print(k,v) end
446 end); 446 end);
447 it("should work work with multiple runners in parallel", function () 447 it("should work work with multiple runners in parallel", function ()
448 -------------------- 448 --------------------
517 r2:run(4); 517 r2:run(4);
518 assert.equal(r1.state, "waiting"); 518 assert.equal(r1.state, "waiting");
519 assert.equal(r2.state, "ready"); 519 assert.equal(r2.state, "ready");
520 520
521 done1(); 521 done1();
522 522
523 assert.equal(r1.state, "ready"); 523 assert.equal(r1.state, "ready");
524 r1:run(4); 524 r1:run(4);
525 assert.equal(r1.state, "ready"); 525 assert.equal(r1.state, "ready");
526 526
527 assert.equal(r1.state, "ready"); 527 assert.equal(r1.state, "ready");