|
11 | 11 | raises)
|
12 | 12 |
|
13 | 13 | from . import (
|
| 14 | + ComposedDispatcher, |
14 | 15 | Constant,
|
15 | 16 | Effect,
|
16 | 17 | base_dispatcher,
|
17 | 18 | parallel,
|
18 |
| - sync_perform) |
| 19 | + sync_perform, |
| 20 | + sync_performer) |
19 | 21 | from .do import do, do_return
|
| 22 | +from .fold import FoldError, sequence |
20 | 23 | from .testing import (
|
21 | 24 | ESConstant,
|
22 | 25 | ESError,
|
|
25 | 28 | EQFDispatcher,
|
26 | 29 | SequenceDispatcher,
|
27 | 30 | fail_effect,
|
| 31 | + parallel_sequence, |
28 | 32 | perform_sequence,
|
29 | 33 | resolve_effect,
|
30 | 34 | resolve_stubs)
|
@@ -403,3 +407,58 @@ def code_under_test():
|
403 | 407 | expected = ("sequence: MyIntent(val='a')\n"
|
404 | 408 | "NOT FOUND: OtherIntent(val='b')")
|
405 | 409 | assert expected in str(exc.value)
|
| 410 | + |
| 411 | + |
| 412 | +def test_parallel_sequence(): |
| 413 | + """ |
| 414 | + Ensures that all parallel effects are found in the given intents, in |
| 415 | + order, and returns the results associated with those intents. |
| 416 | + """ |
| 417 | + seq = [ |
| 418 | + parallel_sequence([ |
| 419 | + [(1, lambda i: "one!")], |
| 420 | + [(2, lambda i: "two!")], |
| 421 | + [(3, lambda i: "three!")], |
| 422 | + ]) |
| 423 | + ] |
| 424 | + p = parallel([Effect(1), Effect(2), Effect(3)]) |
| 425 | + assert perform_sequence(seq, p) == ['one!', 'two!', 'three!'] |
| 426 | + |
| 427 | + |
| 428 | +def test_parallel_sequence_fallback(): |
| 429 | + """ |
| 430 | + Accepts a ``fallback`` dispatcher that will be used when the sequence |
| 431 | + doesn't contain an intent. |
| 432 | + """ |
| 433 | + def dispatch_2(intent): |
| 434 | + if intent == 2: |
| 435 | + return sync_performer(lambda d, i: "two!") |
| 436 | + fallback = ComposedDispatcher([dispatch_2, base_dispatcher]) |
| 437 | + seq = [ |
| 438 | + parallel_sequence([ |
| 439 | + [(1, lambda i: 'one!')], |
| 440 | + [], # only implicit effects in this slot |
| 441 | + [(3, lambda i: 'three!')], |
| 442 | + ], |
| 443 | + fallback_dispatcher=fallback), |
| 444 | + ] |
| 445 | + p = parallel([Effect(1), Effect(2), Effect(3)]) |
| 446 | + assert perform_sequence(seq, p) == ['one!', 'two!', 'three!'] |
| 447 | + |
| 448 | + |
| 449 | +def test_parallel_sequence_must_be_parallel(): |
| 450 | + """ |
| 451 | + If the sequences aren't run in parallel, the parallel_sequence won't |
| 452 | + match and a FoldError of NoPerformerFoundError will be raised. |
| 453 | + """ |
| 454 | + seq = [ |
| 455 | + parallel_sequence([ |
| 456 | + [(1, lambda i: "one!")], |
| 457 | + [(2, lambda i: "two!")], |
| 458 | + [(3, lambda i: "three!")], |
| 459 | + ]) |
| 460 | + ] |
| 461 | + p = sequence([Effect(1), Effect(2), Effect(3)]) |
| 462 | + with pytest.raises(FoldError) as excinfo: |
| 463 | + perform_sequence(seq, p) |
| 464 | + assert excinfo.value.wrapped_exception[0] is AssertionError |
0 commit comments