test_message_handler.py 7.03 KB
Newer Older
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
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.core module of the BEAT platform.             #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform 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.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################


# Tests for experiment execution

import os
import logging
logger = logging.getLogger(__name__)

# in case you want to see the printouts dynamically, set to ``True``
if False:
  logger = logging.getLogger() #root logger
  logger.setLevel(logging.DEBUG)
  ch = logging.StreamHandler()
  ch.setLevel(logging.DEBUG)
  ch.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
  logger.addHandler(ch)

import unittest
import zmq.green as zmq
import nose.tools

from ..agent import MessageHandler
from ..dataformat import DataFormat
from ..inputs import RemoteInput
51
from ..inputs import RemoteException
52
53
54
55
56
from ..inputs import Input
from ..inputs import InputGroup
from ..inputs import InputList

from .mocks import MockDataSource
57
from .mocks import MockDataSource_Crash
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

from . import prefix



class TestMessageHandler(unittest.TestCase):

  def setUp(self):
    dataformat = DataFormat(prefix, 'user/single_integer/1')

    data_source_a = MockDataSource([
        dataformat.type(value=10),
        dataformat.type(value=20),
      ],
      [
        (0, 0),
        (1, 1),
      ]
    )

    input_a = Input('a', 'user/single_integer/1', data_source_a)

    data_source_b = MockDataSource([
        dataformat.type(value=100),
        dataformat.type(value=200),
      ],
      [
        (0, 0),
        (1, 1),
      ]
    )

    input_b = Input('b', 'user/single_integer/1', data_source_b)

    group = InputGroup('channel')
    group.add(input_a)
    group.add(input_b)

    self.input_list = InputList()
    self.input_list.add(group)


    self.server_context = zmq.Context()
    server_socket = self.server_context.socket(zmq.PAIR)
    address = 'tcp://127.0.0.1'
103
    port = server_socket.bind_to_random_port(address, min_port=50000)
104
105
106
107
108
109
110
111
112
113
114
115
    address += ':%d' % port

    self.message_handler = MessageHandler(self.input_list, self.server_context, server_socket)


    self.client_context = zmq.Context()
    client_socket = self.client_context.socket(zmq.PAIR)
    client_socket.connect(address)

    self.remote_input_a = RemoteInput('a', dataformat, client_socket)
    self.remote_input_b = RemoteInput('b', dataformat, client_socket)

116
    self.remote_group = InputGroup('channel', restricted_access=False)
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
163
164
165
166
167
168
    self.remote_group.add(self.remote_input_a)
    self.remote_group.add(self.remote_input_b)

    self.remote_input_list = InputList()
    self.remote_input_list.add(self.remote_group)

    self.message_handler.start()


  def test_input_has_more_data(self):
    assert self.remote_input_a.hasMoreData()


  def test_input_next(self):
    self.remote_input_a.next()
    nose.tools.eq_(self.remote_input_a.data.value, 10)


  def test_input_full_cycle(self):
    assert self.remote_input_a.hasMoreData()
    self.remote_input_a.next()
    nose.tools.eq_(self.remote_input_a.data.value, 10)

    assert self.remote_input_a.hasMoreData()
    self.remote_input_a.next()
    nose.tools.eq_(self.remote_input_a.data.value, 20)

    assert not self.remote_input_a.hasMoreData()


  def test_group_has_more_data(self):
    assert self.remote_group.hasMoreData()


  def test_group_next(self):
    self.remote_group.next()
    nose.tools.eq_(self.remote_input_a.data.value, 10)
    nose.tools.eq_(self.remote_input_b.data.value, 100)


  def test_group_full_cycle(self):
    assert self.remote_group.hasMoreData()
    self.remote_group.next()
    nose.tools.eq_(self.remote_input_a.data.value, 10)
    nose.tools.eq_(self.remote_input_b.data.value, 100)

    assert self.remote_group.hasMoreData()
    self.remote_group.next()
    nose.tools.eq_(self.remote_input_a.data.value, 20)
    nose.tools.eq_(self.remote_input_b.data.value, 200)

    assert not self.remote_group.hasMoreData()
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192


#----------------------------------------------------------


class TestMessageHandlerErrorHandling(unittest.TestCase):

  def setUp(self):
    dataformat = DataFormat(prefix, 'user/single_integer/1')

    data_source = MockDataSource_Crash()

    input = Input('in', 'user/single_integer/1', data_source)

    group = InputGroup('channel')
    group.add(input)

    self.input_list = InputList()
    self.input_list.add(group)


    self.server_context = zmq.Context()
    server_socket = self.server_context.socket(zmq.PAIR)
    address = 'tcp://127.0.0.1'
193
    port = server_socket.bind_to_random_port(address, min_port=50000)
194
195
196
197
198
199
200
201
202
203
204
    address += ':%d' % port

    self.message_handler = MessageHandler(self.input_list, self.server_context, server_socket)


    self.client_context = zmq.Context()
    client_socket = self.client_context.socket(zmq.PAIR)
    client_socket.connect(address)

    self.remote_input = RemoteInput('in', dataformat, client_socket)

205
    self.remote_group = InputGroup('channel', restricted_access=False)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    self.remote_group.add(self.remote_input)

    self.remote_input_list = InputList()
    self.remote_input_list.add(self.remote_group)

    self.message_handler.start()


  def test_input_has_more_data(self):
    self.assertRaises(RemoteException, self.remote_input.hasMoreData)


  def test_input_next(self):
    self.assertRaises(RemoteException, self.remote_input.next)


  def test_group_has_more_data(self):
    self.assertRaises(RemoteException, self.remote_group.hasMoreData)


  def test_group_next(self):
    self.assertRaises(RemoteException, self.remote_group.next)