summaryrefslogtreecommitdiffhomepage
path: root/host/mex/test/test.py
blob: 4be24f4913f4af957007ceeab0c7d15deff7e83a (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
import sys
sys.path.append (sys.path[0] + '/../..')

from mex.hub import Hub
from mex.node import Node
from mex.msg import Msg
from utils.forked import Forked
import select

def log (x):
    print x

h = Hub (min_clients = 2, log = log)

def c1 ():
    n = Node ()
    def a (msg):
        print 'oucouc'
        nb, = msg.pop ('B')
        nb += 1
        m = Msg (msg.mtype)
        m.push ('B', nb)
        n.response (m)
    n.register (0x82, a)
    m = Msg (0x81)
    n.send (m)
    n.wait ()

f1 = Forked (c1)

def c2 ():
    n = Node ()
    def a (msg):
        print 'coucou'
    n.register (0x81, a)
    m = Msg (0x82)
    m.push ('B', 42)
    r = n.request (m)
    assert r.mtype == 0x82
    assert r.pop ('B') == (43,)
    n.wait_async (42)
    while not n.sync ():
        fds = select.select ((n, ), (), ())[0]
        for i in fds:
            i.read ()
    n.wait ()

f2 = Forked (c2)

try:
    h.wait ()
finally:
    f1.kill ()
    f2.kill ()
    import time
    time.sleep (1)