test_start_stop.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) 2009
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 for correct behavior of the server start() and stop() methods.
42  */
43 
44 const PORT = 4444;
45 const PREPATH = "http://localhost:" + PORT;
46 
47 var srv, srv2;
48 
49 function run_test()
50 {
51  if ("@mozilla.org/windows-registry-key;1" in Components.classes)
52  {
53  dumpn("*** not running test_start_stop.js on Windows for now, because " +
54  "Windows is dumb");
55  return;
56  }
57 
58  dumpn("*** run_test");
59 
60  srv = createServer();
61  srv.start(PORT);
62 
63  try
64  {
65  srv.start(PORT);
66  do_throw("starting a started server");
67  }
68  catch (e)
69  {
70  isException(e, Cr.NS_ERROR_ALREADY_INITIALIZED);
71  }
72 
73  try
74  {
75  srv.stop();
76  do_throw("missing argument to stop");
77  }
78  catch (e)
79  {
80  isException(e, Cr.NS_ERROR_NULL_POINTER);
81  }
82 
83  try
84  {
85  srv.stop(null);
86  do_throw("null argument to stop");
87  }
88  catch (e)
89  {
90  isException(e, Cr.NS_ERROR_NULL_POINTER);
91  }
92 
93  do_test_pending();
94  srv.stop(function()
95  {
96  try
97  {
98  do_test_pending();
99  run_test_2();
100  }
101  finally
102  {
103  do_test_finished();
104  }
105  });
106 }
107 
108 function run_test_2()
109 {
110  dumpn("*** run_test_2");
111 
112  do_test_finished();
113 
114  srv.start(PORT);
115  srv2 = createServer();
116 
117  try
118  {
119  srv2.start(PORT);
120  do_throw("two servers on one port?");
121  }
122  catch (e)
123  {
124  isException(e, Cr.NS_ERROR_NOT_AVAILABLE);
125  }
126 
127  do_test_pending();
128  try
129  {
130  srv.stop({onStopped: function()
131  {
132  try
133  {
134  do_test_pending();
135  run_test_3();
136  }
137  finally
138  {
139  do_test_finished();
140  }
141  }
142  });
143  }
144  catch (e)
145  {
146  do_throw("error stopping with an object: " + e);
147  }
148 }
149 
150 function run_test_3()
151 {
152  dumpn("*** run_test_3");
153 
154  do_test_finished();
155 
156  srv.registerPathHandler("/handle", handle);
157  srv.start(PORT);
158 
159  // Don't rely on the exact (but implementation-constant) sequence of events
160  // as it currently exists by making either run_test_4 or serverStopped handle
161  // the final shutdown.
162  do_test_pending();
163 
164  runHttpTests([new Test(PREPATH + "/handle")], run_test_4);
165 }
166 
167 var testsComplete = false;
168 
169 function run_test_4()
170 {
171  dumpn("*** run_test_4");
172 
173  testsComplete = true;
174  if (stopped)
175  do_test_finished();
176 }
177 
178 
179 const INTERVAL = 500;
180 
181 function handle(request, response)
182 {
183  response.processAsync();
184 
185  dumpn("*** stopping server...");
186  srv.stop(serverStopped);
187 
188  callLater(INTERVAL, function()
189  {
190  do_check_false(stopped);
191 
192  callLater(INTERVAL, function()
193  {
194  do_check_false(stopped);
195  response.finish();
196 
197  try
198  {
199  response.processAsync();
200  do_throw("late processAsync didn't throw?");
201  }
202  catch (e)
203  {
204  isException(e, Cr.NS_ERROR_UNEXPECTED);
205  }
206  });
207  });
208 }
209 
210 var stopped = false;
211 function serverStopped()
212 {
213  dumpn("*** server really, fully shut down now");
214  stopped = true;
215  if (testsComplete)
216  do_test_finished();
217 }
function run_test_4()
const INTERVAL
function run_test_2()
function runHttpTests(testArray, done)
Definition: head_utils.js:340
function run_test_3()
function handle(request, response)
function dumpn(str)
Definition: httpd.js:172
function serverStopped()
function run_test()
function createServer()
Definition: head_utils.js:53
var testsComplete
const PORT
return null
Definition: FeedWriter.js:1143
var srv
const PREPATH
function Test(path, initChannel, onStartRequest, onStopRequest)
Definition: head_utils.js:322
const Cr
var srv2
function callLater(msecs, callback)
Definition: head_utils.js:237
function isException(e, code)
Definition: head_utils.js:207
var stopped