File: test_subscription_wire.py

package info (click to toggle)
robotraconteur 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,380 kB
  • sloc: cpp: 1,149,268; cs: 87,653; java: 58,127; python: 26,897; ansic: 356; sh: 152; makefile: 90; xml: 51
file content (162 lines) | stat: -rw-r--r-- 4,620 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import RobotRaconteur as RR
import pytest
import time
import threading

_wire_sub_test_service_def = """
service experimental.wire_sub_test

object testobj
    wire double testwire1 [readonly]
    wire double testwire2 [writeonly]
    objref testobj2 subobj
end

object testobj2
    wire double testwire3 [readonly]
end
"""


class _testobj_impl:
    def __init__(self):
        self.subobj = _testobj2_impl()

    def get_subobj(self):
        return self.subobj, "experimental.wire_sub_test.testobj2"


class _testobj2_impl:
    def __init__(self):
        pass


intra_server_flags = RR.RobotRaconteurNodeSetupFlags_ENABLE_INTRA_TRANSPORT \
    | RR.RobotRaconteurNodeSetupFlags_INTRA_TRANSPORT_START_SERVER \
    | RR.RobotRaconteurNodeSetupFlags_ENABLE_NODE_ANNOUNCE \
    | RR.RobotRaconteurNodeSetupFlags_ENABLE_NODE_DISCOVERY_LISTENING \
    | RR.RobotRaconteurNodeSetupFlags_DISABLE_STRINGTABLE

intra_client_flags = RR.RobotRaconteurNodeSetupFlags_ENABLE_INTRA_TRANSPORT \
    | RR.RobotRaconteurNodeSetupFlags_ENABLE_NODE_DISCOVERY_LISTENING \
    | RR.RobotRaconteurNodeSetupFlags_DISABLE_STRINGTABLE


class _testservice_impl():

    def __init__(self, nodename, nodeid):
        self.obj = _testobj_impl()
        self._node = RR.RobotRaconteurNode()
        self._node.SetNodeID(nodeid)
        self._node.Init()

        self._node.RegisterServiceType(_wire_sub_test_service_def)
        self._node.RegisterService(
            "test_service", "experimental.wire_sub_test.testobj", self.obj)

        self._node_setup = RR.RobotRaconteurNodeSetup(
            nodename, 0, flags=intra_server_flags, node=self._node)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self._node_setup.ReleaseNode()
        self._node.Shutdown()
        self._node_setup = None
        self._node = None


def test_wire_subscription():

    test_servers = {
        "server1": RR.NodeID("0d694574-1ad8-4b9e-9aea-e881524fb451"),
        "server2": RR.NodeID("e23ac123-4357-467e-b44b-4c9eb4ff7916"),
        "server3": RR.NodeID("cb71939a-6c6c-43cc-b6be-070a76acec74")
    }

    client_node = RR.RobotRaconteurNode()
    client_node.Init()

    client_node_setup = RR.RobotRaconteurNodeSetup(
        "", 0, flags=intra_client_flags, node=client_node)

    server1 = _testservice_impl("server1", test_servers["server1"])

    with client_node_setup, server1:

        value_changed_count = [0]

        def value_changed(wire, value, time):
            value_changed_count[0] += 1

        sub = client_node.SubscribeServiceByType(
            "experimental.wire_sub_test.testobj")
        wire_sub = sub.SubscribeWire("testwire1")
        wire_sub.WireValueChanged += value_changed
        sub.GetDefaultClientWait(5)

        for _ in range(10):
            if wire_sub.ActiveWireConnectionCount > 0:
                break
            time.sleep(0.01)
        else:
            assert False

        with pytest.raises(RR.ValueNotSetException):
            _ = wire_sub.InValue
        assert not wire_sub.TryGetInValue()[0]

        server1.obj.testwire1.OutValue = 5.0
        time.sleep(0.001)
        server1.obj.testwire1.OutValue = 5.0

        wire_sub.WaitInValueValid(1)

        assert wire_sub.InValue == 5.0
        assert wire_sub.TryGetInValue()[1] == 5.0

        for _ in range(10):
            if value_changed_count[0] > 0:
                break
            time.sleep(0.01)
        else:
            assert False

        wire_sub.InValueLifespan = 0.1
        assert wire_sub.InValueLifespan == 0.1
        time.sleep(0.2)
        assert not wire_sub.TryGetInValue()[0]

        wire_sub.InValueLifespan = 10
        server1.obj.testwire1.OutValue = 6.0
        time.sleep(0.05)
        assert wire_sub.InValue == 6.0

        wire_sub2 = sub.SubscribeWire("testwire3", "*.subobj")

        time.sleep(0.05)

        assert wire_sub2.ActiveWireConnectionCount > 0

        time.sleep(0.2)

        server1.obj.subobj.testwire3.OutValue = 12.345

        wire_sub2.WaitInValueValid(1)
        assert wire_sub2.InValue == 12.345

        wire_sub2.Close()

        wire_sub3 = sub.SubscribeWire("testwire2")
        time.sleep(0.05)
        assert wire_sub3.ActiveWireConnectionCount > 0
        wire_sub3.SetOutValueAll(17.0)

        time.sleep(0.05)
        assert server1.obj.testwire2.InValue[0] == 17.0
        res, val, _, _ = server1.obj.testwire2.TryGetInValue()
        assert res and val == 17.0
        server1.obj.testwire2.InValueLifespan = 0.1
        time.sleep(0.2)
        assert not server1.obj.testwire2.TryGetInValue()[0]