def test_server_async_callbacks(): button = Button(name='Click') counts = [] async def cb(event, count=[0]): import asyncio count[0] += 1 counts.append(count[0]) await asyncio.sleep(1) count[0] -= 1 button.on_click(cb) server = serve(button, port=5008, threaded=True, show=False) # Wait for server to start time.sleep(1) requests.get("http://localhost:5008/") doc = list(button._models.values())[0][0].document with set_curdoc(doc): for _ in range(5): button.clicks += 1 # Wait for callbacks to be scheduled time.sleep(2) # Ensure multiple callbacks started concurrently try: assert max(counts) > 1 finally: server.stop()
def test_server_thread_pool_change_event(threads): button = Button(name='Click') button2 = Button(name='Click') counts = [] def cb(event, count=[0]): count[0] += 1 counts.append(count[0]) time.sleep(0.5) count[0] -= 1 button.on_click(cb) button2.on_click(cb) layout = Row(button, button2) port = 6015 serve(layout, port=port, threaded=True, show=False) # Wait for server to start time.sleep(1) requests.get(f"http://localhost:{port}/") model = list(layout._models.values())[0][0] doc = model.document with set_curdoc(doc): button._server_change(doc, model.ref['id'], None, 'clicks', 0, 1) button2._server_change(doc, model.ref['id'], None, 'clicks', 0, 1) # Wait for callbacks to be scheduled time.sleep(1) # Checks whether Button on_click callback was executed concurrently assert max(counts) == 2