aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/net/tcp.lux
blob: fae5ac05d87713f40f4bce1aa4a23a7e15d1e596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)))))
        )))