Software / code / prosody
Comparison
spec/util_async_spec.lua @ 8633:8ec18a002c30
util.async: tests: more code re-use
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 19 Mar 2018 16:40:40 +0000 |
| parent | 8632:02b841ed03d1 |
| child | 8648:ca710a71d730 |
comparison
equal
deleted
inserted
replaced
| 8632:02b841ed03d1 | 8633:8ec18a002c30 |
|---|---|
| 28 end | 28 end |
| 29 | 29 |
| 30 local function new(func) | 30 local function new(func) |
| 31 local event_log = {}; | 31 local event_log = {}; |
| 32 local spy_func = spy.new(func); | 32 local spy_func = spy.new(func); |
| 33 return async.runner(spy_func, mock_watchers(event_log)), event_log, spy_func; | 33 return async.runner(spy_func, mock_watchers(event_log)), spy_func, event_log; |
| 34 end | 34 end |
| 35 describe("#runner", function() | 35 describe("#runner", function() |
| 36 it("should work", function() | 36 it("should work", function() |
| 37 local r, l = new(function (item) assert(type(item) == "number") end); | 37 local r = new(function (item) assert(type(item) == "number") end); |
| 38 r:run(1); | 38 r:run(1); |
| 39 r:run(2); | 39 r:run(2); |
| 40 end); | 40 end); |
| 41 | 41 |
| 42 it("should be ready after creation", function () | 42 it("should be ready after creation", function () |
| 43 local r = async.runner(function () end); | 43 local r = new(function () end); |
| 44 assert.equal(r.state, "ready"); | 44 assert.equal(r.state, "ready"); |
| 45 end); | 45 end); |
| 46 | 46 |
| 47 it("should do nothing if the queue is empty", function () | 47 it("should do nothing if the queue is empty", function () |
| 48 local did_run; | 48 local did_run; |
| 49 local r = async.runner(function () did_run = true end); | 49 local r = new(function () did_run = true end); |
| 50 r:run(); | 50 r:run(); |
| 51 assert.equal(r.state, "ready"); | 51 assert.equal(r.state, "ready"); |
| 52 assert.is_nil(did_run); | 52 assert.is_nil(did_run); |
| 53 r:run("hello"); | 53 r:run("hello"); |
| 54 assert.is_true(did_run); | 54 assert.is_true(did_run); |
| 55 end); | 55 end); |
| 56 | 56 |
| 57 it("should support queuing work items without running", function () | 57 it("should support queuing work items without running", function () |
| 58 local did_run; | 58 local did_run; |
| 59 local r = async.runner(function () did_run = true end); | 59 local r = new(function () did_run = true end); |
| 60 r:enqueue("hello"); | 60 r:enqueue("hello"); |
| 61 assert.equal(r.state, "ready"); | 61 assert.equal(r.state, "ready"); |
| 62 assert.is_nil(did_run); | 62 assert.is_nil(did_run); |
| 63 r:run(); | 63 r:run(); |
| 64 assert.is_true(did_run); | 64 assert.is_true(did_run); |
| 65 end); | 65 end); |
| 66 | 66 |
| 67 it("should support queuing multiple work items", function () | 67 it("should support queuing multiple work items", function () |
| 68 local last_item; | 68 local last_item; |
| 69 local s = spy(function (item) last_item = item; end); | 69 local r, s = new(function (item) last_item = item; end); |
| 70 local r = async.runner(s); | |
| 71 r:enqueue("hello"); | 70 r:enqueue("hello"); |
| 72 r:enqueue("there"); | 71 r:enqueue("there"); |
| 73 r:enqueue("world"); | 72 r:enqueue("world"); |
| 74 assert.equal(r.state, "ready"); | 73 assert.equal(r.state, "ready"); |
| 75 r:run(); | 74 r:run(); |
| 78 assert.equal(last_item, "world"); | 77 assert.equal(last_item, "world"); |
| 79 end); | 78 end); |
| 80 | 79 |
| 81 it("should support all simple data types", function () | 80 it("should support all simple data types", function () |
| 82 local last_item; | 81 local last_item; |
| 83 local s = spy(function (item) last_item = item; end); | 82 local r, s = new(function (item) last_item = item; end); |
| 84 local r = async.runner(s); | |
| 85 local values = { {}, 123, "hello", true, false }; | 83 local values = { {}, 123, "hello", true, false }; |
| 86 for i = 1, #values do | 84 for i = 1, #values do |
| 87 r:enqueue(values[i]); | 85 r:enqueue(values[i]); |
| 88 end | 86 end |
| 89 assert.equal(r.state, "ready"); | 87 assert.equal(r.state, "ready"); |
| 197 end); | 195 end); |
| 198 end | 196 end |
| 199 | 197 |
| 200 it("should continue to process work items", function () | 198 it("should continue to process work items", function () |
| 201 local last_item; | 199 local last_item; |
| 202 local runner_func = spy.new(function (item) | 200 local runner, runner_func = new(function (item) |
| 203 if item == "error" then | 201 if item == "error" then |
| 204 error("test error"); | 202 error("test error"); |
| 205 end | 203 end |
| 206 last_item = item; | 204 last_item = item; |
| 207 end); | 205 end); |
| 208 local runner = async.runner(runner_func, mock{ | |
| 209 ready = function () end; | |
| 210 waiting = function () end; | |
| 211 error = function () end; | |
| 212 }); | |
| 213 runner:enqueue("one"); | 206 runner:enqueue("one"); |
| 214 runner:enqueue("error"); | 207 runner:enqueue("error"); |
| 215 runner:enqueue("two"); | 208 runner:enqueue("two"); |
| 216 runner:run(); | 209 runner:run(); |
| 217 assert.equal(runner.state, "ready"); | 210 assert.equal(runner.state, "ready"); |
| 222 assert.equal(last_item, "two"); | 215 assert.equal(last_item, "two"); |
| 223 end); | 216 end); |
| 224 | 217 |
| 225 it("should continue to process work items during resume", function () | 218 it("should continue to process work items during resume", function () |
| 226 local wait, done, last_item; | 219 local wait, done, last_item; |
| 227 local runner_func = spy.new(function (item) | 220 local runner, runner_func = new(function (item) |
| 228 if item == "wait-error" then | 221 if item == "wait-error" then |
| 229 wait, done = async.waiter(); | 222 wait, done = async.waiter(); |
| 230 wait(); | 223 wait(); |
| 231 error("test error"); | 224 error("test error"); |
| 232 end | 225 end |
| 233 last_item = item; | 226 last_item = item; |
| 234 end); | 227 end); |
| 235 local runner = async.runner(runner_func, mock{ | |
| 236 ready = function () end; | |
| 237 waiting = function () end; | |
| 238 error = function () end; | |
| 239 }); | |
| 240 runner:enqueue("one"); | 228 runner:enqueue("one"); |
| 241 runner:enqueue("wait-error"); | 229 runner:enqueue("wait-error"); |
| 242 runner:enqueue("two"); | 230 runner:enqueue("two"); |
| 243 runner:run(); | 231 runner:run(); |
| 244 done(); | 232 done(); |
| 258 end); | 246 end); |
| 259 end); | 247 end); |
| 260 it("should work", function () | 248 it("should work", function () |
| 261 local wait, done; | 249 local wait, done; |
| 262 | 250 |
| 263 local r, l = new(function (item) | 251 local r = new(function (item) |
| 264 assert(type(item) == "number") | 252 assert(type(item) == "number") |
| 265 if item == 3 then | 253 if item == 3 then |
| 266 wait, done = async.waiter(); | 254 wait, done = async.waiter(); |
| 267 wait(); | 255 wait(); |
| 268 end | 256 end |
| 281 | 269 |
| 282 it("should work", function () | 270 it("should work", function () |
| 283 -------------------- | 271 -------------------- |
| 284 local wait, done; | 272 local wait, done; |
| 285 local last_item = 0; | 273 local last_item = 0; |
| 286 local r, l = new(function (item) | 274 local r = new(function (item) |
| 287 assert(type(item) == "number") | 275 assert(type(item) == "number") |
| 288 assert(item == last_item + 1); | 276 assert(item == last_item + 1); |
| 289 last_item = item; | 277 last_item = item; |
| 290 if item == 3 then | 278 if item == 3 then |
| 291 wait, done = async.waiter(); | 279 wait, done = async.waiter(); |
| 307 end); | 295 end); |
| 308 it("should work", function () | 296 it("should work", function () |
| 309 -------------------- | 297 -------------------- |
| 310 local wait, done; | 298 local wait, done; |
| 311 local last_item = 0; | 299 local last_item = 0; |
| 312 local r, l = new(function (item) | 300 local r = new(function (item) |
| 313 assert(type(item) == "number") | 301 assert(type(item) == "number") |
| 314 assert((item == last_item + 1) or item == 3); | 302 assert((item == last_item + 1) or item == 3); |
| 315 last_item = item; | 303 last_item = item; |
| 316 if item == 3 then | 304 if item == 3 then |
| 317 wait, done = async.waiter(); | 305 wait, done = async.waiter(); |
| 345 end); | 333 end); |
| 346 it("should work", function () | 334 it("should work", function () |
| 347 -------------------- | 335 -------------------- |
| 348 local wait, done; | 336 local wait, done; |
| 349 local last_item = 0; | 337 local last_item = 0; |
| 350 local r, l = new(function (item) | 338 local r = new(function (item) |
| 351 assert(type(item) == "number") | 339 assert(type(item) == "number") |
| 352 assert((item == last_item + 1) or item == 3); | 340 assert((item == last_item + 1) or item == 3); |
| 353 last_item = item; | 341 last_item = item; |
| 354 if item == 3 then | 342 if item == 3 then |
| 355 wait, done = async.waiter(); | 343 wait, done = async.waiter(); |
| 384 it("should work with multiple runners in parallel", function () | 372 it("should work with multiple runners in parallel", function () |
| 385 -- Now with multiple runners | 373 -- Now with multiple runners |
| 386 -------------------- | 374 -------------------- |
| 387 local wait1, done1; | 375 local wait1, done1; |
| 388 local last_item1 = 0; | 376 local last_item1 = 0; |
| 389 local r1, l1 = new(function (item) | 377 local r1 = new(function (item) |
| 390 assert(type(item) == "number") | 378 assert(type(item) == "number") |
| 391 assert((item == last_item1 + 1) or item == 3); | 379 assert((item == last_item1 + 1) or item == 3); |
| 392 last_item1 = item; | 380 last_item1 = item; |
| 393 if item == 3 then | 381 if item == 3 then |
| 394 wait1, done1 = async.waiter(); | 382 wait1, done1 = async.waiter(); |
| 396 end | 384 end |
| 397 end, "r1"); | 385 end, "r1"); |
| 398 | 386 |
| 399 local wait2, done2; | 387 local wait2, done2; |
| 400 local last_item2 = 0; | 388 local last_item2 = 0; |
| 401 local r2, l2 = new(function (item) | 389 local r2 = new(function (item) |
| 402 assert(type(item) == "number") | 390 assert(type(item) == "number") |
| 403 assert((item == last_item2 + 1) or item == 3); | 391 assert((item == last_item2 + 1) or item == 3); |
| 404 last_item2 = item; | 392 last_item2 = item; |
| 405 if item == 3 then | 393 if item == 3 then |
| 406 wait2, done2 = async.waiter(); | 394 wait2, done2 = async.waiter(); |
| 456 end); | 444 end); |
| 457 it("should work work with multiple runners in parallel", function () | 445 it("should work work with multiple runners in parallel", function () |
| 458 -------------------- | 446 -------------------- |
| 459 local wait1, done1; | 447 local wait1, done1; |
| 460 local last_item1 = 0; | 448 local last_item1 = 0; |
| 461 local r1, l1 = new(function (item) | 449 local r1 = new(function (item) |
| 462 print("r1 processing ", item); | 450 print("r1 processing ", item); |
| 463 assert(type(item) == "number") | 451 assert(type(item) == "number") |
| 464 assert((item == last_item1 + 1) or item == 3); | 452 assert((item == last_item1 + 1) or item == 3); |
| 465 last_item1 = item; | 453 last_item1 = item; |
| 466 if item == 3 then | 454 if item == 3 then |
| 469 end | 457 end |
| 470 end, "r1"); | 458 end, "r1"); |
| 471 | 459 |
| 472 local wait2, done2; | 460 local wait2, done2; |
| 473 local last_item2 = 0; | 461 local last_item2 = 0; |
| 474 local r2, l2 = new(function (item) | 462 local r2 = new(function (item) |
| 475 print("r2 processing ", item); | 463 print("r2 processing ", item); |
| 476 assert.is_number(item); | 464 assert.is_number(item); |
| 477 assert((item == last_item2 + 1) or item == 3); | 465 assert((item == last_item2 + 1) or item == 3); |
| 478 last_item2 = item; | 466 last_item2 = item; |
| 479 if item == 3 then | 467 if item == 3 then |
| 537 end); | 525 end); |
| 538 | 526 |
| 539 it("should support multiple done() calls", function () | 527 it("should support multiple done() calls", function () |
| 540 local processed_item; | 528 local processed_item; |
| 541 local wait, done; | 529 local wait, done; |
| 542 local r, _, rf = new(function (item) | 530 local r, rf = new(function (item) |
| 543 wait, done = async.waiter(4); | 531 wait, done = async.waiter(4); |
| 544 wait(); | 532 wait(); |
| 545 processed_item = item; | 533 processed_item = item; |
| 546 end); | 534 end); |
| 547 r:run("test"); | 535 r:run("test"); |
| 557 end); | 545 end); |
| 558 | 546 |
| 559 it("should not allow done() to be called more than specified", function () | 547 it("should not allow done() to be called more than specified", function () |
| 560 local processed_item; | 548 local processed_item; |
| 561 local wait, done; | 549 local wait, done; |
| 562 local r, _, rf = new(function (item) | 550 local r, rf = new(function (item) |
| 563 wait, done = async.waiter(4); | 551 wait, done = async.waiter(4); |
| 564 wait(); | 552 wait(); |
| 565 processed_item = item; | 553 processed_item = item; |
| 566 end); | 554 end); |
| 567 r:run("test"); | 555 r:run("test"); |
| 574 assert.spy(r.watchers.error).was_not.called(); | 562 assert.spy(r.watchers.error).was_not.called(); |
| 575 end); | 563 end); |
| 576 | 564 |
| 577 it("should allow done() to be called before wait()", function () | 565 it("should allow done() to be called before wait()", function () |
| 578 local processed_item; | 566 local processed_item; |
| 579 local r, _, rf = new(function (item) | 567 local r, rf = new(function (item) |
| 580 local wait, done = async.waiter(); | 568 local wait, done = async.waiter(); |
| 581 done(); | 569 done(); |
| 582 wait(); | 570 wait(); |
| 583 processed_item = item; | 571 processed_item = item; |
| 584 end); | 572 end); |