aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/net/udp.lux
blob: 1740c861cbdf179626e5a636e1ca651ae8f5f6ca (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
(.module:
  [lux #*
   data/text/format
   ["r" math/random (#+ Random)]
   ["_" test (#+ Test)]
   [control
    ["." monad (#+ do)]
    [concurrency
     ["." promise]]
    [security
     ["!" capability]]]
   [data
    ["." error]
    ["." text]]
   ["." io]
   [world
    ["." binary]
    ["." net]]]
  {1
   ["." /]}
  [///
   ["_." binary]])

(def: localhost net.Address "127.0.0.1")
(def: port
  (r.Random net.Port)
  (|> r.nat
      (:: r.monad map
          (|>> (n/% 1000)
               (n/+ 8000)))))

(def: #export test
  Test
  (<| (_.context (%name (name-of /._)))
      (do r.monad
        [port ..port
         size (|> r.nat (:: @ map (|>> (n/% 100) (n/max 10))))
         from (_binary.binary size)
         to (_binary.binary size)]
        ($_ _.and
            (wrap (do promise.monad
                    [result (promise.future
                             (do (error.with io.monad)
                               [server (/.server port)
                                client /.client
                                ####################
                                _ (!.use (:: client write) [[localhost port] from])
                                [bytes-from [from-address from-port] temp] (!.use (:: server read) size)
                                #let [from-worked? (and (n/= size bytes-from)
                                                        (:: binary.equivalence = from temp))]
                                ####################
                                _ (!.use (:: server write) [[from-address from-port] to])
                                [bytes-to [to-address to-port] temp] (!.use (:: client read) size)
                                #let [to-worked? (and (n/= size bytes-to)
                                                      (:: binary.equivalence = to temp)
                                                      (n/= port to-port))]
                                ####################
                                _ (!.use (:: client close) [])
                                _ (!.use (:: server close) [])]
                               ## (wrap false)
                               (wrap (and from-worked?
                                          to-worked?))
                               ))]
                    (_.assert "Can communicate between client and server."
                              (error.default #0 result))))
            ))))