Skip to content

Commit

Permalink
fix: predicate callable stores method object to prevent garbage colle…
Browse files Browse the repository at this point in the history
…ction, leading to the predicate method being removed
  • Loading branch information
r3w0p committed Oct 20, 2019
1 parent 4b43628 commit 0e610b5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bobocep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__author__ = """r3w0p"""
__email__ = ''
__version__ = '0.33.0'
__version__ = '0.33.1'
2 changes: 2 additions & 0 deletions bobocep/rules/predicates/bobo_predicate_callable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from inspect import isfunction, signature, ismethod
from types import MethodType
from typing import Callable, List

from bobocep.rules.events.bobo_event import BoboEvent
Expand Down Expand Up @@ -33,6 +34,7 @@ def __init__(self, call: Callable) -> None:
"in its signature.".format(self.PARAMETERS))

self._call = call
self._obj = call.__self__ if isinstance(call, MethodType) else None

def evaluate(self,
event: BoboEvent,
Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
# built documents.
#
# The short X.Y version.
version = '0.33.0'
version = '0.33.1'
# The full version, including alpha/beta/rc tags.
release = '0.33.0'
release = '0.33.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.33.0
current_version = 0.33.1
tag = True

[bumpversion:file:setup.py]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
author="r3w0p",
author_email='',
name='bobocep',
version='0.33.0',
version='0.33.1',
description="A fault-tolerant complex event processing engine designed "
"for edge computing in IoT systems.",
long_description=long_description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ def state_from_layer(nfa, label):
lambda x: x.startswith(label), nfa.states.keys()))[0]]


class StubPredicateClass:

def __init__(self) -> None:
super().__init__()

def predicate_true_1(self,
event: BoboEvent,
history: BoboHistory,
recents: List[BoboEvent]):
return True

def predicate_true_2(self,
event: BoboEvent,
history: BoboHistory,
recents: List[BoboEvent]):
return True

def predicate_true_3(self,
event: BoboEvent,
history: BoboHistory,
recents: List[BoboEvent]):
return True


class NFAHandlerSubscriber(INFAHandlerSubscriber):

def __init__(self) -> None:
Expand Down Expand Up @@ -967,3 +991,27 @@ def test_force_run_final_invalid_run(self):
with self.assertRaises(RuntimeError):
handler.force_run_final(run_id=RUN_ID_INVALID,
history=BoboHistory())


class TestPredicateCallable(unittest.TestCase):

def test_all_callables_method_same_object(self):
obj = StubPredicateClass()

pattern = BoboPattern() \
.followed_by(LABEL_LAYER_A,
BoboPredicateCallable(obj.predicate_true_1)) \
.followed_by(LABEL_LAYER_B,
BoboPredicateCallable(obj.predicate_true_2)) \
.followed_by(LABEL_LAYER_C,
BoboPredicateCallable(obj.predicate_true_3))

nfa, buffer, handler, handlersub = handler_setup(
nfa_name=NFA_NAME_A,
pattern=pattern)

handler.process(event_a)
handler.process(event_b)
handler.process(event_c)

self.assertEqual(1, len(handlersub.final))

0 comments on commit 0e610b5

Please sign in to comment.