aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test
diff options
context:
space:
mode:
authorEduardo Julian2018-12-09 17:12:33 -0400
committerEduardo Julian2018-12-09 17:12:33 -0400
commitb47835644a16f43155b44333c0218293bcaf20a6 (patch)
tree6f765133f0b745130cfa62f779e8e732a52d76fe /stdlib/test
parent93ac89e2f8a47e412dcb6b6944c5e39b3ea073f0 (diff)
Basic implementation for sessions.
Diffstat (limited to 'stdlib/test')
-rw-r--r--stdlib/test/test/lux/concurrency/session.lux78
1 files changed, 78 insertions, 0 deletions
diff --git a/stdlib/test/test/lux/concurrency/session.lux b/stdlib/test/test/lux/concurrency/session.lux
new file mode 100644
index 000000000..ad0343d15
--- /dev/null
+++ b/stdlib/test/test/lux/concurrency/session.lux
@@ -0,0 +1,78 @@
+(.module:
+ [lux #*
+ [control
+ [monad (#+ do)]]
+ [concurrency
+ ["." promise]
+ ["$" session (#+ Session choice: << >> ?? !! +<< -<< \/ /\)]]
+ [data
+ [text
+ format]]
+ [math
+ ["r" random]]]
+ lux/test)
+
+(choice: Calculation [#Add #Negate])
+
+(def: $add (<| (!! Int) (!! Int) (?? Int) >>))
+(def: $negate (<| (!! Int) (?? Int) >>))
+
+(def: (add-session _)
+ (-> [] (:~ (<< $add)))
+ (<| $.send $.send $.receive $.end))
+
+(def: (negate-session _)
+ (-> [] (:~ (<< $negate)))
+ (<| $.send $.receive $.end))
+
+(def: $calculation
+ Session
+ ($_ \/
+ $add
+ $negate))
+
+(def: (calculation-session _)
+ (-> [] (:~ (<< $calculation)))
+ ($_ $.fork
+ (add-session [])
+ (negate-session [])))
+
+(def: (__my-calculation _)
+ (-> [] (:~ (+<< $calculation)))
+ ($.my (calculation-session [])))
+
+(def: (__your-calculation _)
+ (-> [] (:~ (-<< $calculation)))
+ ($.your (calculation-session [])))
+
+(context: "Sessions."
+ (do @
+ [#let [[$me $you] (calculation-session [])]
+ param r.int
+ subject r.int]
+ ($_ seq
+ (wrap (do promise.Monad<Promise>
+ [$me ($me (#Add id))
+ $me ($me param)
+ $me ($me subject)
+ [output $end] $me]
+ (assert "Client [#Add]"
+ (i/= (i/+ param subject) output))))
+ (wrap (do promise.Monad<Promise>
+ [choice $you]
+ (case choice
+ (#Add $you)
+ (do @
+ [[param-input $you] $you
+ [subject-input $you] $you
+ $end ($you (i/+ param-input subject-input))]
+ (assert "Server [#Add]"
+ true))
+
+ (#Negate $you)
+ (do @
+ [[subject $you] $you
+ $end ($you (i/* -1 subject))]
+ (assert "Server [#Negate]"
+ true)))))
+ )))