test_message_handler.py 5.41 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
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
163
164
165
166
167
#!/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
from ..inputs import RemoteInputGroup
from ..inputs import Input
from ..inputs import InputGroup
from ..inputs import InputList

from .mocks import MockDataSource

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'
    port = server_socket.bind_to_random_port(address)
    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)

    self.remote_group = RemoteInputGroup('channel', False, client_socket)
    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()