import asyncio

import pytest

pytest.importorskip("bokeh")

from bokeh.models import ColumnDataSource, Model

from distributed.dashboard.components.shared import (
    Processing,
    ProfilePlot,
    ProfileTimePlot,
)
from distributed.utils_test import gen_cluster, slowinc


@pytest.mark.parametrize("Component", [Processing])
def test_basic(Component):
    c = Component()
    assert isinstance(c.source, ColumnDataSource)
    assert isinstance(c.root, Model)


@gen_cluster(client=True, clean_kwargs={"threads": False})
async def test_profile_plot(c, s, a, b):
    p = ProfilePlot()
    assert not p.source.data["left"]
    await c.gather(c.map(slowinc, range(10), delay=0.05))
    p.update(a.profile_recent)
    assert len(p.source.data["left"]) >= 1


@gen_cluster(client=True, clean_kwargs={"threads": False})
async def test_profile_time_plot(c, s, a, b):
    from bokeh.io import curdoc

    sp = ProfileTimePlot(s, doc=curdoc())
    sp.trigger_update()

    ap = ProfileTimePlot(a, doc=curdoc())
    ap.trigger_update()

    assert len(sp.source.data["left"]) <= 1
    assert len(ap.source.data["left"]) <= 1

    await c.gather(c.map(slowinc, range(10), delay=0.05))
    ap.trigger_update()
    sp.trigger_update()
    await asyncio.sleep(0.05)

@@ -627,47 +627,91 @@
     
-await c.gather(c.map(slowinc, range(10)
+while not len(p.source.data%5B%22left%22%5D):%0A        await c.submit(slowinc, 1, pure=False
 , de
@@ -716,25 +716,27 @@
 delay=0.
-05))%0A
+1)%0A  
     
+  
 p.update
@@ -758,51 +758,8 @@
 nt)%0A
-    assert len(p.source.data%5B%22left%22%5D) %3E= 1%0A
 %0A%0A@g
@@ -1035,24 +1035,28 @@
 %0A    assert 
+not 
 len(sp.sourc
@@ -1070,21 +1070,16 @@
 %22left%22%5D)
- %3C= 1
 %0A    ass
@@ -1082,16 +1082,20 @@
  assert 
+not 
 len(ap.s
@@ -1117,13 +1117,8 @@
 t%22%5D)
- %3C= 1
 %0A%0A  

Fix intermittent test_profile_plot failure (#6456)