@@ -1495,6 +1495,110 @@ def fake_stream(func, **kw):
14951495 # Still in cache.
14961496 self .assertIsNotNone (informer .cache .get_by_key ("default/stable-pod" ))
14971497
1498+ def test_stop_during_initial_list_does_not_open_watch (self ):
1499+ """stop() during the initial list returns without opening a watch."""
1500+ list_started = threading .Event ()
1501+ release_list = threading .Event ()
1502+ release_watch = threading .Event ()
1503+ self .addCleanup (release_watch .set )
1504+ self .addCleanup (release_list .set )
1505+
1506+ list_resp = MagicMock ()
1507+ list_resp .items = []
1508+ list_resp .metadata = MagicMock (resource_version = "1" )
1509+
1510+ def list_func (** kw ):
1511+ list_started .set ()
1512+ release_list .wait (timeout = 10 )
1513+ return list_resp
1514+
1515+ informer = SharedInformer (list_func = list_func )
1516+
1517+ with patch ("kubernetes.informer.informer.Watch" ) as MockWatch :
1518+ mock_w = MagicMock ()
1519+
1520+ def fake_stream (func , ** kw ):
1521+ # A quiet resource: the stream blocks without yielding events.
1522+ release_watch .wait (timeout = 10 )
1523+ return iter ([])
1524+
1525+ mock_w .stream .side_effect = fake_stream
1526+ MockWatch .return_value = mock_w
1527+
1528+ informer .start ()
1529+ self .assertTrue (list_started .wait (timeout = 3 ))
1530+ worker = informer ._thread
1531+ stopper = threading .Thread (target = informer .stop , daemon = True )
1532+ stopper .start ()
1533+ self .assertTrue (informer ._stop_event .wait (timeout = 3 ))
1534+
1535+ release_list .set ()
1536+ stopper .join (timeout = 3 )
1537+ stop_returned = not stopper .is_alive ()
1538+ # Unblock a watch opened by mistake so the worker can exit.
1539+ release_watch .set ()
1540+ worker .join (timeout = 3 )
1541+
1542+ self .assertTrue (stop_returned , "stop() did not return after the list" )
1543+ mock_w .stream .assert_not_called ()
1544+ self .assertFalse (worker .is_alive ())
1545+
1546+ def test_stop_during_watch_does_not_report_read_error (self ):
1547+ """The read error caused by stop() is not reported as a failure."""
1548+ from urllib3 .exceptions import ProtocolError
1549+
1550+ list_func = MagicMock (return_value = MagicMock (
1551+ items = [], metadata = MagicMock (resource_version = "1" )))
1552+ informer = SharedInformer (list_func = list_func )
1553+ errors = []
1554+ informer .add_event_handler (ERROR , errors .append )
1555+ watching = threading .Event ()
1556+ shut_down = threading .Event ()
1557+ self .addCleanup (shut_down .set )
1558+
1559+ def fake_stream (func , ** kw ):
1560+ # Watch.stop() shuts the connection down, so the read fails.
1561+ watching .set ()
1562+ shut_down .wait (timeout = 10 )
1563+ raise ProtocolError ("Response ended prematurely" )
1564+
1565+ with patch ("kubernetes.informer.informer.Watch" ) as MockWatch , \
1566+ self .assertNoLogs ("kubernetes.informer.informer" , "WARNING" ):
1567+ MockWatch .return_value .stream .side_effect = fake_stream
1568+ MockWatch .return_value .stop .side_effect = shut_down .set
1569+ informer .start ()
1570+ self .assertTrue (watching .wait (timeout = 3 ))
1571+ informer .stop ()
1572+
1573+ self .assertEqual (errors , [])
1574+
1575+ def test_stop_from_handler_returns_to_handler (self ):
1576+ """stop() called from a handler returns, and start() works again."""
1577+ pod = _make_pod ("default" , "p" )
1578+ list_func = MagicMock (return_value = MagicMock (
1579+ items = [], metadata = MagicMock (resource_version = "1" )))
1580+ informer = SharedInformer (list_func = list_func )
1581+ after_stop = []
1582+
1583+ def handler (obj ):
1584+ informer .stop ()
1585+ after_stop .append (obj )
1586+
1587+ informer .add_event_handler (ADDED , handler )
1588+
1589+ with patch ("kubernetes.informer.informer.Watch" ) as MockWatch , \
1590+ self .assertNoLogs ("kubernetes.informer.informer" , "WARNING" ):
1591+ MockWatch .return_value .stream .side_effect = \
1592+ lambda func , ** kw : iter ([{"type" : ADDED , "object" : pod }])
1593+ informer .start ()
1594+ informer ._thread .join (timeout = 3 )
1595+ informer .start ()
1596+ second = informer ._thread
1597+ second .join (timeout = 3 )
1598+
1599+ self .assertEqual (after_stop , [pod , pod ])
1600+ self .assertFalse (second .is_alive ())
1601+
14981602
14991603if __name__ == "__main__" :
15001604 unittest .main ()
0 commit comments