Bug 522416 - Tests for blocking input stream in NetUtil.asyncFetch.
authorShawn Wilsher <sdwilsh@shawnwilsher.com>
Mon, 19 Oct 2009 12:23:17 -0700
changeset 34251 3314dc670ff48b1420ee6564e15d98cd047c751c
parent 34250 8e1ea35b129498f73ad127f32a42bfd1b3d6270c
child 34252 6c793e95c534f3cd0902e5f60532c427273beaea
push id115
push userbmcbride@mozilla.com
push dateMon, 19 Oct 2009 23:27:00 +0000
bugs522416
milestone1.9.3a1pre
Bug 522416 - Tests for blocking input stream in NetUtil.asyncFetch.
netwerk/test/unit/test_NetUtil.js
--- a/netwerk/test/unit/test_NetUtil.js
+++ b/netwerk/test/unit/test_NetUtil.js
@@ -230,33 +230,60 @@ function test_asyncFetch()
     do_check_eq(aInputStream.available(), TEST_DATA.length);
     let is = Cc["@mozilla.org/scriptableinputstream;1"].
              createInstance(Ci.nsIScriptableInputStream);
     is.init(aInputStream);
     let result = is.read(TEST_DATA.length);
     do_check_eq(TEST_DATA, result);
 
     server.stop(run_next_test);
- 
+  });
+}
+
+function test_asyncFetch_does_not_block()
+{
+  // Create our channel that has no data.
+  let channel = NetUtil.ioService.
+                newChannel("data:text/plain,", null, null);
+
+  // Open our channel asynchronously.
+  NetUtil.asyncFetch(channel, function(aInputStream, aResult) {
+    // Check that we had success.
+    do_check_true(Components.isSuccessCode(aResult));
+
+    // Check that reading a byte throws that the stream was closed (as opposed
+    // saying it would block).
+    let is = Cc["@mozilla.org/scriptableinputstream;1"].
+             createInstance(Ci.nsIScriptableInputStream);
+    is.init(aInputStream);
+    try {
+      is.read(1);
+      do_throw("should throw!");
+    }
+    catch (e) {
+      do_check_eq(e.result, Cr.NS_BASE_STREAM_CLOSED);
+    }
+
     run_next_test();
   });
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Test Runner
 
 let tests = [
   test_async_write_file,
   test_async_write_file_nsISafeOutputStream,
   test_newURI_no_spec_throws,
   test_newURI,
   test_ioService,
   test_asyncFetch_no_channel,
   test_asyncFetch_no_callback,
   test_asyncFetch,
+  test_asyncFetch_does_not_block,
 ];
 let index = 0;
 
 function run_next_test()
 {
   if (index < tests.length) {
     do_test_pending();
     print("Running the next test: " + tests[index].name);