def test_create_session_with_keepalive_option(cbcsdk_mock):
    """Test creating a Live Response session using the keepalive option."""
    cbcsdk_mock.mock_request('POST',
                             '/integrationServices/v3/cblr/session/2468',
                             SESSION_INIT_RESP)
    cbcsdk_mock.mock_request('GET',
                             '/integrationServices/v3/cblr/session/1:2468',
                             SESSION_POLL_RESP)
    cbcsdk_mock.mock_request('GET', '/integrationServices/v3/device/2468',
                             DEVICE_RESPONSE)
    cbcsdk_mock.mock_request('PUT', '/integrationServices/v3/cblr/session',
                             SESSION_CLOSE_RESP)
    manager = LiveResponseSessionManager(cbcsdk_mock.api, 100000, True)
    try:
        with manager.request_session(2468) as session1:
            assert session1.session_id == '1:2468'
            assert session1.device_id == 2468
            assert session1._cblr_manager is manager
            assert session1._cb is cbcsdk_mock.api
            assert session1.os_type == 1
        with manager.request_session(2468) as session2:
            assert session2 is session1
        assert len(manager._sessions) == 1
        manager._maintain_sessions()
        assert len(manager._sessions) == 0
    finally:
        manager.stop_keepalive_thread()
def test_session_maintenance_sends_keepalive(cbcsdk_mock, thrown_exception):
    """Test to ensure the session maintenance sends the keepalive messages as needed."""
    cbcsdk_mock.mock_request('POST', '/integrationServices/v3/cblr/session/2468', SESSION_INIT_RESP)
    cbcsdk_mock.mock_request('GET', '/integrationServices/v3/cblr/session/1:2468', SESSION_POLL_RESP)
    cbcsdk_mock.mock_request('GET', '/integrationServices/v3/device/2468', DEVICE_RESPONSE)
    cbcsdk_mock.mock_request('GET', '/integrationServices/v3/cblr/session/1:2468/keepalive', {})
    cbcsdk_mock.mock_request('GET', '/integrationServices/v3/cblr/session/1:2468/keepalive', thrown_exception)
    manager = LiveResponseSessionManager(cbcsdk_mock.api, 100000, True)
    try:
        with manager.request_session(2468):
            manager._maintain_sessions()
            assert len(manager._sessions) == 1
            manager._maintain_sessions()
    finally:
        manager.stop_keepalive_thread()