Debian Codegraph @

python/tests/proton_tests/handler.py

raw
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 from __future__ import absolute_import
21
22 import os
23 import gc
24 import traceback
25
26 from proton import *
27 from proton.reactor import Container
28
29 from . import common
30
31 CUSTOM = EventType("custom")
32
33
34 class HandlerTest(common.Test):
35 def test_reactorHandlerCycling(self, n=0):
36
37 class CustomHandler(Handler):
38 UNSET = 999999999
39
40 def __init__(self):
41 self.offset = len(traceback.extract_stack())
42
43 def on_reactor_init(self, event):
44 self.depth = len(traceback.extract_stack())
45
46 def reset(self):
47 self.depth = self.UNSET
48
49 @property
50 def init_depth(self):
51 d = self.depth - self.offset
52 return d
53 custom = CustomHandler()
54
55 container = Container()
56 container.handler = custom
57 for i in range(n):
58 h = container.handler
59 container.handler = h
60 custom.reset()
61 container.run()
62 assert custom.init_depth < 50, "Unexpectedly long traceback for a simple handler"
63
64 def test_reactorHandlerCycling10k(self):
65 self.test_reactorHandlerCycling(10000)
66
67 def test_reactorHandlerCycling100(self):
68 self.test_reactorHandlerCycling(100)
69
70 def do_customEvent(self, reactor_handler, event_root):
71
72 class CustomHandler:
73 did_custom = False
74 did_init = False
75
76 def __init__(self, *handlers):
77 self.handlers = handlers
78
79 def on_reactor_init(self, event):
80 self.did_init = True
81
82 def on_custom(self, event):
83 self.did_custom = True
84
85 class CustomInvoker(CustomHandler):
86 def on_reactor_init(self, event):
87 h = event_root(event)
88 event.dispatch(h, CUSTOM)
89 self.did_init = True
90
91 child = CustomInvoker()
92 root = CustomHandler(child)
93
94 container = Container()
95
96 reactor_handler(container, root)
97 container.run()
98 assert root.did_init
99 assert child.did_init
100 assert root.did_custom
101 assert child.did_custom
102
103 def set_root(self, reactor, root):
104 reactor.handler = root
105
106 def add_root(self, reactor, root):
107 reactor.handler.add(root)
108
109 def append_root(self, reactor, root):
110 reactor.handler.handlers.append(root)
111
112 def event_root(self, event):
113 return event.handler
114
115 def event_reactor_handler(self, event):
116 return event.reactor.handler
117
118 def test_set_handler(self):
119 self.do_customEvent(self.set_root, self.event_reactor_handler)
120
121 def test_add_handler(self):
122 self.do_customEvent(self.add_root, self.event_reactor_handler)
123
124 def test_append_handler(self):
125 self.do_customEvent(self.append_root, self.event_reactor_handler)
126
127 def test_set_root(self):
128 self.do_customEvent(self.set_root, self.event_root)
129
130 def test_add_root(self):
131 self.do_customEvent(self.add_root, self.event_root)
132
133 def test_append_root(self):
134 self.do_customEvent(self.append_root, self.event_root)