aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/net/tcp.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/test/lux/world/net/tcp.lux')
-rw-r--r--stdlib/source/test/lux/world/net/tcp.lux71
1 files changed, 71 insertions, 0 deletions
diff --git a/stdlib/source/test/lux/world/net/tcp.lux b/stdlib/source/test/lux/world/net/tcp.lux
new file mode 100644
index 000000000..fae5ac05d
--- /dev/null
+++ b/stdlib/source/test/lux/world/net/tcp.lux
@@ -0,0 +1,71 @@
+(.module:
+ [lux #*
+ ["." io]
+ [control
+ [monad (#+ do)]
+ ["ex" exception (#+ exception:)]
+ [security
+ ["." taint]]]
+ [concurrency
+ ["." promise (#+ Promise promise)]
+ [frp ("frp/." functor)]]
+ [data
+ ["." error]
+ ["." text
+ format]]
+ [world
+ ["." binary]
+ ["." net
+ ["@" tcp]]]
+ [math
+ ["r" random]]]
+ lux/test
+ [///
+ ["_." binary]])
+
+(def: localhost net.Address "127.0.0.1")
+
+(def: port
+ (r.Random net.Port)
+ (|> r.nat
+ (:: r.monad map
+ (|>> (n/% 1000)
+ (n/+ 8000)))))
+
+(context: "TCP networking."
+ (do @
+ [port ..port
+ size (|> r.nat (:: @ map (|>> (n/% 100) (n/max 10))))
+ from (_binary.binary size)
+ to (_binary.binary size)]
+ ($_ seq
+ (wrap (do promise.monad
+ [#let [from-worked? (: (Promise Bit)
+ (promise #.Nil))]
+ result (promise.future
+ (do io.monad
+ [[server-close server] (@.server port)
+ #let [_ (frp/map (function (_ client)
+ (promise.future
+ (do @
+ [[trasmission-size transmission] (:: client read size)
+ #let [_ (io.run (promise.resolve (and (n/= size trasmission-size)
+ (:: binary.equivalence = from (taint.trust transmission)))
+ from-worked?))]]
+ (:: client write to))))
+ server)]
+ client (@.client localhost port)
+ _ (:: client write from)
+ ####################
+ [trasmission-size transmission] (:: client read size)
+ #let [to-worked? (and (n/= size trasmission-size)
+ (:: binary.equivalence = to (taint.trust transmission)))]
+ ####################
+ _ (:: client close [])
+ _ (io.from-io (promise.resolve [] server-close))]
+ (wrap to-worked?)))
+ from-worked? from-worked?]
+ (assert "Can communicate between client and server."
+ (and from-worked?
+ (error.default #0 result)))))
+ )))