PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/conftest.py

https://bitbucket.org/prologic/circuits/
Python | 157 lines | 110 code | 42 blank | 5 comment | 24 complexity | ca2853cecd2a0ee939a4389b6a972a7b MD5 | raw file
  1# Module:   conftest
  2# Date:     6th December 2010
  3# Author:   James Mills, prologic at shortcircuit dot net dot au
  4
  5"""py.test config"""
  6
  7import pytest
  8
  9import sys
 10import threading
 11import collections
 12from time import sleep
 13from collections import deque
 14
 15from circuits.core.manager import TIMEOUT
 16from circuits import handler, BaseComponent, Debugger, Manager
 17
 18
 19class Watcher(BaseComponent):
 20
 21    def init(self):
 22        self._lock = threading.Lock()
 23        self.events = deque()
 24
 25    @handler(channel="*", priority=999.9)
 26    def _on_event(self, event, *args, **kwargs):
 27        with self._lock:
 28            self.events.append(event)
 29
 30    def clear(self):
 31        self.events.clear()
 32
 33    def wait(self, name, channel=None, timeout=6.0):
 34        try:
 35            for i in range(int(timeout / TIMEOUT)):
 36                if channel is None:
 37                    with self._lock:
 38                        for event in self.events:
 39                            if event.name == name:
 40                                return True
 41                else:
 42                    with self._lock:
 43                        for event in self.events:
 44                            if event.name == name and \
 45                                    channel in event.channels:
 46                                return True
 47
 48                sleep(TIMEOUT)
 49        finally:
 50            pass
 51            #self.events.clear()
 52
 53
 54class Flag(object):
 55    status = False
 56
 57
 58def call_event_from_name(manager, event, event_name, *channels):
 59    fired = False
 60    value = None
 61    for r in manager.waitEvent(event_name):
 62        if not fired:
 63            fired = True
 64            value = manager.fire(event, *channels)
 65        sleep(0.1)
 66    return value
 67
 68
 69def call_event(manager, event, *channels):
 70    return call_event_from_name(manager, event, event.name, *channels)
 71
 72
 73class WaitEvent(object):
 74
 75    def __init__(self, manager, name, channel=None, timeout=6.0):
 76        if channel is None:
 77            channel = getattr(manager, "channel", None)
 78
 79        self.timeout = timeout
 80        self.manager = manager
 81
 82        flag = Flag()
 83
 84        @handler(name, channel=channel)
 85        def on_event(self, *args, **kwargs):
 86            flag.status = True
 87
 88        self.handler = self.manager.addHandler(on_event)
 89        self.flag = flag
 90
 91    def wait(self):
 92        try:
 93            for i in range(int(self.timeout / TIMEOUT)):
 94                if self.flag.status:
 95                    return True
 96                sleep(TIMEOUT)
 97        finally:
 98            self.manager.removeHandler(self.handler)
 99
100
101def wait_for(obj, attr, value=True, timeout=3.0):
102    from circuits.core.manager import TIMEOUT
103    for i in range(int(timeout / TIMEOUT)):
104        if isinstance(value, collections.Callable):
105            if value(obj, attr):
106                return True
107        elif getattr(obj, attr) == value:
108            return True
109        sleep(TIMEOUT)
110
111
112@pytest.fixture(scope="session")
113def manager(request):
114    manager = Manager()
115
116    def finalizer():
117        manager.stop()
118
119    request.addfinalizer(finalizer)
120
121    waiter = WaitEvent(manager, "started")
122    manager.start()
123    assert waiter.wait()
124
125    if request.config.option.verbose:
126        verbose = True
127    else:
128        verbose = False
129
130    Debugger(events=verbose).register(manager)
131
132    return manager
133
134
135@pytest.fixture
136def watcher(request, manager):
137    watcher = Watcher().register(manager)
138
139    def finalizer():
140        waiter = WaitEvent(manager, "unregistered")
141        watcher.unregister()
142        waiter.wait()
143
144    request.addfinalizer(finalizer)
145
146    return watcher
147
148
149def pytest_namespace():
150    return dict((
151        ("WaitEvent", WaitEvent),
152        ("wait_for", wait_for),
153        ("call_event", call_event),
154        ("PLATFORM", sys.platform),
155        ("PYVER", sys.version_info[:3]),
156        ("call_event_from_name", call_event_from_name),
157    ))