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_periodic(threads): button = Button(name='Click') counts = [] def cb(count=[0]): count[0] += 1 counts.append(count[0]) time.sleep(0.5) count[0] -= 1 port = 6017 serve(button, port=port, threaded=True, show=False) # Wait for server to start time.sleep(1) requests.get(f"http://localhost:{port}/") doc = list(button._models.values())[0][0].document with set_curdoc(doc): state.add_periodic_callback(cb, 100) # Wait for callbacks to be scheduled time.sleep(1) # Checks whether periodic callbacks were executed concurrently assert max(counts) >= 2
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
def test_server_thread_pool_bokeh_event(threads): import pandas as pd df = pd.DataFrame([[1, 1], [2, 2]], columns=['A', 'B']) tabulator = Tabulator(df) counts = [] def cb(event, count=[0]): count[0] += 1 counts.append(count[0]) time.sleep(0.5) count[0] -= 1 tabulator.on_edit(cb) port = 6016 serve(tabulator, port=port, threaded=True, show=False) # Wait for server to start time.sleep(1) requests.get(f"http://localhost:{port}/") model = list(tabulator._models.values())[0][0] doc = model.document event = TableEditEvent(model, 'A', 0) with set_curdoc(doc): for _ in range(2): tabulator._server_event(doc, event) # Wait for callbacks to be scheduled time.sleep(1) # Checks whether Tabulator on_edit callback was executed concurrently assert max(counts) == 2