11from __future__ import annotations
22
3- import os
4- from time import sleep
5-
63import pytest
74from tlz import frequencies
85
@@ -85,50 +82,36 @@ async def test_collect(c, s, a, b):
8582 assert tasks .collect (start = start , count = 3 ) == list (tasks .buffer )[:3 ]
8683
8784
88- @gen_cluster (client = True )
89- async def test_no_startstops (c , s , a , b ):
90- tasks = TaskStreamPlugin (s )
91- s .add_plugin (tasks )
92- # just to create the key on the scheduler
93- future = c .submit (inc , 1 )
94- await wait (future )
95- assert len (tasks .buffer ) == 1
96-
97- tasks .transition (future .key , "processing" , "erred" , stimulus_id = "s1" )
98- # Transition was not recorded because it didn't contain `startstops`
99- assert len (tasks .buffer ) == 1
100-
101- tasks .transition (future .key , "processing" , "erred" , stimulus_id = "s2" , startstops = [])
102- # Transition was not recorded because `startstops` was empty
103- assert len (tasks .buffer ) == 1
104-
105- tasks .transition (
106- future .key ,
107- "processing" ,
108- "erred" ,
109- stimulus_id = "s3" ,
110- startstops = [dict (start = time (), stop = time ())],
111- )
112- assert len (tasks .buffer ) == 2
113-
114-
11585@gen_cluster (client = True )
11686async def test_client (c , s , a , b ):
117- L = await c .get_task_stream ()
118- assert L == ()
87+ await c .get_task_stream ()
11988
120- futures = c .map (slowinc , range (10 ), delay = 0.1 )
89+ futures = c .map (inc , range (10 ))
12190 await wait (futures )
122-
123- tasks = s .plugins [TaskStreamPlugin .name ]
124- L = await c .get_task_stream ()
125- assert L == tuple (tasks .buffer )
91+ data = await c .get_task_stream ()
92+ assert len (data ) == 10
12693
12794
12895def test_client_sync (client ):
129- with get_task_stream (client = client ) as ts :
130- sleep (0.1 ) # to smooth over time differences on the scheduler
131- # to smooth over time differences on the scheduler
96+ client .get_task_stream ()
97+
98+ futures = client .map (inc , range (10 ))
99+ wait (futures )
100+ data = client .get_task_stream ()
101+ assert len (data ) == 10
102+
103+
104+ @gen_cluster (client = True )
105+ async def test_client_ctx (c , s , a , b ):
106+ async with get_task_stream () as ts :
107+ futures = c .map (inc , range (10 ))
108+ await wait (futures )
109+
110+ assert len (ts .data ) == 10
111+
112+
113+ def test_client_ctx_sync (client ):
114+ with get_task_stream () as ts :
132115 futures = client .map (inc , range (10 ))
133116 wait (futures )
134117
@@ -140,23 +123,29 @@ async def test_get_task_stream_plot(c, s, a, b):
140123 bkm = pytest .importorskip ("bokeh.models" )
141124 await c .get_task_stream ()
142125
143- futures = c .map (slowinc , range (10 ), delay = 0.1 )
126+ futures = c .map (inc , range (10 ))
144127 await wait (futures )
145128
146129 data , figure = await c .get_task_stream (plot = True )
130+ assert len (data ) == 10
147131 assert isinstance (figure , bkm .Plot )
148132
149133
150- def test_get_task_stream_save (client , tmp_path ):
134+ @gen_cluster (client = True )
135+ async def test_get_task_stream_save (c , s , a , b , tmp_path ):
151136 bkm = pytest .importorskip ("bokeh.models" )
152- tmpdir = str (tmp_path )
153- fn = os .path .join (tmpdir , "foo.html" )
137+ await c .get_task_stream ()
138+
139+ futures = c .map (inc , range (10 ))
140+ await wait (futures )
141+
142+ fn = str (tmp_path / "foo.html" )
143+ data , figure = await c .get_task_stream (plot = "save" , filename = fn )
144+ assert len (data ) == 10
154145
155- with get_task_stream (plot = "save" , filename = fn ) as ts :
156- wait (client .map (inc , range (10 )))
157146 with open (fn ) as f :
158147 data = f .read ()
159148 assert "inc" in data
160149 assert "bokeh" in data
161150
162- assert isinstance (ts . figure , bkm .Plot )
151+ assert isinstance (figure , bkm .Plot )
0 commit comments