|
8 | 8 | import pytest
|
9 | 9 |
|
10 | 10 | from streamlink_cli.utils.progress import Progress, ProgressFormatter
|
| 11 | +from tests.testutils.handshake import Handshake |
11 | 12 |
|
12 | 13 |
|
13 | 14 | class TestProgressFormatter:
|
@@ -294,3 +295,43 @@ def test_download_speed(self):
|
294 | 295 | progress.update()
|
295 | 296 | assert output_write.call_args_list[-1] \
|
296 | 297 | == call("\r[download] Written 21.00 KiB (6s @ 6.00 KiB/s) ")
|
| 298 | + |
| 299 | + def test_update(self): |
| 300 | + handshake = Handshake() |
| 301 | + |
| 302 | + class _Progress(Progress): |
| 303 | + def update(self): |
| 304 | + with handshake(): |
| 305 | + return super().update() |
| 306 | + |
| 307 | + stream = StringIO() |
| 308 | + thread = _Progress(stream=stream, path=PurePath()) |
| 309 | + # override the thread's polling time after initializing the deque of the rolling average download speed: |
| 310 | + # the interval constructor keyword is used to set the deque size |
| 311 | + thread.interval = 0 |
| 312 | + thread.start() |
| 313 | + |
| 314 | + # first tick |
| 315 | + assert handshake.wait_ready(1) |
| 316 | + thread.write(b"123") |
| 317 | + assert handshake.step(1) |
| 318 | + assert stream.getvalue().split("\r")[-1].startswith("[download] Written 3 bytes") |
| 319 | + |
| 320 | + # second tick |
| 321 | + assert handshake.wait_ready(1) |
| 322 | + thread.write(b"465") |
| 323 | + assert handshake.step(1) |
| 324 | + assert stream.getvalue().split("\r")[-1].startswith("[download] Written 6 bytes") |
| 325 | + |
| 326 | + # close progress thread |
| 327 | + assert handshake.wait_ready(1) |
| 328 | + thread.close() |
| 329 | + assert handshake.step(1) |
| 330 | + assert stream.getvalue().split("\r")[-1].startswith("[download] Written 6 bytes") |
| 331 | + |
| 332 | + # write data right after closing the thread, but before it has halted |
| 333 | + thread.write(b"789") |
| 334 | + handshake.go() |
| 335 | + thread.join(1) |
| 336 | + assert not thread.is_alive() |
| 337 | + assert stream.getvalue().split("\r")[-1].startswith("[download] Written 9 bytes") |
0 commit comments