~vpec/maus/tof_calib_read

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
"""
Test class for mauscelery.mausprocess module.
"""

#  This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
# 
#  MAUS 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 3 of the License, or
#  (at your option) any later version.
# 
#  MAUS 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 MAUS.  If not, see <http://www.gnu.org/licenses/>.

import logging
import os
import unittest
import json

import maus_cpp.globals
import Configuration
import mauscelery.mausprocess
from framework.workers import WorkerBirthFailedException
from mauscelery.state import MausConfiguration
from mauscelery.state import MausTransform
from mauscelery.mausprocess import worker_process_init_callback
from mauscelery.mausprocess import process_birth
from mauscelery.mausprocess import process_death
from MapPyTestMap import MapPyTestMap

DEFAULT_CONFIG = Configuration.Configuration().getConfigJSON()
def init_logging():
    """
    Set logging to verbose mode if I am debugging
    """
    logging.basicConfig()
    logger = logging.getLogger(mauscelery.mausprocess.__name__)
    logger.setLevel(logging.DEBUG)
    logger = logging.getLogger(mauscelery.state.__name__)
    logger.setLevel(logging.DEBUG)
    logger = logging.getLogger(MausTransform.__name__)
    logger.setLevel(logging.DEBUG)
    logger = logging.getLogger(MausConfiguration.__name__)
    logger.setLevel(logging.DEBUG)

class ProcessInitialiseTestCase(unittest.TestCase): # pylint: disable=R0904, C0301
    """
    Test class for mauscelery.mausprocess.worker_process_init_callback 
    method.
    """

    def setUp(self): # pylint: disable=C0103
        """ 
        Reset MausTransform and set logging.
        @param self Object reference.
        """
        MausTransform.transform = None
        MausTransform.is_dead = True
        MausConfiguration.transform = "MapPyTestMap"
        # Configure lowest logging level so all logging statements
        # are executed.

    def test_callback_no_globals(self):
        """ 
        Invoke worker_process_init_callback - check globals are not created with
        empty configuration
        """
        MausConfiguration.configuration = "{}"
        if maus_cpp.globals.has_instance():
            maus_cpp.globals.death()
        # doesnt initialise globals with configuration {}
        self.assertRaises(WorkerBirthFailedException,
                          worker_process_init_callback)
        self.assertFalse(maus_cpp.globals.has_instance())
        self.assertEquals(MapPyTestMap, MausTransform.transform.__class__)
        
    def test_process_defaults(self):
        """ 
        Check worker_process_init_callback constructs globals okay
        """
        MausConfiguration.configuration = DEFAULT_CONFIG
        # initialises globals with reasonable configuration
        worker_process_init_callback()
        # reinitialised globals
        worker_process_init_callback()
        self.assertTrue(maus_cpp.globals.has_instance())
        self.assertEquals(MapPyTestMap, MausTransform.transform.__class__)

