test_helpers.py 9.97 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.backend.python 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__)

import unittest
import zmq
import tempfile
import shutil

from ..dbexecution import DBExecutor
from ..database import Database
from ..data_loaders import DataLoader
from ..data import RemoteDataSource
from ..hash import hashDataset
from ..hash import toPath
from ..algorithm import Algorithm
from ..helpers import create_inputs_from_configuration
from ..helpers import AccessMode
from ..message_handler import MessageHandler

from . import prefix


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


DB_VIEW_HASH = hashDataset('integers_db/1', 'double', 'double')
DB_INDEX_PATH = toPath(DB_VIEW_HASH, suffix='.db')

CONFIGURATION_DB_LEGACY = {
    'queue': 'queue',
    'algorithm': 'legacy/echo/1',
    'nb_slots': 1,
    'channel': 'integers',
    'parameters': {
    },
    'environment': {
        'name': 'Python 2.7',
        'version': '1.2.0'
    },
    'inputs': {
        'in': {
            'database': 'integers_db/1',
            'protocol': 'double',
            'set': 'double',
            'output': 'a',
            'endpoint': 'in',
            'channel': 'integers',
            'path': DB_INDEX_PATH,
            'hash': DB_VIEW_HASH,
        },
    },
    'outputs': {
        'out': {
            'endpoint': 'out',
            'channel': 'integers',
            'path': '20/61/b6/2df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681',
            'hash': '2061b62df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681',
        }
    }
}

CONFIGURATION_DB_SEQUENTIAL = {
    'queue': 'queue',
    'algorithm': 'sequential/echo/1',
    'nb_slots': 1,
    'channel': 'integers',
    'parameters': {
    },
    'environment': {
        'name': 'Python 2.7',
        'version': '1.2.0'
    },
    'inputs': {
        'in': {
            'database': 'integers_db/1',
            'protocol': 'double',
            'set': 'double',
            'output': 'a',
            'endpoint': 'in',
            'channel': 'integers',
            'path': DB_INDEX_PATH,
            'hash': DB_VIEW_HASH,
        },
    },
    'outputs': {
        'out': {
            'endpoint': 'out',
            'channel': 'integers',
            'path': '20/61/b6/2df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681',
            'hash': '2061b62df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681',
        }
    }
}

CONFIGURATION_DB_AUTONOMOUS = {
    'queue': 'queue',
    'algorithm': 'autonomous/echo/1',
    'nb_slots': 1,
    'channel': 'integers',
    'parameters': {
    },
    'environment': {
        'name': 'Python 2.7',
        'version': '1.2.0'
    },
    'inputs': {
        'in': {
            'database': 'integers_db/1',
            'protocol': 'double',
            'set': 'double',
            'output': 'a',
            'endpoint': 'in',
            'channel': 'integers',
            'path': DB_INDEX_PATH,
            'hash': DB_VIEW_HASH,
        },
    },
    'outputs': {
        'out': {
            'endpoint': 'out',
            'channel': 'integers',
            'path': '20/61/b6/2df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681',
            'hash': '2061b62df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681',
        }
    }
}


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


class TestCreateInputsFromConfiguration_RemoteDatabase(unittest.TestCase):

    def setUp(self, remote=True):
        self.remote = remote

        self.cache_root = tempfile.mkdtemp(prefix=__name__)

        database = Database(prefix, 'integers_db/1')
        view = database.view('double', 'double')

        view.index(os.path.join(self.cache_root, DB_INDEX_PATH))

        self.databases = {}
        self.databases['integers_db/1'] = database

        if remote:
            view.setup(os.path.join(self.cache_root, DB_INDEX_PATH))

            data_sources = {
                'in': view.data_sources['a'],
            }

            self.message_handler = MessageHandler('127.0.0.1', data_sources=data_sources)
            self.message_handler.start()

            self.zmq_context = zmq.Context()
            self.socket = self.zmq_context.socket(zmq.PAIR)
            self.socket.connect(self.message_handler.address)
        else:
            self.message_handler = None
            self.socket = None


    def tearDown(self):
        if self.message_handler is not None:
            self.message_handler.kill()
            self.message_handler.join()
            self.message_handler.destroy()
            self.message_handler = None

        if self.socket is not None:
            self.socket.setsockopt(zmq.LINGER, 0)
            self.socket.close()
            self.zmq_context.destroy()
            self.socket = None
            self.zmq_context = None

        shutil.rmtree(self.cache_root)


    def test_legacy_algorithm(self):
        algorithm = Algorithm(prefix, CONFIGURATION_DB_LEGACY['algorithm'])
        runner = algorithm.runner()

        if self.remote:
            db_access = db_access=AccessMode.REMOTE
        else:
            db_access = db_access=AccessMode.LOCAL

        (input_list, data_loader_list) = \
                create_inputs_from_configuration(CONFIGURATION_DB_LEGACY, algorithm,
                                                 prefix, self.cache_root,
                                                 cache_access=AccessMode.NONE,
                                                 db_access=db_access,
                                                 unpack=True,
                                                 databases=self.databases,
                                                 socket=self.socket,
                                                 no_synchronisation_listeners=False
        )

        self.assertEqual(len(input_list), 1)
        self.assertEqual(len(data_loader_list), 0)


    def test_sequential_algorithm(self):
        algorithm = Algorithm(prefix, CONFIGURATION_DB_SEQUENTIAL['algorithm'])
        runner = algorithm.runner()

        if self.remote:
            db_access = db_access=AccessMode.REMOTE
        else:
            db_access = db_access=AccessMode.LOCAL

        (input_list, data_loader_list) = \
                create_inputs_from_configuration(CONFIGURATION_DB_SEQUENTIAL, algorithm,
                                                 prefix, self.cache_root,
                                                 cache_access=AccessMode.NONE,
                                                 db_access=db_access,
                                                 unpack=True,
                                                 databases=self.databases,
                                                 socket=self.socket,
                                                 no_synchronisation_listeners=False
        )

        self.assertEqual(len(input_list), 1)
        self.assertEqual(len(data_loader_list), 0)


    def test_autonomous_algorithm(self):
        algorithm = Algorithm(prefix, CONFIGURATION_DB_AUTONOMOUS['algorithm'])
        runner = algorithm.runner()

        if self.remote:
            db_access = db_access=AccessMode.REMOTE
        else:
            db_access = db_access=AccessMode.LOCAL

        (input_list, data_loader_list) = \
                create_inputs_from_configuration(CONFIGURATION_DB_AUTONOMOUS, algorithm,
                                                 prefix, self.cache_root,
                                                 cache_access=AccessMode.NONE,
                                                 db_access=db_access,
                                                 unpack=True,
                                                 databases=self.databases,
                                                 socket=self.socket,
                                                 no_synchronisation_listeners=False
        )

        self.assertEqual(len(input_list), 0)
        self.assertEqual(len(data_loader_list), 1)


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


class TestCreateInputsFromConfiguration_LocalDatabase(TestCreateInputsFromConfiguration_RemoteDatabase):

    def setUp(self):
        super(TestCreateInputsFromConfiguration_LocalDatabase, self).setUp(remote=False)