|
14 | 14 |
|
15 | 15 | import json |
16 | 16 | import os |
| 17 | +import threading |
17 | 18 | import time |
18 | 19 | from types import SimpleNamespace |
19 | 20 | from typing import Any, Optional |
@@ -429,6 +430,68 @@ def test_watch_stream_loop(self): |
429 | 430 | self.assertEqual(fake_resp.close.call_count, 2) |
430 | 431 | self.assertEqual(fake_resp.release_conn.call_count, 2) |
431 | 432 |
|
| 433 | + def test_watch_stop_during_request(self): |
| 434 | + """stop() called while the request is in flight ends the stream.""" |
| 435 | + fake_resp = Mock() |
| 436 | + fake_resp.close = Mock() |
| 437 | + fake_resp.release_conn = Mock() |
| 438 | + fake_resp.stream = Mock( |
| 439 | + return_value=['{"type": "ADDED", "object": 1}\n']) |
| 440 | + |
| 441 | + w = Watch(float) |
| 442 | + |
| 443 | + def get_namespaces(**kwargs): |
| 444 | + # Simulate stop() from another thread before the response |
| 445 | + # headers arrive, i.e. before the stream holds the response. |
| 446 | + stopper = threading.Thread(target=w.stop) |
| 447 | + stopper.start() |
| 448 | + stopper.join(timeout=5) |
| 449 | + return fake_resp |
| 450 | + |
| 451 | + fake_api = Mock() |
| 452 | + fake_api.get_namespaces = Mock(side_effect=get_namespaces) |
| 453 | + fake_api.get_namespaces.__doc__ = ':rtype: V1NamespaceList' |
| 454 | + |
| 455 | + events = list(w.stream(fake_api.get_namespaces)) |
| 456 | + |
| 457 | + self.assertEqual([], events) |
| 458 | + fake_api.get_namespaces.assert_called_once_with( |
| 459 | + _preload_content=False, watch=True) |
| 460 | + fake_resp.stream.assert_not_called() |
| 461 | + fake_resp.close.assert_called_once() |
| 462 | + fake_resp.release_conn.assert_called_once() |
| 463 | + |
| 464 | + def test_watch_stop_during_reconnect_request(self): |
| 465 | + """stop() during a reconnect request ends the stream unread.""" |
| 466 | + first_resp = Mock() |
| 467 | + first_resp.stream = Mock( |
| 468 | + return_value=['{"type": "ADDED", "object": 1}\n']) |
| 469 | + second_resp = Mock() |
| 470 | + second_resp.stream = Mock( |
| 471 | + return_value=['{"type": "ADDED", "object": 2}\n']) |
| 472 | + |
| 473 | + w = Watch(float) |
| 474 | + |
| 475 | + def get_namespaces(**kwargs): |
| 476 | + if fake_api.get_namespaces.call_count == 1: |
| 477 | + return first_resp |
| 478 | + stopper = threading.Thread(target=w.stop) |
| 479 | + stopper.start() |
| 480 | + stopper.join(timeout=5) |
| 481 | + return second_resp |
| 482 | + |
| 483 | + fake_api = Mock() |
| 484 | + fake_api.get_namespaces = Mock(side_effect=get_namespaces) |
| 485 | + fake_api.get_namespaces.__doc__ = ':rtype: V1NamespaceList' |
| 486 | + |
| 487 | + events = list(w.stream(fake_api.get_namespaces, resource_version='1')) |
| 488 | + |
| 489 | + self.assertEqual(1, len(events)) |
| 490 | + self.assertEqual(2, fake_api.get_namespaces.call_count) |
| 491 | + second_resp.stream.assert_not_called() |
| 492 | + second_resp.close.assert_called_once() |
| 493 | + second_resp.release_conn.assert_called_once() |
| 494 | + |
432 | 495 | def test_unmarshal_with_float_object(self): |
433 | 496 | w = Watch() |
434 | 497 | event = w.unmarshal_event('{"type": "ADDED", "object": 1}', 'float') |
|
0 commit comments