python-effect / effect

effect isolation in Python, to facilitate more purely functional code
372 stars 16 forks source link

Utility for testing parallel effects in SequenceDispatcher #49

Closed radix closed 9 years ago

radix commented 9 years ago

There should be a utility assert_parallel such that you can do something like this:

seq = SequenceDispatcher([
    assert_parallel([(i1, lambda i: result), (i2, lambda i: result)]),
    (..., ...),
])

It would allow the listed intents to be specified in any order.

This is better than the idiom of composing in perform_parallel_async and then just listing the effects in the top-level SequenceDispatcher for two reasons:

Sometimes order actually does matter, though, since parallel() guarantees that results are returned in the same order as the passed-in effects (and this is used in practice to perform multiple heterogenous operations at the same time when they have no causality relationship). For that reason, there should also be an assert_parallel_ordered function that does ensure the effects are passed to the parallel in the same order that they appear in the test.

assert_parallel* doesn't sound like the greatest name, so do some more thinking about that.

radix commented 9 years ago

How about expect_parallel?

radix commented 9 years ago

@cyli wrote a utility in the otter codebase that's kinda close to this:

https://github.com/rackerlabs/otter/blob/737b7d55ce4d46ab27521f7104c1acebec235c05/otter/test/utils.py#L693-L711

It only supports effects that wrap one other effect, not multiple, but I think something more general can be extracted (and then be used to build an expect_parallel function).

cyli commented 9 years ago

@radix is there a common pattern to effects wrapping other effects that can be expected?

radix commented 9 years ago

Okay, I implemented this in otter:

https://github.com/rackerlabs/otter/blob/d9b8ba3becedf013a83b46e286a8665cb24edd66/otter/test/utils.py#L803