##// END OF EJS Templates
Fix timeout test
Jessica B. Hamrick -
Show More
@@ -1,149 +1,151
1 1 """
2 2 Module with tests for the execute preprocessor.
3 3 """
4 4
5 5 # Copyright (c) IPython Development Team.
6 6 # Distributed under the terms of the Modified BSD License.
7 7
8 8 import copy
9 9 import glob
10 10 import io
11 11 import os
12 12 import re
13 13
14 14 try:
15 15 from queue import Empty # Py 3
16 16 except ImportError:
17 17 from Queue import Empty # Py 2
18 18
19 19 from IPython import nbformat
20 20
21 21 from .base import PreprocessorTestsBase
22 22 from ..execute import ExecutePreprocessor
23 23
24 24 from IPython.nbconvert.filters import strip_ansi
25 25 from nose.tools import assert_raises
26 26
27 27 addr_pat = re.compile(r'0x[0-9a-f]{7,9}')
28 28
29 29 class TestExecute(PreprocessorTestsBase):
30 30 """Contains test functions for execute.py"""
31 31
32 32 @staticmethod
33 33 def normalize_output(output):
34 34 """
35 35 Normalizes outputs for comparison.
36 36 """
37 37 output = dict(output)
38 38 if 'metadata' in output:
39 39 del output['metadata']
40 40 if 'text' in output:
41 41 output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text'])
42 42 if 'text/plain' in output.get('data', {}):
43 43 output['data']['text/plain'] = \
44 44 re.sub(addr_pat, '<HEXADDR>', output['data']['text/plain'])
45 45 if 'traceback' in output:
46 46 tb = []
47 47 for line in output['traceback']:
48 48 tb.append(strip_ansi(line))
49 49 output['traceback'] = tb
50 50
51 51 return output
52 52
53 53
54 54 def assert_notebooks_equal(self, expected, actual):
55 55 expected_cells = expected['cells']
56 56 actual_cells = actual['cells']
57 57 self.assertEqual(len(expected_cells), len(actual_cells))
58 58
59 59 for expected_cell, actual_cell in zip(expected_cells, actual_cells):
60 60 expected_outputs = expected_cell.get('outputs', [])
61 61 actual_outputs = actual_cell.get('outputs', [])
62 62 normalized_expected_outputs = list(map(self.normalize_output, expected_outputs))
63 63 normalized_actual_outputs = list(map(self.normalize_output, actual_outputs))
64 64 self.assertEqual(normalized_expected_outputs, normalized_actual_outputs)
65 65
66 66 expected_execution_count = expected_cell.get('execution_count', None)
67 67 actual_execution_count = actual_cell.get('execution_count', None)
68 68 self.assertEqual(expected_execution_count, actual_execution_count)
69 69
70 70
71 71 def build_preprocessor(self, opts):
72 72 """Make an instance of a preprocessor"""
73 73 preprocessor = ExecutePreprocessor()
74 74 preprocessor.enabled = True
75 75 for opt in opts:
76 76 setattr(preprocessor, opt, opts[opt])
77 77 return preprocessor
78 78
79 79
80 80 def test_constructor(self):
81 81 """Can a ExecutePreprocessor be constructed?"""
82 82 self.build_preprocessor({})
83 83
84 84
85 85 def run_notebook(self, filename, opts, resources):
86 86 """Loads and runs a notebook, returning both the version prior to
87 87 running it and the version after running it.
88 88
89 89 """
90 90 with io.open(filename) as f:
91 91 input_nb = nbformat.read(f, 4)
92 92 preprocessor = self.build_preprocessor(opts)
93 93 cleaned_input_nb = copy.deepcopy(input_nb)
94 94 for cell in cleaned_input_nb.cells:
95 95 if 'execution_count' in cell:
96 96 del cell['execution_count']
97 97 cell['outputs'] = []
98 98 output_nb, _ = preprocessor(cleaned_input_nb, resources)
99 99 return input_nb, output_nb
100 100
101 101 def test_run_notebooks(self):
102 102 """Runs a series of test notebooks and compares them to their actual output"""
103 103 current_dir = os.path.dirname(__file__)
104 104 input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
105 105 for filename in input_files:
106 106 if os.path.basename(filename) == "Disable Stdin.ipynb":
107 107 continue
108 108 elif os.path.basename(filename) == "Interrupt.ipynb":
109 109 opts = dict(timeout=1, interrupt_on_timeout=True)
110 110 else:
111 111 opts = {}
112 112 res = self.build_resources()
113 113 res['metadata']['path'] = os.path.dirname(filename)
114 114 input_nb, output_nb = self.run_notebook(filename, opts, res)
115 115 self.assert_notebooks_equal(input_nb, output_nb)
116 116
117 117 def test_empty_path(self):
118 118 """Can the kernel be started when the path is empty?"""
119 119 current_dir = os.path.dirname(__file__)
120 120 filename = os.path.join(current_dir, 'files', 'HelloWorld.ipynb')
121 121 res = self.build_resources()
122 122 res['metadata']['path'] = ''
123 123 input_nb, output_nb = self.run_notebook(filename, {}, res)
124 124 self.assert_notebooks_equal(input_nb, output_nb)
125 125
126 126 def test_disable_stdin(self):
127 127 """Test disabling standard input"""
128 128 current_dir = os.path.dirname(__file__)
129 129 filename = os.path.join(current_dir, 'files', 'Disable Stdin.ipynb')
130 130 res = self.build_resources()
131 131 res['metadata']['path'] = os.path.dirname(filename)
132 132 input_nb, output_nb = self.run_notebook(filename, {}, res)
133 133
134 134 # We need to special-case this particular notebook, because the
135 135 # traceback contains machine-specific stuff like where IPython
136 136 # is installed. It is sufficient here to just check that an error
137 137 # was thrown, and that it was a StdinNotImplementedError
138 138 self.assertEqual(len(output_nb['cells']), 1)
139 139 self.assertEqual(len(output_nb['cells'][0]['outputs']), 1)
140 140 output = output_nb['cells'][0]['outputs'][0]
141 141 self.assertEqual(output['output_type'], 'error')
142 142 self.assertEqual(output['ename'], 'StdinNotImplementedError')
143 143 self.assertEqual(output['evalue'], 'raw_input was called, but this frontend does not support input requests.')
144 144
145 145 def test_timeout(self):
146 146 """Check that an error is raised when a computation times out"""
147 147 current_dir = os.path.dirname(__file__)
148 148 filename = os.path.join(current_dir, 'files', 'Interrupt.ipynb')
149 assert_raises(Empty, self.run_notebook, filename, dict(timeout=1))
149 res = self.build_resources()
150 res['metadata']['path'] = os.path.dirname(filename)
151 assert_raises(Empty, self.run_notebook, filename, dict(timeout=1), res)
General Comments 0
You need to be logged in to leave comments. Login now