[Telepathy-commits] [telepathy-gabble/master] Support for waiting for events that might have already happened and predicate functions to decide if we have a match.
Senko Rasic
senko at phyrexia.lan
Tue Dec 2 04:34:00 PST 2008
---
tests/twisted/servicetest.py | 29 ++++++++++++++++++++++++++++-
1 files changed, 28 insertions(+), 1 deletions(-)
diff --git a/tests/twisted/servicetest.py b/tests/twisted/servicetest.py
index a700630..e63235e 100644
--- a/tests/twisted/servicetest.py
+++ b/tests/twisted/servicetest.py
@@ -174,6 +174,10 @@ class EventTest:
class EventPattern:
def __init__(self, type, **properties):
self.type = type
+ self.predicate = lambda x: True
+ if 'predicate' in properties:
+ self.predicate = properties['predicate']
+ del properties['predicate']
self.properties = properties
def match(self, event):
@@ -187,7 +191,11 @@ class EventPattern:
except AttributeError:
return False
- return True
+ if self.predicate(event):
+ return True
+
+ return False
+
class TimeoutError(Exception):
pass
@@ -200,6 +208,7 @@ class BaseEventQueue:
def __init__(self, timeout=None):
self.verbose = False
+ self.past_events = []
if timeout is None:
self.timeout = 5
@@ -210,6 +219,22 @@ class BaseEventQueue:
if self.verbose:
print s
+ def flush_past_events(self):
+ self.past_events = []
+
+ def expect_racy(self, type, **kw):
+ pattern = EventPattern(type, **kw)
+
+ for event in self.past_events:
+ if pattern.match(event):
+ self.log('past event handled')
+ map(self.log, format_event(event))
+ self.log('')
+ self.past_events.remove(event)
+ return event
+
+ return self.expect(type, **kw)
+
def expect(self, type, **kw):
pattern = EventPattern(type, **kw)
@@ -223,6 +248,7 @@ class BaseEventQueue:
self.log('')
return event
+ self.past_events.append(event)
self.log('not handled')
self.log('')
@@ -241,6 +267,7 @@ class BaseEventQueue:
ret[i] = event
break
else:
+ self.past_events.append(event)
self.log('not handled')
self.log('')
--
1.5.6.5
More information about the Telepathy-commits
mailing list