Ejemplo n.º 1
0
def test_rebinding_ioloop():
    """ Test recreating server objects, and its binding to the current ioloop.
    """
    
    res = []
    def add_res(i):
        res.append(i)
    
    # Create new ioloop
    loop = IOLoop()
    loop.make_current()
    
    # Create new flexx server, which binds to that loop
    server1 = app.create_server()
    assert server1 is app.current_server()
    #
    assert loop is server1._loop
    
    # Create new ioloop
    loop = IOLoop()
    loop.make_current()
    
    # This is a new loop
    assert loop is not server1._loop
    
    # Create new flexx server, which binds to that loop
    server2 = app.create_server()
    assert server2 is app.current_server()
    assert server1 is not server2
    #
    assert loop is server2._loop
Ejemplo n.º 2
0
def test_rebinding_ioloop():
    """ Test recreating server objects, and its binding to the current ioloop.
    """

    res = []

    def add_res(i):
        res.append(i)

    # Create new ioloop
    loop = IOLoop()
    loop.make_current()

    # Create new flexx server, which binds to that loop
    server1 = app.create_server()
    assert server1 is app.current_server()
    #
    assert loop is server1._loop

    # Create new ioloop
    loop = IOLoop()
    loop.make_current()

    # This is a new loop
    assert loop is not server1._loop

    # Create new flexx server, which binds to that loop
    server2 = app.create_server()
    assert server2 is app.current_server()
    assert server1 is not server2
    #
    assert loop is server2._loop
Ejemplo n.º 3
0
def test_flexx_in_thread1():
    """ Test threading and ioloop selection.
    """
    def main():
        loop2.make_current()
        app.create_server()

    # Create 3 loops, nr 2 is made current in the thread
    loop1 = IOLoop()
    loop2 = IOLoop()
    loop3 = IOLoop()

    loop1.make_current()
    server1 = app.create_server()

    t = threading.Thread(target=main)
    t.start()
    t.join()
    server2 = app.current_server()  # still current as set by the thread

    loop3.make_current()
    server3 = app.create_server()

    assert server1._loop is loop1
    assert server2._loop is loop2
    assert server3._loop is loop3
Ejemplo n.º 4
0
def test_flexx_in_thread1():
    """ Test threading and ioloop selection.
    """
    
    def main():
        loop2.make_current()
        app.create_server()
    
    # Create 3 loops, nr 2 is made current in the thread
    loop1 = IOLoop()
    loop2 = IOLoop()
    loop3 = IOLoop()
    
    loop1.make_current()
    server1 = app.create_server()
    
    t = threading.Thread(target=main)
    t.start()
    t.join()
    server2 = app.current_server()  # still current as set by the thread
    
    loop3.make_current()
    server3 = app.create_server()
    
    assert server1._loop is loop1
    assert server2._loop is loop2
    assert server3._loop is loop3
Ejemplo n.º 5
0
    def init(self):
        with ui.HBox():
            with ui.VBox():
                ui.Label(text='<h3>Server monitor</h3>')
                self.info = ui.Label(text='...')

                if app.current_server().serving[0] == 'localhost':
                    # Don't do this for a public server
                    self.button = ui.Button(text='Do some work')
                    self.button.connect('mouse_down', self._do_work)

                self.cpu_plot = ui.PlotWidget(
                    style='width: 640px; height: 320px;',
                    xdata=[],
                    yrange=(0, 100),
                    ylabel='CPU usage (%)',
                    sync_props=False)
                self.mem_plot = ui.PlotWidget(
                    style='width: 640px; height: 320px;',
                    xdata=[],
                    yrange=(0, 100),
                    ylabel='Mem usage (%)',
                    sync_props=False)
                ui.Widget(flex=1)

        # Relay global info into this app
        relay.connect(self._push_info, 'system_info:' + self.id)
Ejemplo n.º 6
0
def test_flexx_in_thread1():
    """ Test threading and ioloop selection.
    """
    def main():
        asyncio.set_event_loop(loop2)
        app.create_server()

    # Create 3 loops, nr 2 is made current in the thread
    loop1 = asyncio.new_event_loop()
    loop2 = asyncio.new_event_loop()
    loop3 = asyncio.new_event_loop()

    asyncio.set_event_loop(loop1)
    server1 = app.create_server()

    t = threading.Thread(target=main)
    t.start()
    t.join()
    server2 = app.current_server()  # still current as set by the thread

    asyncio.set_event_loop(loop3)
    server3 = app.create_server()

    assert server1._loop is loop1
    assert server2._loop is loop2
    assert server3._loop is loop3
Ejemplo n.º 7
0
def test_flexx_in_thread1():
    """ Test threading and ioloop selection.
    """

    def main():
        asyncio.set_event_loop(loop2)
        app.create_server()

    # Create 3 loops, nr 2 is made current in the thread
    loop1 = asyncio.new_event_loop()
    loop2 = asyncio.new_event_loop()
    loop3 = asyncio.new_event_loop()

    asyncio.set_event_loop(loop1)
    server1 = app.create_server()

    t = threading.Thread(target=main)
    t.start()
    t.join()
    server2 = app.current_server()  # still current as set by the thread

    asyncio.set_event_loop(loop3)
    server3 = app.create_server()

    assert server1._loop is loop1
    assert server2._loop is loop2
    assert server3._loop is loop3
