summaryrefslogtreecommitdiff
path: root/host/mex/test/test.py
blob: 6332ecb8e4f03e066b25c58487b46e8ffadf0915 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
# mex - Message Exchange library. {{{
#
# Copyright (C) 2008 Nicolas Schodet
#
# APBTeam:
#        Web: http://apbteam.org/
#      Email: team AT apbteam DOT org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# }}} */
import select

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

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)
    def b ():
        assert False
    eb = n.schedule (31, b)
    def c ():
        print 'hello'
        n.cancel (eb)
    n.schedule (28, c)
    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)