B2G/gecko/netwerk/test/httpserver/test/test_processasync.js
/*
 * Tests that accessing output stream *before* calling processAsync() works
 * correctly, sending written data immediately as it is written, not buffering
 * until finish() is called -- which for this much data would mean we would all
 * but certainly deadlock, since we're trying to read/write all this data in one
 * process on a single thread.
 */
Tests that accessing output stream *before* calling processAsync() works
correctly, sending written data immediately as it is written, not buffering
until finish() is called -- which for this much data would mean we would all
but certainly deadlock, since we're trying to read/write all this data in one
process on a single thread.
Tests that accessing output stream * before * calling processAsync () works correctly sending written data immediately as it is written not buffering until finish () is called -- which for this much data would mean we would all but certainly deadlock since we ' re trying to read / write all this data in one process on a single thread .
function handleAsyncOrdering(request, response)
{
  var out = new BinaryOutputStream(response.bodyOutputStream);
  var data = [];
  for (var i = 0; i < 65536; i++)
    data[i] = 0;
  var count = 20;
  var writeData =
    {
      run: function()
      {
        if (count-- === 0)
        {
          response.finish();
          return;
        }
        try
        {
          out.writeByteArray(data, data.length);
          step();
        }
        catch (e)
        {
          try
          {
            do_throw("error writing data: " + e);
          }
          finally
          {
            response.finish();
          }
        }
      }
    };
  function step()
  {
    gThreadManager.currentThread
                  .dispatch(writeData, Ci.nsIThread.DISPATCH_NORMAL);
  }
  step();
  response.processAsync();
}
function handleAsyncOrdering ( request , response ) { var out = new BinaryOutputStream ( response . bodyOutputStream ) ; var data = [ ] ; for ( var i = 0 ; i < 65536 ; i ++ ) data [ i ] = 0 ; var count = 20 ; var writeData = { run : function ( ) { if ( count -- === 0 ) { response . finish ( ) ; return ; } try { out . writeByteArray ( data , data . length ) ; step ( ) ; } catch ( e ) { try { do_throw ( "error writing data: " + e ) ; } finally { response . finish ( ) ; } } } } ; function step ( ) { gThreadManager . currentThread . dispatch ( writeData , Ci . nsIThread . DISPATCH_NORMAL ) ; } step ( ) ; response . processAsync ( ) ; }
Tests that accessing output stream *before* calling processAsync() works
 correctly, sending written data immediately as it is written, not buffering
 until finish() is called -- which for this much data would mean we would all
 but certainly deadlock, since we're trying to read/write all this data in one
 process on a single thread.
Tests that accessing output stream * before * calling processAsync () works correctly sending written data immediately as it is written not buffering until finish () is called -- which for this much data would mean we would all but certainly deadlock since we ' re trying to read / write all this data in one process on a single thread .
// Use gThreadManager here because it's expedient, *not* because it's // intended for public use!  If you do this in client code, expect me to // knowingly break your code by changing the variable name.  :-P