async def test_bad_database_filepath(jp_runtime_dir): kernel_manager = DummyMKM() # Try to write to a path that's a directory, not a file. path_id_directory = str(jp_runtime_dir) # Should raise an error because the path is a directory. with pytest.raises(TraitError) as err: SessionManager( kernel_manager=kernel_manager, contents_manager=ContentsManager(), database_filepath=str(path_id_directory), ) # Try writing to file that's not a valid SQLite 3 database file. non_db_file = jp_runtime_dir.joinpath("non_db_file.db") non_db_file.write_bytes(b"this is a bad file") # Should raise an error because the file doesn't # start with an SQLite database file header. with pytest.raises(TraitError) as err: SessionManager( kernel_manager=kernel_manager, contents_manager=ContentsManager(), database_filepath=str(non_db_file), )
async def test_good_database_filepath(jp_runtime_dir): kernel_manager = DummyMKM() # Try writing to an empty file. empty_file = jp_runtime_dir.joinpath("empty.db") empty_file.write_bytes(b"") session_manager = SessionManager( kernel_manager=kernel_manager, contents_manager=ContentsManager(), database_filepath=str(empty_file), ) await session_manager.create_session(path="/path/to/test.ipynb", kernel_name="python", type="notebook") # Assert that the database file exists assert empty_file.exists() # Close the current session manager del session_manager # Try writing to a file that already exists. session_manager = SessionManager( kernel_manager=kernel_manager, contents_manager=ContentsManager(), database_filepath=str(empty_file), ) assert session_manager.database_filepath == str(empty_file)
async def test_session_persistence(jp_runtime_dir): session_db_path = jp_runtime_dir.joinpath("test-session.db") # Kernel manager needs to persist. kernel_manager = DummyMKM() # Initialize a session and start a connection. # This should create the session database the first time. session_manager = SessionManager( kernel_manager=kernel_manager, contents_manager=ContentsManager(), database_filepath=str(session_db_path), ) session = await session_manager.create_session(path="/path/to/test.ipynb", kernel_name="python", type="notebook") # Assert that the database file exists assert session_db_path.exists() with open(session_db_path, "rb") as f: header = f.read(100) assert header.startswith(b"SQLite format 3") # Close the current session manager del session_manager # Get a new session_manager session_manager = SessionManager( kernel_manager=kernel_manager, contents_manager=ContentsManager(), database_filepath=str(session_db_path), ) # Assert that the session database persists. session = await session_manager.get_session(session_id=session["id"])
async def test_pending_kernel(): session_manager = SessionManager( kernel_manager=SlowStartingKernelsMKM(), contents_manager=ContentsManager() ) # Create a session with a slow starting kernel fut = session_manager.create_session( path="/path/to/test.ipynb", kernel_name="python", type="notebook" ) task = asyncio.create_task(fut) await asyncio.sleep(0.1) assert len(session_manager._pending_sessions) == 1 # Get a handle on the record record = session_manager._pending_sessions._records[0] session = await task # Check that record is cleared after the task has completed. assert record not in session_manager._pending_sessions # Check pending kernel list when sessions are fut = session_manager.delete_session(session_id=session["id"]) task = asyncio.create_task(fut) await asyncio.sleep(0.1) assert len(session_manager._pending_sessions) == 1 # Get a handle on the record record = session_manager._pending_sessions._records[0] session = await task # Check that record is cleared after the task has completed. assert record not in session_manager._pending_sessions # Test multiple, parallel pending kernels fut1 = session_manager.create_session( path="/path/to/test.ipynb", kernel_name="python", type="notebook" ) fut2 = session_manager.create_session( path="/path/to/test.ipynb", kernel_name="python", type="notebook" ) task1 = asyncio.create_task(fut1) await asyncio.sleep(0.1) task2 = asyncio.create_task(fut2) await asyncio.sleep(0.1) assert len(session_manager._pending_sessions) == 2 await task1 await task2 session1, session2 = await asyncio.gather(task1, task2) assert len(session_manager._pending_sessions) == 0
def session_manager(): return SessionManager(kernel_manager=DummyMKM(), contents_manager=ContentsManager())