This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_event.py
90 lines (74 loc) · 2.96 KB
/
test_event.py
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
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import absolute_import
import mock
import pytest
from mock import MagicMock
from tornado.gen import maybe_future
from tchannel import TChannel
from tchannel import schemes
from tchannel.errors import BadRequestError
from tchannel.event import EventEmitter
from tchannel.event import EventHook
from tchannel.event import EventRegistrar
from tchannel.event import EventType
@pytest.mark.parametrize('event_name', EventType._fields)
@pytest.mark.gen_test
def test_event_hook(event_name):
event_value = getattr(EventType, event_name)
mock_hook = MagicMock(spec=EventHook)
event_emitter = EventEmitter()
event_emitter.register_hook(mock_hook)
yield event_emitter.fire(event_value, None)
assert getattr(mock_hook, event_name).called is True
@pytest.mark.gen_test
def test_decorator_registration():
event_emitter = EventEmitter()
registrar = EventRegistrar(event_emitter)
called = [False, False]
# Multiple handlers can be defined for the same event.
@registrar.before_send_request
def foo():
called[0] = True
@registrar.before_send_request
def bar():
called[1] = True
yield event_emitter.fire(EventType.before_send_request)
assert called[0] is True
assert called[1] is True
@pytest.mark.gen_test
def test_after_send_error_event_called():
tchannel = TChannel('test')
tchannel.listen()
with mock.patch(
'tchannel.event.EventEmitter.fire', autospec=True,
) as mock_fire:
mock_fire.return_value = maybe_future(None)
with pytest.raises(BadRequestError):
yield tchannel.call(
scheme=schemes.RAW,
service='test',
arg1='endpoint',
hostport=tchannel.hostport,
timeout=0.3,
)
mock_fire.assert_any_call(
mock.ANY, EventType.after_send_error, mock.ANY,
)