Beispiel #1
0
 def try_start():
     try:
         app.stop()
         app.start()
     except RuntimeError:
         res.append('start-fail')
     else:
         res.append('start-ok')
Beispiel #2
0
 def try_start():
     try:
         app.stop()
         app.start()
     except RuntimeError:
         res.append('start-fail')
     else:
         res.append('start-ok')
Beispiel #3
0
def test_serving_apps_at_output_message():
    """ Test for 'Serving apps at' ready signal.
    """
    with capture_log('info') as log:
        server = app.create_server()
        app.stop()  # triggers event to stop
        app.start()

    assert 'Serving apps at' in ''.join(log)
Beispiel #4
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']
Beispiel #5
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']    
Beispiel #6
0
 def _done(self, v):
     self._result = v
     #print('done', v)
     app.stop()
Beispiel #7
0
 def stop():
     if isrunning:
         app.stop()
Beispiel #8
0
 def stop():
     if isrunning:
         app.stop()
Beispiel #9
0
class MyComponent1(event.Component):
    
    foo = event.Property(0, settable=True)
    
    @event.reaction('foo')
    def on_foo(self, *events):
        for ev in events:
            print('foo changed to', ev.new_value)

# Create component in main thread
comp = MyComponent1()

# Start server in its own thread
def start_flexx():
    app.create_server(loop=asyncio.new_event_loop())
    app.start()

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

# Manipulate component from main thread
# (the component's on_foo() gets called from other thread)
for i in range(5, 9):
    time.sleep(1)
    comp.set_foo(i)

# Stop event loop (this is thread-safe) and wait for thread to end
app.stop()
t.join()
Beispiel #10
0
def test_more_stopping():
    """ Test calling stop multiple times.
    """

    # This is why you want to create new IOLoop instances for each test

    # Create new ioloop and make Flexx use it
    loop = IOLoop()
    loop.make_current()
    server = app.create_server()

    app.stop()  # triggers event to stop
    app.start()

    app.stop()  # Extra stop - pending stop event

    # Which means the next stop does hardly block
    t0 = time.time()
    app.call_later(0.2, app.stop)
    app.start()
    assert time.time() - t0 < 0.1

    loop = IOLoop()
    loop.make_current()
    server = app.create_server()

    # But stops dont stack
    app.stop()
    app.stop()
    app.stop()
    app.stop()

    # Flush all stops ...
    app.stop()
    app.start()

    # ... so that we now have an expected loop
    t0 = time.time()
    app.call_later(0.2, app.stop)
    app.start()
    assert time.time() - t0 >= 0.1
Beispiel #11
0
 def main():
     app.stop()
     app.start()
     assert server.loop is IOLoop.current()
     res.append(3)
Beispiel #12
0
def test_more_stopping():
    """ Test calling stop multiple times.
    """
    
    # This is why you want to create new IOLoop instances for each test
    
    # Create new ioloop and make Flexx use it
    loop = IOLoop()
    loop.make_current()
    server = app.create_server()
    
    app.stop()  # triggers event to stop
    app.start()
    
    app.stop()  # Extra stop - pending stop event
    
    # Which means the next stop does hardly block
    t0 = time.time()
    app.call_later(0.2, app.stop)
    app.start()
    assert time.time() - t0 < 0.1
    
    
    loop = IOLoop()
    loop.make_current()
    server = app.create_server()
    
    # But stops dont stack
    app.stop()
    app.stop()
    app.stop()
    app.stop()
    
    # Flush all stops ...
    app.stop()
    app.start()
    
    # ... so that we now have an expected loop
    t0 = time.time()
    app.call_later(0.2, app.stop)
    app.start()
    assert time.time() - t0 >= 0.1
Beispiel #13
0
 def main():
     app.stop()
     app.start()
     assert server.loop is IOLoop.current()
     res.append(3)
Beispiel #14
0
from flexx import app, event


class MyModel1(event.HasEvents):
    @event.prop
    def foo(self, v=0):
        return v
    
    @event.connect('foo')
    def on_foo(self, *events):
        for ev in events:
            print('foo changed to', ev.new_value)

# Create model in main thread
model = MyModel1()

# Start server in its own thread
app.create_server(new_loop=True)
t = threading.Thread(target=app.start)
t.start()

# Manipulate model from main thread (the model's on_foo() gets called from other thread)
for i in range(5, 9):
    time.sleep(1)
    model.foo = i

# Stop event loop (this is thread-safe) and wait for thread to end
app.stop()
t.join()