| 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 |
""" |
| 21 |
PROTON-1709 [python] ApplicationEvent causing memory growth |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import absolute_import |
| 25 |
from __future__ import division |
| 26 |
from __future__ import print_function |
| 27 |
|
| 28 |
import gc |
| 29 |
import platform |
| 30 |
import threading |
| 31 |
import unittest |
| 32 |
|
| 33 |
import proton |
| 34 |
from proton.handlers import MessagingHandler |
| 35 |
from proton.reactor import Container, ApplicationEvent, EventInjector |
| 36 |
|
| 37 |
|
| 38 |
class Program(MessagingHandler): |
| 39 |
def __init__(self, injector): |
| 40 |
self.injector = injector |
| 41 |
self.counter = 0 |
| 42 |
self.on_start_ = threading.Event() |
| 43 |
|
| 44 |
def on_reactor_init(self, event): |
| 45 |
event.reactor.selectable(self.injector) |
| 46 |
self.on_start_.set() |
| 47 |
|
| 48 |
def on_count_up(self, event): |
| 49 |
self.counter += 1 |
| 50 |
gc.collect() |
| 51 |
|
| 52 |
def on_done(self, event): |
| 53 |
event.subject.stop() |
| 54 |
|
| 55 |
|
| 56 |
class Proton1709Test(unittest.TestCase): |
| 57 |
@unittest.skipIf(platform.system() == 'Windows', "TODO jdanek: Test is broken on Windows") |
| 58 |
def test_application_event_no_object_leaks(self): |
| 59 |
event_types_count = len(proton.EventType.TYPES) |
| 60 |
|
| 61 |
injector = EventInjector() |
| 62 |
p = Program(injector) |
| 63 |
c = Container(p) |
| 64 |
t = threading.Thread(target=c.run) |
| 65 |
t.start() |
| 66 |
|
| 67 |
p.on_start_.wait() |
| 68 |
|
| 69 |
object_counts = [] |
| 70 |
|
| 71 |
gc.collect() |
| 72 |
object_counts.append(len(gc.get_objects())) |
| 73 |
|
| 74 |
for i in range(100): |
| 75 |
injector.trigger(ApplicationEvent("count_up")) |
| 76 |
|
| 77 |
gc.collect() |
| 78 |
object_counts.append(len(gc.get_objects())) |
| 79 |
|
| 80 |
self.assertEqual(len(proton.EventType.TYPES), event_types_count + 1) |
| 81 |
|
| 82 |
injector.trigger(ApplicationEvent("done", subject=c)) |
| 83 |
self.assertEqual(len(proton.EventType.TYPES), event_types_count + 2) |
| 84 |
|
| 85 |
t.join() |
| 86 |
|
| 87 |
gc.collect() |
| 88 |
object_counts.append(len(gc.get_objects())) |
| 89 |
|
| 90 |
self.assertEqual(p.counter, 100) |
| 91 |
|
| 92 |
self.assertTrue(object_counts[1] - object_counts[0] <= 220, |
| 93 |
"Object counts should not be increasing too fast: {0}".format(object_counts)) |
| 94 |
self.assertTrue(object_counts[2] - object_counts[0] <= 10, |
| 95 |
"No objects should be leaking at the end: {0}".format(object_counts)) |