test_sjs_object_state.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http: *www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is httpd.js code.
17  *
18  * The Initial Developer of the Original Code is
19  * the Mozilla Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 2008
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Jeff Walden <jwalden+code@mit.edu>
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 /*
41  * Tests that the object-state-preservation mechanism works correctly.
42  */
43 
44 const PORT = 4444;
45 
46 const PATH = "http://localhost:" + PORT + "/object-state.sjs";
47 
48 var srv;
49 
50 function run_test()
51 {
52  srv = createServer();
53  var sjsDir = do_get_file("data/sjs/");
54  srv.registerDirectory("/", sjsDir);
55  srv.registerContentType("sjs", "sjs");
56  srv.start(PORT);
57 
58  do_test_pending();
59 
60  new HTTPTestLoader(PATH + "?state=initial", initialStart, initialStop);
61 }
62 
63 /********************
64  * OBSERVER METHODS *
65  ********************/
66 
67 /*
68  * In practice the current implementation will guarantee an exact ordering of
69  * all start and stop callbacks. However, in the interests of robustness, this
70  * test will pass given any valid ordering of callbacks. Any ordering of calls
71  * which obeys the partial ordering shown by this directed acyclic graph will be
72  * handled correctly:
73  *
74  * initialStart
75  * |
76  * V
77  * intermediateStart
78  * |
79  * V
80  * intermediateStop
81  * | \
82  * | V
83  * | initialStop
84  * V
85  * triggerStart
86  * |
87  * V
88  * triggerStop
89  *
90  */
91 
92 var initialStarted = false;
93 function initialStart(ch, cx)
94 {
95  dumpn("*** initialStart");
96 
97  if (initialStarted)
98  do_throw("initialStart: initialStarted is true?!?!");
99 
100  initialStarted = true;
101 
102  new HTTPTestLoader(PATH + "?state=intermediate",
104 }
105 
106 var initialStopped = false;
107 function initialStop(ch, cx, status, data)
108 {
109  dumpn("*** initialStop");
110 
111  do_check_eq(data.map(function(v) { return String.fromCharCode(v); }).join(""),
112  "done");
113 
114  do_check_eq(srv.getObjectState("object-state-test"), null);
115 
116  if (!initialStarted)
117  do_throw("initialStop: initialStarted is false?!?!");
118  if (initialStopped)
119  do_throw("initialStop: initialStopped is true?!?!");
120  if (!intermediateStarted)
121  do_throw("initialStop: intermediateStarted is false?!?!");
122  if (!intermediateStopped)
123  do_throw("initialStop: intermediateStopped is false?!?!");
124 
125  initialStopped = true;
126 
127  checkForFinish();
128 }
129 
131 function intermediateStart(ch, cx)
132 {
133  dumpn("*** intermediateStart");
134 
135  do_check_neq(srv.getObjectState("object-state-test"), null);
136 
137  if (!initialStarted)
138  do_throw("intermediateStart: initialStarted is false?!?!");
139  if (intermediateStarted)
140  do_throw("intermediateStart: intermediateStarted is true?!?!");
141 
142  intermediateStarted = true;
143 }
144 
146 function intermediateStop(ch, cx, status, data)
147 {
148  dumpn("*** intermediateStop");
149 
150  do_check_eq(data.map(function(v) { return String.fromCharCode(v); }).join(""),
151  "intermediate");
152 
153  do_check_neq(srv.getObjectState("object-state-test"), null);
154 
155  if (!initialStarted)
156  do_throw("intermediateStop: initialStarted is false?!?!");
157  if (!intermediateStarted)
158  do_throw("intermediateStop: intermediateStarted is false?!?!");
159  if (intermediateStopped)
160  do_throw("intermediateStop: intermediateStopped is true?!?!");
161 
162  intermediateStopped = true;
163 
164  new HTTPTestLoader(PATH + "?state=trigger", triggerStart,
165  triggerStop);
166 }
167 
168 var triggerStarted = false;
169 function triggerStart(ch, cx)
170 {
171  dumpn("*** triggerStart");
172 
173  if (!initialStarted)
174  do_throw("triggerStart: initialStarted is false?!?!");
175  if (!intermediateStarted)
176  do_throw("triggerStart: intermediateStarted is false?!?!");
177  if (!intermediateStopped)
178  do_throw("triggerStart: intermediateStopped is false?!?!");
179  if (triggerStarted)
180  do_throw("triggerStart: triggerStarted is true?!?!");
181 
182  triggerStarted = true;
183 }
184 
185 var triggerStopped = false;
186 function triggerStop(ch, cx, status, data)
187 {
188  dumpn("*** triggerStop");
189 
190  do_check_eq(data.map(function(v) { return String.fromCharCode(v); }).join(""),
191  "trigger");
192 
193  if (!initialStarted)
194  do_throw("triggerStop: initialStarted is false?!?!");
195  if (!intermediateStarted)
196  do_throw("triggerStop: intermediateStarted is false?!?!");
197  if (!intermediateStopped)
198  do_throw("triggerStop: intermediateStopped is false?!?!");
199  if (!triggerStarted)
200  do_throw("triggerStop: triggerStarted is false?!?!");
201  if (triggerStopped)
202  do_throw("triggerStop: triggerStopped is false?!?!");
203 
204  triggerStopped = true;
205 
206  checkForFinish();
207 }
208 
209 var finished = false;
210 function checkForFinish()
211 {
212  if (finished)
213  {
214  try
215  {
216  do_throw("uh-oh, how are we being finished twice?!?!");
217  }
218  finally
219  {
220  quit(1);
221  }
222  }
223 
224  if (triggerStopped && initialStopped)
225  {
226  finished = true;
227  try
228  {
229  do_check_eq(srv.getObjectState("object-state-test"), null);
230 
231  if (!initialStarted)
232  do_throw("checkForFinish: initialStarted is false?!?!");
233  if (!intermediateStarted)
234  do_throw("checkForFinish: intermediateStarted is false?!?!");
235  if (!intermediateStopped)
236  do_throw("checkForFinish: intermediateStopped is false?!?!");
237  if (!triggerStarted)
238  do_throw("checkForFinish: triggerStarted is false?!?!");
239  }
240  finally
241  {
242  srv.stop(do_test_finished);
243  }
244  }
245 }
246 
247 
248 /*********************************
249  * UTILITY OBSERVABLE URL LOADER *
250  *********************************/
251 
254 {
256  this._path = path;
257 
259  this._data = [];
260 
262  this._start = start;
263 
265  this._stop = stop;
266 
267  var channel = makeChannel(path);
268  channel.asyncOpen(this, null);
269 }
270 HTTPTestLoader.prototype =
271  {
272  onStartRequest: function(request, cx)
273  {
274  dumpn("*** HTTPTestLoader.onStartRequest for " + this._path);
275 
276  var ch = request.QueryInterface(Ci.nsIHttpChannel)
277  .QueryInterface(Ci.nsIHttpChannelInternal);
278 
279  try
280  {
281  try
282  {
283  this._start(ch, cx);
284  }
285  catch (e)
286  {
287  do_throw(this._path + ": error in onStartRequest: " + e);
288  }
289  }
290  catch (e)
291  {
292  dumpn("!!! swallowing onStartRequest exception so onStopRequest is " +
293  "called...");
294  }
295  },
296  onDataAvailable: function(request, cx, inputStream, offset, count)
297  {
298  dumpn("*** HTTPTestLoader.onDataAvailable for " + this._path);
299 
300  Array.prototype.push.apply(this._data,
301  makeBIS(inputStream).readByteArray(count));
302  },
303  onStopRequest: function(request, cx, status)
304  {
305  dumpn("*** HTTPTestLoader.onStopRequest for " + this._path);
306 
307  var ch = request.QueryInterface(Ci.nsIHttpChannel)
308  .QueryInterface(Ci.nsIHttpChannelInternal);
309 
310  this._stop(ch, cx, status, this._data);
311  },
312  QueryInterface: function(aIID)
313  {
314  dumpn("*** QueryInterface: " + aIID);
315 
316  if (aIID.equals(Ci.nsIStreamListener) ||
317  aIID.equals(Ci.nsIRequestObserver) ||
318  aIID.equals(Ci.nsISupports))
319  return this;
320  throw Cr.NS_ERROR_NO_INTERFACE;
321  }
322  };
function start(ch)
do_check_eq(typeof PlacesUtils,"object")
var triggerStopped
function stop(ch, cx, status, data)
function makeBIS(stream)
Definition: head_utils.js:80
const PORT
function initialStop(ch, cx, status, data)
sbOSDControlService prototype QueryInterface
var intermediateStopped
function run_test()
function HTTPTestLoader(path, start, stop)
function checkForFinish()
function dumpn(str)
Definition: httpd.js:172
var intermediateStarted
PRUint32 & offset
function createServer()
Definition: head_utils.js:53
var count
Definition: test_bug7406.js:32
function triggerStop(ch, cx, status, data)
const PATH
var finished
function triggerStart(ch, cx)
function makeChannel(url)
Definition: head_utils.js:64
return null
Definition: FeedWriter.js:1143
var initialStarted
function intermediateStart(ch, cx)
var initialStopped
const Cr
var triggerStarted
const Ci
function initialStart(ch, cx)
observe data
Definition: FeedWriter.js:1329
function intermediateStop(ch, cx, status, data)