Skip to content

Commit

Permalink
Test for parallel execution of notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudy committed May 7, 2019
1 parent cb2ef94 commit e348ee1
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 5 deletions.
84 changes: 84 additions & 0 deletions nbconvert/preprocessors/tests/files/Parallel Execute.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import os.path\n",
"import tempfile\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"other_notebook = {'A':'B', 'B':'A'}[this_notebook]\n",
"directory = os.environ['NBEXECUTE_TEST_PARALLEL_TMPDIR']\n",
"with open(os.path.join(directory, 'test_file_{}.txt'.format(this_notebook)), 'w') as f:\n",
" f.write('Hello from {}'.format(this_notebook))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"start = time.time()\n",
"timeout = 5\n",
"end = start + timeout\n",
"target_file = os.path.join(directory, 'test_file_{}.txt'.format(other_notebook))\n",
"while time.time() < end:\n",
" time.sleep(0.1)\n",
" if os.path.exists(target_file):\n",
" with open(target_file, 'r') as f:\n",
" text = f.read()\n",
" if text == 'Hello from {}'.format(other_notebook):\n",
" break\n",
"else:\n",
" assert False, \"Timed out – didn't get a message from {}\".format(other_notebook)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
69 changes: 64 additions & 5 deletions nbconvert/preprocessors/tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import glob
import io
import os
import logging
import re
import threading
import multiprocessing as mp

import nbformat
import sys
Expand Down Expand Up @@ -69,14 +72,17 @@ def build_preprocessor(opts):
return preprocessor


def run_notebook(filename, opts, resources):
def run_notebook(filename, opts, resources, preprocess_notebook=None):
"""Loads and runs a notebook, returning both the version prior to
running it and the version after running it.
"""
with io.open(filename) as f:
input_nb = nbformat.read(f, 4)

if preprocess_notebook:
input_nb = preprocess_notebook(input_nb)

preprocessor = build_preprocessor(opts)
cleaned_input_nb = copy.deepcopy(input_nb)
for cell in cleaned_input_nb.cells:
Expand Down Expand Up @@ -218,6 +224,12 @@ def assert_notebooks_equal(expected, actual):
actual_execution_count = actual_cell.get('execution_count', None)
assert expected_execution_count == actual_execution_count

def notebook_resources():
res = ResourcesDict()
res['metadata'] = ResourcesDict()
res['metadata']['path'] = os.path.join(current_dir, 'files')
return res


@pytest.mark.parametrize(
["input_name", "opts"],
Expand All @@ -244,13 +256,60 @@ def assert_notebooks_equal(expected, actual):
def test_run_all_notebooks(input_name, opts):
"""Runs a series of test notebooks and compares them to their actual output"""
input_file = os.path.join(current_dir, 'files', input_name)
res = ResourcesDict()
res['metadata'] = ResourcesDict()
res['metadata']['path'] = os.path.join(current_dir, 'files')
input_nb, output_nb = run_notebook(input_file, opts, res)
input_nb, output_nb = run_notebook(input_file, opts, notebook_resources())
assert_notebooks_equal(input_nb, output_nb)


def label_parallel_notebook(nb, label):
"""Insert a cell in a notebook which sets the variable `this_notebook` to the string `label`.
Used for parallel testing to label two notebooks which are run simultaneously.
"""
label_cell = nbformat.NotebookNode(
{
"cell_type": "code",
"execution_count": None,
"metadata": {},
"outputs": [],
"source": "this_notebook = '{}'".format(label),
}
)

nb.cells.insert(1, label_cell)
return nb


def test_parallel_notebooks(capfd, tmpdir):
"""Two notebooks should be able to be run simultaneously without problems.
The two notebooks spawned here use the filesystem to check that the other notebook
wrote to the filesystem."""

opts = dict(kernel_name="python")
input_name = "Parallel Execute.ipynb"
input_file = os.path.join(current_dir, "files", input_name)
res = notebook_resources()

with modified_env({"NBEXECUTE_TEST_PARALLEL_TMPDIR": str(tmpdir)}):
threads = [
threading.Thread(
target=run_notebook,
args=(
input_file,
opts,
res,
functools.partial(label_parallel_notebook, label=label),
),
)
for label in ("A", "B")
]
[t.start() for t in threads]
[t.join(timeout=2) for t in threads]

captured = capfd.readouterr()
assert captured.err == ""


class TestExecute(PreprocessorTestsBase):
"""Contains test functions for execute.py"""
maxDiff = None
Expand Down

0 comments on commit e348ee1

Please sign in to comment.