aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/net/udp.lux
blob: 2b85958faf7e00e830fe3bc1a597c65280ad5e3e (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
(.module:
  [lux #*
   [control
    [monad (#+ do)]
    [security
     ["." integrity]]]
   [concurrency
    ["." promise]]
   [data
    ["." error]
    ["." text
     format]]
   ["." io]
   [world
    ["." binary]
    ["." net
     ["@" udp]]]
   [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: "UDP 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
                [result (promise.future
                         (do io.monad
                           [server (@.server port)
                            client @.client
                            ####################
                            _ (:: client write [[localhost port] from])
                            [bytes-from [from-address from-port] temp] (:: server read size)
                            #let [from-worked? (and (n/= size bytes-from)
                                                    (:: binary.equivalence = from (integrity.trust temp)))]
                            ####################
                            _ (:: server write [[from-address from-port] to])
                            [bytes-to [to-address to-port] temp] (:: client read size)
                            #let [to-worked? (and (n/= size bytes-to)
                                                  (:: binary.equivalence = to (integrity.trust temp))
                                                  (n/= port to-port))]
                            ####################
                            _ (:: client close [])
                            _ (:: server close [])]
                           ## (wrap false)
                           (wrap (and from-worked?
                                      to-worked?))
                           ))]
                (assert "Can communicate between client and server."
                        (error.default #0 result))))
        )))