Ejemplo n.º 8
0
    def init(self):

        with ui.VBox():
            ui.Label(html='<h3>Server monitor</h3>')
            if app.current_server().serving[0] == 'localhost':
                # Don't do this for a public server
                self.button = ui.Button(text='Do some work')
            self.view = MonitorView(flex=1)
Ejemplo n.º 9
0
def main():
    if len(sys.argv) > 1:
        app.export(BeetMe, "beetme.html", link=0)
    else:
        app.serve(BeetMe)
        tornado_app = app.current_server().app
        tornado_app.add_handlers(r".*", [
            (r"/beet.*", BeetHandler),
        ])
        app.start()
Ejemplo n.º 10
0
def test_flexx_in_thread3():
    """ Test starting and creating server when a server is currently running.
    """
    res = []

    def main():
        # Create fresh ioloop and make flexx use it
        loop = IOLoop()
        loop.make_current()
        app.create_server()
        app.start()

    def try_start():
        try:
            app.start()
        except RuntimeError:
            res.append('start-fail')

    def try_create():
        try:
            main()
        except RuntimeError:
            res.append('create-fail')

    t = threading.Thread(target=main)
    t.start()

    # With that thread running ...
    while not app.current_server()._running:
        time.sleep(0.01)

    with raises(RuntimeError):
        app.start()

    with raises(RuntimeError):
        app.create_server()

    t1 = threading.Thread(target=try_start)
    t1.start()
    t1.join()

    t2 = threading.Thread(target=try_create)
    t2.start()
    t2.join()

    # Stop
    app.stop()  # Start does not work, but we can stop it!
    t.join()  # Otherwise it would never join

    # Note that we cannot start it right after calling stop, because it wont
    # stop *at once*. We need to join first.

    assert res == ['start-fail', 'create-fail']
Ejemplo n.º 11
0
def test_flexx_in_thread3():
    """ Test starting and creating server when a server is currently running.
    """
    res = []
    
    def main():
        # Create fresh ioloop and make flexx use it
        loop = IOLoop()
        loop.make_current()
        app.create_server()
        app.start()
    
    def try_start():
        try:
            app.start()
        except RuntimeError:
            res.append('start-fail')
    
    def try_create():
        try:
            main()
        except RuntimeError:
            res.append('create-fail')

    t = threading.Thread(target=main)
    t.start()
    
    # With that thread running ...
    while not app.current_server()._running:
        time.sleep(0.01)
    
    with raises(RuntimeError):
        app.start()
    
    with raises(RuntimeError):
        app.create_server()
    
    t1 = threading.Thread(target=try_start)
    t1.start()
    t1.join()
    
    t2 = threading.Thread(target=try_create)
    t2.start()
    t2.join()
    
    # Stop
    app.stop()  # Start does not work, but we can stop it!
    t.join()  # Otherwise it would never join
    
    # Note that we cannot start it right after calling stop, because it wont
    # stop *at once*. We need to join first.
    
    assert res == ['start-fail', 'create-fail']    
Ejemplo n.º 12
0
 def init(self):
     with ui.HBox():
         with ui.VBox():
             ui.Label(text='<h3>Server monitor</h3>')
             self.info = ui.Label(text='...')
             
             if app.current_server().serving[0] == 'localhost':
                 # Don't do this for a public server
                 self.button = ui.Button(text='Do some work')
                 self.button.connect('mouse_down', self._do_work)
             
             self.cpu_plot = ui.PlotWidget(style='width: 640px; height: 320px;',
                                           xdata=[], yrange=(0, 100), 
                                           ylabel='CPU usage (%)',
                                           sync_props=False)
             self.mem_plot = ui.PlotWidget(style='width: 640px; height: 320px;',
                                           xdata=[], yrange=(0, 100), 
                                           ylabel='Mem usage (%)',
                                           sync_props=False)
             ui.Widget(flex=1)
Ejemplo n.º 13
0
def test_add_handlers():
    server = app.current_server()
    tornado_app = server.app
    assert tornado_app.add_handlers
Ejemplo n.º 14
0
def test_add_handlers():
    server = app.current_server()
    tornado_app = server.app
    assert tornado_app.add_handlers
Ejemplo n.º 15
0
app.serve(ChatRoom)


class MyAboutHandler(tornado.web.RequestHandler):
    
    def get(self):
        self.write('<html>This is just an <i>example</i>.</html>')


class MyAPIHandler(tornado.web.RequestHandler):
    
    def get(self, path):
        # self.request.path -> full path
        # path -> the regexp group specified in add_handlers
        self.write('echo ' + path)


# Get a ref to the tornado.web.Application object
tornado_app = app.current_server().app

# Add our handler
tornado_app.add_handlers(r".*", [(r"/about", MyAboutHandler),
                                 (r"/api/(.*)", MyAPIHandler)])

# Note: Tornado tries to match handlers in order, but the handlers
# specified in the constructor come last. Therefore we can easily add
# specific handlers here even though Flexx' main handler is very
# generic.

app.start()