diff options
Diffstat (limited to 'stdlib/source/test/lux/control/concurrency/promise.lux')
-rw-r--r-- | stdlib/source/test/lux/control/concurrency/promise.lux | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/stdlib/source/test/lux/control/concurrency/promise.lux b/stdlib/source/test/lux/control/concurrency/promise.lux new file mode 100644 index 000000000..e50320901 --- /dev/null +++ b/stdlib/source/test/lux/control/concurrency/promise.lux @@ -0,0 +1,68 @@ +(.module: + [lux #* + ["." io] + [control + ["M" monad (#+ Monad do)] + pipe + [concurrency + ["&" promise ("&/." monad)]]] + [math + ["r" random]]] + lux/test) + +(context: "Promises" + ($_ seq + (wrap (do &.monad + [running? (&.future (io.io #1))] + (assert "Can run IO actions in separate threads." + running?))) + + (wrap (do &.monad + [_ (&.wait 500)] + (assert "Can wait for a specified amount of time." + #1))) + + (wrap (do &.monad + [[left right] (&.and (&.future (io.io #1)) + (&.future (io.io #0)))] + (assert "Can combine promises sequentially." + (and left (not right))))) + + (wrap (do &.monad + [?left (&.or (&.delay 100 #1) + (&.delay 200 #0)) + ?right (&.or (&.delay 200 #1) + (&.delay 100 #0))] + (assert "Can combine promises alternatively." + (case [?left ?right] + [(#.Left #1) (#.Right #0)] + #1 + + _ + #0)))) + + (wrap (do &.monad + [?left (&.either (&.delay 100 #1) + (&.delay 200 #0)) + ?right (&.either (&.delay 200 #1) + (&.delay 100 #0))] + (assert "Can combine promises alternatively [Part 2]." + (and ?left (not ?right))))) + + (test "Can poll a promise for its value." + (and (|> (&.poll (&/wrap #1)) + (case> (#.Some #1) #1 _ #0)) + (|> (&.poll (&.delay 200 #1)) + (case> #.None #1 _ #0)))) + + (wrap (do &.monad + [?none (&.time-out 100 (&.delay 200 #1)) + ?some (&.time-out 200 (&.delay 100 #1))] + (assert "Can establish maximum waiting times for promises to be fulfilled." + (case [?none ?some] + [#.None (#.Some #1)] + #1 + + _ + #0)))) + )) |