From 9671484b6cb3f3c56d6a3053a4a55b4634c14a89 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 22 Aug 2022 20:45:33 -0400 Subject: Added support for the agent model. --- stdlib/source/test/lux/control.lux | 2 + .../source/test/lux/control/concurrency/actor.lux | 60 ++++++++++++++++++++++ .../source/test/lux/control/concurrency/agent.lux | 49 ++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 stdlib/source/test/lux/control/concurrency/agent.lux (limited to 'stdlib/source/test') diff --git a/stdlib/source/test/lux/control.lux b/stdlib/source/test/lux/control.lux index c51399523..4eba3a2a7 100644 --- a/stdlib/source/test/lux/control.lux +++ b/stdlib/source/test/lux/control.lux @@ -7,6 +7,7 @@ ["[1][0]" concatenative] ["[1][0]" concurrency ["[1]/[0]" actor] + ["[1]/[0]" agent] ["[1]/[0]" atom] ["[1]/[0]" frp] ["[1]/[0]" thread] @@ -39,6 +40,7 @@ Test (all _.and /concurrency/actor.test + /concurrency/agent.test /concurrency/atom.test /concurrency/frp.test /concurrency/thread.test diff --git a/stdlib/source/test/lux/control/concurrency/actor.lux b/stdlib/source/test/lux/control/concurrency/actor.lux index e8d3568fd..8353b2e8a 100644 --- a/stdlib/source/test/lux/control/concurrency/actor.lux +++ b/stdlib/source/test/lux/control/concurrency/actor.lux @@ -200,4 +200,64 @@ actual (async.future (atom.read! sink))] (unit.coverage [/.Stop /.observe! /.obituary] (at (list.equivalence n.equivalence) = expected (sequence.list actual)))))) + (in (do async.monad + [actor (async.future (/.spawn! /.default initial_state)) + actual (/.tell! /.state actor)] + (unit.coverage [/.state] + (<| (try.else false) + (do try.monad + [actual actual] + (in (same? initial_state actual))))))) + (in (do async.monad + [actor (async.future (/.spawn! /.default initial_state)) + before,after (/.tell! (/.update ++) actor) + actual (/.tell! /.state actor)] + (unit.coverage [/.update] + (<| (try.else false) + (do try.monad + [[before after] before,after + actual actual] + (in (and (n.= (++ before) after) + (same? after actual)))))))) + (in (do async.monad + [actor (async.future (/.spawn! /.default initial_state)) + before,after (/.tell! (/.update ++) actor) + _ (/.tell! (/.reset initial_state) actor) + actual (/.tell! /.state actor)] + (unit.coverage [/.reset] + (<| (try.else false) + (do try.monad + [[before after] before,after + actual actual] + (in (and (n.= (++ before) after) + (same? initial_state before) + (same? initial_state actual)))))))) + (in (do async.monad + [actor (async.future (/.spawn! (/.validated (n.< initial_state) /.default) + initial_state)) + before,after (/.tell! (/.update --) actor) + actual (/.tell! /.state actor)] + (unit.coverage [/.validated] + (<| (try.else false) + (do try.monad + [[before after] before,after + actual actual] + (in (and (n.= (-- before) after) + (same? after actual)))))))) + (in (do async.monad + [actor (async.future (/.spawn! (/.validated (n.< initial_state) /.default) + initial_state)) + before,after (/.tell! (/.update ++) actor) + [cause_of_death state pending] (/.obituary actor) + actual (/.tell! /.state actor)] + (unit.coverage [/.invalid] + (when [before,after actual] + [{try.#Success [before after]} + {try.#Failure afterwards}] + (and (n.= (++ before) after) + (exception.match? /.invalid cause_of_death) + (exception.match? /.dead afterwards)) + + _ + false)))) )))) diff --git a/stdlib/source/test/lux/control/concurrency/agent.lux b/stdlib/source/test/lux/control/concurrency/agent.lux new file mode 100644 index 000000000..9627da9f9 --- /dev/null +++ b/stdlib/source/test/lux/control/concurrency/agent.lux @@ -0,0 +1,49 @@ +(.require + [library + [lux (.except) + [abstract + [monad (.only do)]] + [control + ["[0]" try]] + [math + ["[0]" random] + [number + ["n" nat]]] + [test + ["_" property (.only Test)] + ["[0]" unit]]]] + [\\library + ["[0]" / (.only) + [// + ["[0]" atom (.only Atom)] + ["[0]" async (.only Async Resolver) (.use "[1]#[0]" monad)] + ["[0]" frp] + ["[0]" actor]]]]) + +(def .public test + Test + (do [! random.monad] + [left random.nat + right random.nat] + (<| (_.covering /._) + (_.for [/.Agent]) + (all _.and + (in (do async.monad + [agent (async.future + (actor.spawn! actor.default 0)) + _ (async.future + (/.react! (frp.sequential 0 (list left right)) + (function (_ next current) + (async#in {try.#Success (n.+ next current)})) + agent)) + _ (async.delay 1) + ?state (actor.tell! actor.state agent)] + (unit.coverage [/.react!] + (when ?state + {try.#Success actual} + (n.= (n.+ left right) + actual) + + failure + false)))) + )))) -- cgit v1.2.3