async def test_stream_keepalive(hass):
    'Test hls stream retries the stream when keepalive=True.'
    (await async_setup_component(hass, 'stream', {'stream': {}}))
    source = 'test_stream_keepalive_source'
    stream = preload_stream(hass, source)
    track = stream.add_provider('hls')
    track.num_segments = 2
    cur_time = 0

    def time_side_effect():
        nonlocal cur_time
        if (cur_time >= 80):
            stream.keepalive = False
        cur_time += 40
        return cur_time
    with patch('av.open') as av_open, patch('homeassistant.components.stream.worker.time') as mock_time, patch('homeassistant.components.stream.worker.STREAM_RESTART_INCREMENT', 0):
        av_open.side_effect = av.error.InvalidDataError((- 2), 'error')
        mock_time.time.side_effect = time_side_effect
        request_stream(hass, source, keepalive=True)
        stream._thread.join()
        stream._thread = None
        assert (av_open.call_count == 2)
    stream.stop()
Test hls stream retries the stream when keepalive=True.
async def test_stream_keepalive(hass):
    
    (await async_setup_component(hass, 'stream', {'stream': {}}))
    source = 'test_stream_keepalive_source'
    stream = preload_stream(hass, source)
    track = stream.add_provider('hls')
    track.num_segments = 2
    cur_time = 0

    def time_side_effect():
        nonlocal cur_time
        if (cur_time >= 80):
            stream.keepalive = False
        cur_time += 40
        return cur_time
    with patch('av.open') as av_open, patch('homeassistant.components.stream.worker.time') as mock_time, patch('homeassistant.components.stream.worker.STREAM_RESTART_INCREMENT', 0):
        av_open.side_effect = av.error.InvalidDataError((- 2), 'error')
        mock_time.time.side_effect = time_side_effect
        request_stream(hass, source, keepalive=True)
        stream._thread.join()
        stream._thread = None
        assert (av_open.call_count == 2)
    stream.stop()