class DieselTest(object): def setup_method(self, *args): self._app = Application(allow_app_replacement=True) self._trigger = TestTrigger() # XXX py.test magic args? def prepare_test(self): return self._app, self._trigger.touch, TestAccumulator() def run_test(self, count=1, timeout=10): def trigger_thread(): self._trigger.wait(timeout, count) try: self._app.halt() except app.ApplicationEnd: # XXX Does halt have to raise this? Should we do anything but # pass? pass self._app.hub.wake_from_other_thread() thread.start_new_thread(trigger_thread, ()) self._app.run() if self._trigger.timed_out: raise TestTimeout() def teardown_method(self, *args): try: self._app.halt() except app.ApplicationEnd: # This is always raised? pass self._app = self._trigger = None
def test_loop_keep_alive_normal_death(): v = [0] def l(): v[0] += 1 def p(): sleep(0.9) WVPASS(v[0] > 1) a.halt() a = Application() a.add_loop(Loop(l), keep_alive=True) a.add_loop(Loop(p)) a.run()
def test_loop_keep_alive_exception(): v = [0] def l(): v[0] += 1 a = b # exception! def p(): sleep(0.9) WVPASS(v[0] > 1) a.halt() a = Application() a.add_loop(Loop(l), keep_alive=True) a.add_loop(Loop(p)) a.run()
def main(): app = Application() app.add_loop(Loop(santa)) elf_do = "meets in study" for i in xrange(10): app.add_loop(Loop(actor("Elf %d" % i, 'elf', elf_group, elf_do, 3, 3))) deer_do = "delivers toys" for name in [ 'Dasher', 'Dancer', 'Prancer', 'Vixen', 'Comet', 'Cupid', 'Donner', 'Blitzen', 'Rudolph', ]: app.add_loop(Loop(actor(name, 'deer', deer_group, deer_do, 9, 9))) app.run()
class DieselTest(object): def setup_method(self, *args): self._app = Application(allow_app_replacement=True) self._trigger = TestTrigger() # XXX py.test magic args? def prepare_test(self): return self._app, self._trigger.touch, TestAccumulator() def run_test(self, count=1, timeout=10): def trigger_thread(): self._trigger.wait(timeout, count) self._app.halt() self._app.hub.wake_from_other_thread() thread.start_new_thread(trigger_thread, ()) self._app.run() if self._trigger.timed_out: raise TestTimeout() def teardown_method(self, *args): self._app.halt() self._app = self._trigger = None
assert r.hexists("h1", "bahbah") == True assert r.hexists("h1", "nope") == False r.hdel("h1", "bahbah") assert r.hexists("h1", "bahbah") == False assert r.hlen("h1") == 3 assert r.hkeys("h1") == set(['foo', 'baz', 'count']) assert set(r.hvals("h1")) == set(['bar', 'bosh', '7']) assert r.hgetall("h1") == {'foo': 'bar', 'baz': 'bosh', 'count': '7'} print 'all tests pass.' a.halt() a.add_loop(Loop(do_set)) a.run() ######################################### ## Hub, an abstraction of sub behavior, etc class RedisSubHub(object): def __init__(self, host='127.0.0.1', port=REDIS_PORT): self.host = host self.port = port self.sub_wake_signal = uuid.uuid4().hex self.sub_adds = [] self.sub_rms = [] self.subs = {} def make_client(self): client = RedisClient(self.host, self.port)
# vim:ts=4:sw=4:expandtab '''The oh-so-canonical "Hello, World!" http server. ''' from diesel import Application, Service from diesel.protocols import http # Pre-gen, since it's static.. content = "Hello, World!" headers = http.HttpHeaders() headers.add('Content-Length', len(content)) headers.add('Content-Type', 'text/plain') def hello_http(req): return http.http_response(req, 200, headers, content) app = Application() app.add_service(Service(http.HttpServer(hello_http), 8088)) app.run()
def free_loop(): global free free += 1 sleep(random.random()) free -= 1 print "FREE", free def sync_loop(): global sync id = random.random() with synchronized(): sync += 1 sleep(random.random()) sync -= 1 print "SYNC", sync def manage(): sleep(10) a.halt() a = Application() for l in (free_loop, sync_loop): for x in xrange(10): a.add_loop(Loop(l)) a.add_loop(Loop(manage)) a.run()
from diesel import Application, Service, Client, Loop, until, call, response def handle_echo(remote_addr): while True: message = yield until('\r\n') yield "you said: %s" % message class EchoClient(Client): @call def echo(self, message): yield message + '\r\n' back = yield until("\r\n") yield response(back) app = Application() def do_echos(): client = EchoClient() yield client.connect('localhost', 8000) t = time.time() for x in xrange(5000): msg = "hello, world #%s!" % x echo_result = yield client.echo(msg) assert echo_result.strip() == "you said: %s" % msg print '5000 loops in %.2fs' % (time.time() - t) app.halt() app.add_service(Service(handle_echo, port=8000)) app.add_loop(Loop(do_echos)) app.run()