aboutsummaryrefslogtreecommitdiff
path: root/tests/test_signal.py
blob: 48a14b15023388c7d7af4deb7e9332e317bb6976 (plain)
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
#    Copyright 2018 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import logging
import unittest

from nose.tools import assert_equal, assert_true, assert_false

import wa.framework.signal as signal


class Callable(object):

    def __init__(self, val):
        self.val = val

    def __call__(self):
        return self.val


class TestPriorityDispatcher(unittest.TestCase):

    def setUp(self):
        # Stop logger output interfering with nose output in the console.
        logger = logging.getLogger('signal')
        logger.setLevel(logging.CRITICAL)

    def test_ConnectNotify(self):
        one = Callable(1)
        two = Callable(2)
        three = Callable(3)
        signal.connect(
            two,
            'test',
            priority=200
        )
        signal.connect(
            one,
            'test',
            priority=100
        )
        signal.connect(
            three,
            'test',
            priority=300
        )
        result = [i[1] for i in signal.send('test')]
        assert_equal(result, [3, 2, 1])

    def test_wrap_propagate(self):
        d = {'before': False, 'after': False, 'success': False}
        def before():
            d['before'] = True
        def after():
            d['after'] = True
        def success():
            d['success'] = True
        signal.connect(before, signal.BEFORE_WORKLOAD_SETUP)
        signal.connect(after, signal.AFTER_WORKLOAD_SETUP)
        signal.connect(success, signal.SUCCESSFUL_WORKLOAD_SETUP)

        caught = False
        try:
            with signal.wrap('WORKLOAD_SETUP'):
                raise RuntimeError()
        except RuntimeError:
            caught=True

        assert_true(d['before'])
        assert_true(d['after'])
        assert_true(caught)
        assert_false(d['success'])