Show More
@@ -1,191 +1,203 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | 'services/kernels/js/kernel', |
|
9 | 9 | ], function(IPython, $, utils, kernel) { |
|
10 | 10 | "use strict"; |
|
11 | 11 | |
|
12 | 12 | var Session = function (options) { |
|
13 | 13 | this.id = null; |
|
14 | 14 | this.notebook_model = { |
|
15 | 15 | name: options.notebook_name, |
|
16 | 16 | path: options.notebook_path |
|
17 | 17 | }; |
|
18 | 18 | this.kernel_model = { |
|
19 | 19 | id: null, |
|
20 | 20 | name: options.kernel_name |
|
21 | 21 | }; |
|
22 | 22 | |
|
23 | 23 | this.base_url = options.base_url; |
|
24 | 24 | this.ws_url = options.ws_url; |
|
25 | 25 | this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions'); |
|
26 | 26 | this.session_url = null; |
|
27 | 27 | |
|
28 | 28 | this.notebook = options.notebook; |
|
29 | 29 | this.kernel = null; |
|
30 | 30 | this.events = options.events; |
|
31 | 31 | }; |
|
32 | 32 | |
|
33 | 33 | /** |
|
34 | 34 | * GET /api/sessions |
|
35 | 35 | */ |
|
36 | 36 | Session.prototype.list = function (success, error) { |
|
37 | 37 | $.ajax(this.session_service_url, { |
|
38 | 38 | processData: false, |
|
39 | 39 | cache: false, |
|
40 | 40 | type: "GET", |
|
41 | 41 | dataType: "json", |
|
42 | 42 | success: success, |
|
43 | 43 | error: this._on_error(error) |
|
44 | 44 | }); |
|
45 | 45 | }; |
|
46 | 46 | |
|
47 | 47 | /** |
|
48 | 48 | * POST /api/sessions |
|
49 | 49 | */ |
|
50 | 50 | Session.prototype.start = function (success, error) { |
|
51 | if (this.kernel !== null) { | |
|
52 | throw new Error("session has already been started"); | |
|
53 | }; | |
|
54 | ||
|
51 | 55 | var that = this; |
|
52 | 56 | var on_success = function (data, status, xhr) { |
|
53 | 57 | var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels"); |
|
54 | 58 | that.kernel = new kernel.Kernel( |
|
55 | 59 | kernel_service_url, that.ws_url, that.notebook, |
|
56 | 60 | that.kernel_model.id, that.kernel_model.name); |
|
57 | 61 | that.kernel._kernel_started(data.kernel); |
|
58 | 62 | if (success) { |
|
59 | 63 | success(data, status, xhr); |
|
60 | 64 | } |
|
61 | 65 | }; |
|
62 | 66 | var on_error = function (xhr, status, err) { |
|
63 | 67 | that.events.trigger('status_dead.Kernel'); |
|
64 | 68 | if (error) { |
|
65 | 69 | error(xhr, status, err); |
|
66 | 70 | } |
|
67 | 71 | }; |
|
68 | 72 | |
|
69 | 73 | $.ajax(this.session_service_url, { |
|
70 | 74 | processData: false, |
|
71 | 75 | cache: false, |
|
72 | 76 | type: "POST", |
|
73 | 77 | data: JSON.stringify(this._get_model()), |
|
74 | 78 | dataType: "json", |
|
75 | 79 | success: this._on_success(on_success), |
|
76 | 80 | error: this._on_error(on_error) |
|
77 | 81 | }); |
|
78 | 82 | }; |
|
79 | 83 | |
|
80 | 84 | /** |
|
81 | 85 | * GET /api/sessions/[:session_id] |
|
82 | 86 | */ |
|
83 | 87 | Session.prototype.get_info = function (success, error) { |
|
84 | 88 | $.ajax(this.session_url, { |
|
85 | 89 | processData: false, |
|
86 | 90 | cache: false, |
|
87 | 91 | type: "GET", |
|
88 | 92 | dataType: "json", |
|
89 | 93 | success: this._on_success(success), |
|
90 | 94 | error: this._on_error(error) |
|
91 | 95 | }); |
|
92 | 96 | }; |
|
93 | 97 | |
|
94 | 98 | /** |
|
95 | 99 | * PATCH /api/sessions/[:session_id] |
|
96 | 100 | */ |
|
97 | 101 | Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) { |
|
102 | if (notebook_name !== undefined) { | |
|
98 | 103 | this.notebook_model.name = notebook_name; |
|
104 | } | |
|
105 | if (notebook_path !== undefined) { | |
|
99 | 106 | this.notebook_model.path = notebook_path; |
|
107 | } | |
|
108 | if (kernel_name !== undefined) { | |
|
100 | 109 | this.kernel_model.name = kernel_name; |
|
110 | } | |
|
111 | ||
|
112 | console.log(JSON.stringify(this._get_model())); | |
|
101 | 113 | |
|
102 | 114 | $.ajax(this.session_url, { |
|
103 | 115 | processData: false, |
|
104 | 116 | cache: false, |
|
105 | 117 | type: "PATCH", |
|
106 | 118 | data: JSON.stringify(this._get_model()), |
|
107 | 119 |
dataType |
|
108 | 120 | success: this._on_success(success), |
|
109 | 121 | error: this._on_error(error) |
|
110 | 122 | }); |
|
111 | 123 | }; |
|
112 | 124 | |
|
113 | 125 | Session.prototype.rename_notebook = function (name, path, success, error) { |
|
114 |
this.change(name, path, |
|
|
126 | this.change(name, path, undefined, success, error); | |
|
115 | 127 | }; |
|
116 | 128 | |
|
117 | 129 | /** |
|
118 | 130 | * DELETE /api/sessions/[:session_id] |
|
119 | 131 | */ |
|
120 | 132 | Session.prototype.delete = function (success, error) { |
|
121 | 133 | if (this.kernel) { |
|
122 | 134 | this.kernel._kernel_dead(); |
|
123 | 135 | } |
|
124 | 136 | |
|
125 | 137 | $.ajax(this.session_url, { |
|
126 | 138 | processData: false, |
|
127 | 139 | cache: false, |
|
128 | 140 | type: "DELETE", |
|
129 | 141 | dataType: "json", |
|
130 | 142 | success: this._on_success(success), |
|
131 | 143 | error: this._on_error(error) |
|
132 | 144 | }); |
|
133 | 145 | }; |
|
134 | 146 | |
|
135 | 147 | Session.prototype._get_model = function () { |
|
136 | 148 | return { |
|
137 | 149 | notebook: this.notebook_model, |
|
138 | 150 | kernel: this.kernel_model |
|
139 | 151 | }; |
|
140 | 152 | }; |
|
141 | 153 | |
|
142 | 154 | Session.prototype._update_model = function (data) { |
|
143 | 155 | if (data && data.id) { |
|
144 | 156 | this.id = data.id; |
|
145 | 157 | this.session_url = utils.url_join_encode(this.session_service_url, this.id); |
|
146 | 158 | } |
|
147 | 159 | if (data && data.notebook) { |
|
148 | 160 | this.notebook_model.name = data.notebook.name; |
|
149 | 161 | this.notebook_model.path = data.notebook.path; |
|
150 | 162 | } |
|
151 | 163 | if (data && data.kernel) { |
|
152 | 164 | this.kernel_model.name = data.kernel.name; |
|
153 | 165 | this.kernel_model.id = data.kernel.id; |
|
154 | 166 | } |
|
155 | 167 | }; |
|
156 | 168 | |
|
157 | 169 | Session.prototype._on_success = function (success) { |
|
158 | 170 | var that = this; |
|
159 | 171 | return function (data, status, xhr) { |
|
160 | 172 | that._update_model(data); |
|
161 | 173 | if (success) { |
|
162 | 174 | success(data, status, xhr); |
|
163 | 175 | } |
|
164 | 176 | }; |
|
165 | 177 | }; |
|
166 | 178 | |
|
167 | 179 | Session.prototype._on_error = function (error) { |
|
168 | 180 | return function (xhr, status, err) { |
|
169 | 181 | utils.log_ajax_error(xhr, status, err); |
|
170 | 182 | if (error) { |
|
171 | 183 | error(xhr, status, err); |
|
172 | 184 | } |
|
173 | 185 | }; |
|
174 | 186 | }; |
|
175 | 187 | |
|
176 | 188 | |
|
177 | 189 | var SessionAlreadyStarting = function (message) { |
|
178 | 190 | this.name = "SessionAlreadyStarting"; |
|
179 | 191 | this.message = (message || ""); |
|
180 | 192 | }; |
|
181 | 193 | |
|
182 | 194 | SessionAlreadyStarting.prototype = Error.prototype; |
|
183 | 195 | |
|
184 | 196 | // For backwards compatability. |
|
185 | 197 | IPython.Session = Session; |
|
186 | 198 | |
|
187 | 199 | return { |
|
188 | 200 | Session: Session, |
|
189 | 201 | SessionAlreadyStarting: SessionAlreadyStarting |
|
190 | 202 | }; |
|
191 | 203 | }); |
@@ -1,24 +1,110 | |||
|
1 | 1 | |
|
2 | 2 | // |
|
3 | 3 | // Tests for the Session object |
|
4 | 4 | // |
|
5 | 5 | |
|
6 | 6 | casper.notebook_test(function () { |
|
7 | var that = this; | |
|
8 | var get_info = function () { | |
|
9 | return that.evaluate(function () { | |
|
10 | return JSON.parse(JSON.stringify(IPython.notebook.session._get_model())); | |
|
11 | }); | |
|
12 | }; | |
|
13 | ||
|
14 | // test that the kernel is running | |
|
7 | 15 | this.then(function () { |
|
8 | 16 | this.test.assert(this.kernel_running(), 'session: kernel is running'); |
|
9 | 17 | }); |
|
10 | 18 | |
|
19 | // test list | |
|
11 | 20 | this.thenEvaluate(function () { |
|
12 |
IPython. |
|
|
21 | IPython._sessions = null; | |
|
22 | IPython.notebook.session.list(function (data) { | |
|
23 | IPython._sessions = data; | |
|
24 | }); | |
|
25 | }); | |
|
26 | this.waitFor(function () { | |
|
27 | return this.evaluate(function () { | |
|
28 | return IPython._sessions !== null; | |
|
29 | }); | |
|
30 | }); | |
|
31 | this.then(function () { | |
|
32 | var num_sessions = this.evaluate(function () { | |
|
33 | return IPython._sessions.length; | |
|
34 | }); | |
|
35 | this.test.assertEquals(num_sessions, 1, 'one session running'); | |
|
13 | 36 | }); |
|
14 | 37 | |
|
38 | // test get_info | |
|
39 | var session_info = get_info(); | |
|
40 | this.thenEvaluate(function () { | |
|
41 | IPython._session_info = null; | |
|
42 | IPython.notebook.session.get_info(function (data) { | |
|
43 | IPython._session_info = data; | |
|
44 | }); | |
|
45 | }); | |
|
15 | 46 | this.waitFor(function () { |
|
16 | 47 | return this.evaluate(function () { |
|
17 | return IPython.notebook.kernel.is_fully_disconnected(); | |
|
48 | return IPython._session_info !== null; | |
|
18 | 49 | }); |
|
19 | 50 | }); |
|
51 | this.then(function () { | |
|
52 | var new_session_info = this.evaluate(function () { | |
|
53 | return IPython._session_info; | |
|
54 | }); | |
|
55 | this.test.assertEquals(session_info.notebook.name, new_session_info.notebook.name, 'session: notebook name correct'); | |
|
56 | this.test.assertEquals(session_info.notebook.path, new_session_info.notebook.path, 'session: notebook path correct'); | |
|
57 | this.test.assertEquals(session_info.kernel.name, new_session_info.kernel.name, 'session: kernel name correct'); | |
|
58 | this.test.assertEquals(session_info.kernel.id, new_session_info.kernel.id, 'session: kernel id correct'); | |
|
59 | }); | |
|
60 | ||
|
61 | // test rename_notebook | |
|
62 | // | |
|
63 | // TODO: the PATCH request isn't supported by phantom, so this test always | |
|
64 | // fails, see https://github.com/ariya/phantomjs/issues/11384 | |
|
65 | // when this is fixed we can properly run this test | |
|
66 | // | |
|
67 | // this.thenEvaluate(function () { | |
|
68 | // IPython._renamed = false; | |
|
69 | // IPython.notebook.session.rename_notebook( | |
|
70 | // "foo", | |
|
71 | // "bar", | |
|
72 | // function (data) { | |
|
73 | // IPython._renamed = true; | |
|
74 | // } | |
|
75 | // ); | |
|
76 | // }); | |
|
77 | // this.waitFor(function () { | |
|
78 | // return this.evaluate(function () { | |
|
79 | // return IPython._renamed; | |
|
80 | // }); | |
|
81 | // }); | |
|
82 | // this.then(function () { | |
|
83 | // var info = get_info(); | |
|
84 | // this.test.assertEquals(info.notebook.name, "foo", "notebook was renamed"); | |
|
85 | // this.test.assertEquals(info.notebook.path, "bar", "notebook path was changed"); | |
|
86 | // }); | |
|
20 | 87 | |
|
88 | // test delete | |
|
89 | this.thenEvaluate(function () { | |
|
90 | IPython.notebook.session.delete(); | |
|
91 | }); | |
|
92 | this.waitFor(function () { | |
|
93 | return this.evaluate(function () { | |
|
94 | return IPython.notebook.kernel.is_fully_disconnected(); | |
|
95 | }); | |
|
96 | }); | |
|
21 | 97 | this.then(function () { |
|
22 | 98 | this.test.assert(!this.kernel_running(), 'session deletes kernel'); |
|
23 | 99 | }); |
|
100 | ||
|
101 | // test start after delete -- should throw an error | |
|
102 | this.then(function () { | |
|
103 | var start = function () { | |
|
104 | this.evaluate(function () { | |
|
105 | IPython.notebook.session.start(); | |
|
106 | }); | |
|
107 | }; | |
|
108 | this.test.assertRaises(start, [], 'error raised on start after delete'); | |
|
109 | }); | |
|
24 | 110 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now