Example #1
0
    def test_waiters_get_woken (self):
        # verify that when there's someone waiting on an empty pool
        # and someone puts an immediately-closed connection back in
        # the pool that the waiter gets woken
        self.pool.put(self.connection)
        self.pool.clear()
        self.pool = self.create_pool(max_size = 1, max_age = 0)

        self.connection = self.pool.get()
        self.assertEquals(self.pool.free(), 0)
        self.assertEquals(self.pool.waiting(), 0)
        e = event.Event()

        def retrieve (pool, ev):
            c = pool.get()
            ev.send(c)

        evy.spawn(retrieve, self.pool, e)
        sleep(0) # these two sleeps should advance the retrieve
        sleep(0) # coroutine until it's waiting in get()
        self.assertEquals(self.pool.free(), 0)
        self.assertEquals(self.pool.waiting(), 1)
        self.pool.put(self.connection)
        timer = Timeout(1)
        conn = e.wait()
        timer.cancel()
        self.assertEquals(self.pool.free(), 0)
        self.assertEquals(self.pool.waiting(), 0)
        self.pool.put(conn)
Example #2
0
    def test_waiters_get_woken(self):
        # verify that when there's someone waiting on an empty pool
        # and someone puts an immediately-closed connection back in
        # the pool that the waiter gets woken
        self.pool.put(self.connection)
        self.pool.clear()
        self.pool = self.create_pool(max_size=1, max_age=0)

        self.connection = self.pool.get()
        self.assertEquals(self.pool.free(), 0)
        self.assertEquals(self.pool.waiting(), 0)
        e = event.Event()

        def retrieve(pool, ev):
            c = pool.get()
            ev.send(c)

        evy.spawn(retrieve, self.pool, e)
        sleep(0)  # these two sleeps should advance the retrieve
        sleep(0)  # coroutine until it's waiting in get()
        self.assertEquals(self.pool.free(), 0)
        self.assertEquals(self.pool.waiting(), 1)
        self.pool.put(self.connection)
        timer = Timeout(1)
        conn = e.wait()
        timer.cancel()
        self.assertEquals(self.pool.free(), 0)
        self.assertEquals(self.pool.waiting(), 0)
        self.pool.put(conn)
Example #3
0
def publish(writer):
    print "connected"
    socket = ctx.socket(zmq.SUB)

    socket.setsockopt(zmq.SUBSCRIBE, "")
    socket.connect(ADDR)
    evy.sleep(0.1)

    while True:
        msg = socket.recv_pyobj()
        str_msg = "%s: %s" % msg
        writer.write(str_msg)
        writer.flush()
Example #4
0
def publish (writer):
    print "connected"
    socket = ctx.socket(zmq.SUB)

    socket.setsockopt(zmq.SUBSCRIBE, "")
    socket.connect(ADDR)
    evy.sleep(0.1)

    while True:
        msg = socket.recv_pyobj()
        str_msg = "%s: %s" % msg
        writer.write(str_msg)
        writer.flush()
Example #5
0
def handle(ws):
    """  This is the websocket handler function.  Note that we 
    can dispatch based on path in here, too."""
    if ws.path == "/echo":
        while True:
            m = ws.wait()
            if m is None:
                break
            ws.send(m)

    elif ws.path == "/data":
        for i in xrange(10000):
            ws.send("0 %s %s\n" % (i, random.random()))
            evy.sleep(0.1)
Example #6
0
    def test_max_idle_many (self):
        # This test is timing-sensitive.  Rename the function without
        # the "dont" to run it, but beware that it could fail or take
        # a while.

        self.pool = self.create_pool(max_size = 2, max_idle = 0.02)
        self.connection, conn2 = self.pool.get(), self.pool.get()
        self.connection.close()
        sleep(0.01)
        self.assertEquals(len(self.pool.free_items), 1)
        conn2.close()
        self.assertEquals(len(self.pool.free_items), 2)
        sleep(0.02)  # trigger cleanup of conn1 but not conn2
        self.assertEquals(len(self.pool.free_items), 1)
Example #7
0
    def test_max_idle_many(self):
        # This test is timing-sensitive.  Rename the function without
        # the "dont" to run it, but beware that it could fail or take
        # a while.

        self.pool = self.create_pool(max_size=2, max_idle=0.02)
        self.connection, conn2 = self.pool.get(), self.pool.get()
        self.connection.close()
        sleep(0.01)
        self.assertEquals(len(self.pool.free_items), 1)
        conn2.close()
        self.assertEquals(len(self.pool.free_items), 2)
        sleep(0.02)  # trigger cleanup of conn1 but not conn2
        self.assertEquals(len(self.pool.free_items), 1)
Example #8
0
    def test_max_age_many (self):
        # This test is timing-sensitive.  Rename the function without
        # the "dont" to run it, but beware that it could fail or take
        # a while.

        self.pool = self.create_pool(max_size = 2, max_age = 0.15)
        self.connection, conn2 = self.pool.get(), self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0)  # not long enough to trigger the age timeout
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.2) # long enough to trigger age timeout
        self.assertEquals(len(self.pool.free_items), 0)
        conn2.close()  # should not be added to the free items
        self.assertEquals(len(self.pool.free_items), 0)