class ProcessBirthTestCase(unittest.TestCase): # pylint: disable=R0904, C0301
    """
    Test class for mauscelery.mausprocess.process_birth method. 
    """

    def setUp(self): # pylint: disable=C0103
        """ 
        Reset MausTransform and set logging.
        @param self Object reference.
        """
        MausConfiguration.configuration = DEFAULT_CONFIG
        worker_process_init_callback()
        MausTransform.transform = None
        MausTransform.is_dead = True
        # Configure lowest logging level so all logging statements
        # are executed.
 
    def test_process_birth_same_id(self):
        """ 
        Invoke process_birth with a config ID that matches
        the current ID (in MausConfiguration.config_id).
        @param self Object reference.
        """
        status = process_birth(set(), MausConfiguration.config_id, \
            "MapPyTestMap", DEFAULT_CONFIG, 0)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID")
        self.assertEquals(None, status[1], "Expected None")

    def test_process_birth(self):
        """ 
        Invoke process_birth.
        @param self Object reference.
        """
        new_id = MausConfiguration.config_id + 1
        status = process_birth(set(), new_id, "MapPyTestMap", DEFAULT_CONFIG, 9)
        self.assertEquals(MapPyTestMap, \
            MausTransform.transform.__class__, "Unexpected transform")
        self.assertTrue(not MausTransform.is_dead, 
            "Expected is_dead to be False")
        self.assertEquals("MapPyTestMap", MausConfiguration.transform,
            "Unexpected MausConfiguration.transform")
        self.assertEquals(DEFAULT_CONFIG, MausConfiguration.configuration,
            "Unexpected MausConfiguration.configuration")
        self.assertEquals(new_id, MausConfiguration.config_id,
            "Unexpected MausConfiguration.config_id")
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID")
        self.assertEquals(None, status[1], "Expected None")
        self.assertEquals(9, json.loads(status[2])["run_number"])

    def test_process_birth_fails(self):
        """ 
        Invoke process_birth where birth throws a
      . WorkerBirthFailedException.
        @param self Object reference.
        """
        status = process_birth(set(), MausConfiguration.config_id + 1, 
            "MapPyTestMap", """{"birth_result":%s}""" % \
            MapPyTestMap.FAIL, 0)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID")
        self.assertTrue(status[1].has_key("error"),
            "Expect an error key")
        self.assertTrue(status[1].has_key("message"),
            "Expect a message key")

    def test_process_birth_exception(self):
        """ 
        Invoke process_birth where birth throws an exception.
        @param self Object reference.
        """
        status = process_birth(set(), MausConfiguration.config_id + 1, 
            "MapPyTestMap", """{"birth_result":%s}""" % \
            MapPyTestMap.EXCEPTION, 0)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID")
        self.assertTrue(status[1].has_key("error"),
            "Expect an error key")
        self.assertTrue(status[1].has_key("message"),
            "Expect a message key")

    def test_process_death_fails(self):
        """ 
        Invoke process_birth where death of existing transform throws
        an exception.
        @param self Object reference.
        """
        process_birth(set(), MausConfiguration.config_id + 1, 
            "MapPyTestMap", """{"death_result":%s}""" % \
            MapPyTestMap.EXCEPTION, 0)
        # Now try knowing that death will fail.
        status = process_birth(set(), MausConfiguration.config_id + 2, 
            "MapPyTestMap", DEFAULT_CONFIG, 0)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID")
        self.assertTrue(status[1].has_key("error"),
            "Expect an error key")
        self.assertTrue(status[1].has_key("message"),
            "Expect a message key")
        self.assertTrue(MausTransform.is_dead, 
            "Expected is_dead to be True")
        # Now try again - should be fine.
        status = process_birth(set(), MausConfiguration.config_id + 3, 
            "MapPyDoNothing", DEFAULT_CONFIG, 0)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID on subsequent birth call")
        self.assertEquals(None, status[1], 
            "Unexpected status value on subsequent birth call")
        self.assertTrue(not MausTransform.is_dead, 
            "Expected is_dead to be False after subsequent birth call")
        self.assertEquals("MapPyDoNothing", MausConfiguration.transform,
            "Unexpected MausConfiguration.transform")

class ProcessDeathTestCase(unittest.TestCase): # pylint: disable=R0904, C0301
    """
    Test class for mauscelery.mausprocess.process_death method. 
    """

    def setUp(self): # pylint: disable=C0103
        """ 
        Reset MausTransform and set logging.
        @param self Object reference.
        """
        MausTransform.transform = None
        MausTransform.is_dead = True
        MausConfiguration.configuration = DEFAULT_CONFIG
        worker_process_init_callback() # start the worker
        # Configure lowest logging level so all logging statements
        # are executed.
        logger = logging.getLogger(mauscelery.mausprocess.__name__)
        logger.setLevel(logging.DEBUG)
        logger = logging.getLogger(mauscelery.state.__name__)
        logger.setLevel(logging.DEBUG)
 
    def test_process_death(self):
        """ 
        Invoke process_death.
        @param self Object reference.
        """
        process_birth(set(), MausConfiguration.config_id + 1, 
            "MapPyTestMap", DEFAULT_CONFIG, -1)
        status = process_death(set(), -69)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID")
        self.assertEquals(None, status[1],
            "Unexpected status value")
        self.assertEquals(-69, json.loads(status[2])["run_number"])
        self.assertTrue(MausTransform.is_dead, 
            "Expected is_dead to be True")
        # Expect same again. Doesn't call death()/end_of_run() twice
        status = process_death(set(), -69)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID on subseqent death call")
        self.assertEquals(None, status[1],
            "Unexpected status value on subsequent death call")
        self.assertEquals('', status[2],
            "Unexpected run_number on subsequent death call "+status[2])
        self.assertTrue(MausTransform.is_dead, 
            "Expected is_dead to be True after subsequent death call")

    def test_process_death_exception(self):
        """ 
        Invoke process_death where death throws an exception.
        @param self Object reference.
        """
        
        status = process_birth(set(), MausConfiguration.config_id + 1, 
            "MapPyTestMap", """{"death_result":%s}""" % \
            MapPyTestMap.EXCEPTION, 0)
        status = process_death(set(), 0)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID")
        self.assertTrue(status[1].has_key("error"),
            "Expect an error key")
        self.assertTrue(status[1].has_key("message"),
            "Expect a message key")
        # Expect subsequent attempt to succeed.
        status = process_death(set(), 0)
        self.assertEquals(os.getpid(), status[0], 
            "Unexpected process ID on subseqent death call")
        self.assertEquals(None, status[1],
            "Unexpected status value on subsequent death call")
        self.assertTrue(MausTransform.is_dead, 
            "Expected is_dead to be True after subsequent death call")

if __name__ == '__main__':
    #init_logging()
    unittest.main()