Example #9
0
    def test_max_age_many(self):
        # This test is timing-sensitive.  Rename the function without
        # the "dont" to run it, but beware that it could fail or take
        # a while.

        self.pool = self.create_pool(max_size=2, max_age=0.15)
        self.connection, conn2 = self.pool.get(), self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0)  # not long enough to trigger the age timeout
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.2)  # long enough to trigger age timeout
        self.assertEquals(len(self.pool.free_items), 0)
        conn2.close()  # should not be added to the free items
        self.assertEquals(len(self.pool.free_items), 0)
Example #10
0
 def wait (self, check_interval = 0.01):
     # Instead of a blocking OS call, this version of wait() uses logic
     # borrowed from the evy 0.2 processes.Process.wait() method.
     try:
         while True:
             status = self.poll()
             if status is not None:
                 return status
             evy.sleep(check_interval)
     except OSError, e:
         if e.errno == errno.ECHILD:
             # no child process, this happens if the child process
             # already died and has been cleaned up
             return -1
         else:
             raise
def handle(ws):
    global pub_socket
    name_id = participants[ws]
    ws.send("Connected as %s, change name with 'name: new_name'" % name_id)
    try:
        while True:
            m = ws.wait()
            if m is None:
                break
            if m.startswith('name:'):
                old_name = str(name_id)
                new_name = m.split(':', 1)[1].strip()
                name_id.name = new_name
                m = 'Changed name from %s' % old_name
            pub_socket.send_pyobj(name_id.pack_message(m))
            sleep()
    finally:
        del participants[ws]
Example #12
0
def handle (ws):
    global pub_socket
    name_id = participants[ws]
    ws.send("Connected as %s, change name with 'name: new_name'" % name_id)
    try:
        while True:
            m = ws.wait()
            if m is None:
                break
            if m.startswith('name:'):
                old_name = str(name_id)
                new_name = m.split(':', 1)[1].strip()
                name_id.name = new_name
                m = 'Changed name from %s' % old_name
            pub_socket.send_pyobj(name_id.pack_message(m))
            sleep()
    finally:
        del participants[ws]
Example #13
0
    def test_schedule (self):
        hub = hubs.get_hub()
        # clean up the runloop, preventing side effects from previous tests
        # on this thread
        if hub.running:
            hub.abort()
            sleep(0)
        called = []

        #t = timer.Timer(0, lambda: (called.append(True), hub.abort()))
        #t.schedule()
        # let's have a timer somewhere in the future; make sure abort() still works
        #hubs.get_hub().schedule_call_global(10000, lambda: (called.append(True), hub.abort()))

        hubs.get_hub().run_callback(lambda: (called.append(True), hub.abort()))
        hub.default_sleep = lambda: 0.0
        hub.switch()

        assert called
        assert not hub.running
Example #14
0
    def test_schedule(self):
        hub = hubs.get_hub()
        # clean up the runloop, preventing side effects from previous tests
        # on this thread
        if hub.running:
            hub.abort()
            sleep(0)
        called = []

        #t = timer.Timer(0, lambda: (called.append(True), hub.abort()))
        #t.schedule()
        # let's have a timer somewhere in the future; make sure abort() still works
        #hubs.get_hub().schedule_call_global(10000, lambda: (called.append(True), hub.abort()))

        hubs.get_hub().run_callback(lambda: (called.append(True), hub.abort()))
        hub.default_sleep = lambda: 0.0
        hub.switch()

        assert called
        assert not hub.running
Example #15
0
    def test_max_idle (self):
        # This test is timing-sensitive.  Rename the function without
        # the "dont" to run it, but beware that it could fail or take
        # a while.

        self.pool = self.create_pool(max_size = 2, max_idle = 0.02)
        self.connection = self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.01)  # not long enough to trigger the idle timeout
        self.assertEquals(len(self.pool.free_items), 1)
        self.connection = self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.01)  # idle timeout should have fired but done nothing
        self.assertEquals(len(self.pool.free_items), 1)
        self.connection = self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.03) # long enough to trigger idle timeout for real
        self.assertEquals(len(self.pool.free_items), 0)
Example #16
0
    def test_max_idle(self):
        # This test is timing-sensitive.  Rename the function without
        # the "dont" to run it, but beware that it could fail or take
        # a while.

        self.pool = self.create_pool(max_size=2, max_idle=0.02)
        self.connection = self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.01)  # not long enough to trigger the idle timeout
        self.assertEquals(len(self.pool.free_items), 1)
        self.connection = self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.01)  # idle timeout should have fired but done nothing
        self.assertEquals(len(self.pool.free_items), 1)
        self.connection = self.pool.get()
        self.connection.close()
        self.assertEquals(len(self.pool.free_items), 1)
        sleep(0.03)  # long enough to trigger idle timeout for real
        self.assertEquals(len(self.pool.free_items), 0)
Example #17
0
def cleanup ():
    evy.sleep(0.2)