about summary refs log tree commit homepage
path: root/t/async_chunk_app.ru
diff options
context:
space:
mode:
Diffstat (limited to 't/async_chunk_app.ru')
-rw-r--r--t/async_chunk_app.ru44
1 files changed, 44 insertions, 0 deletions
diff --git a/t/async_chunk_app.ru b/t/async_chunk_app.ru
new file mode 100644
index 0000000..26b9915
--- /dev/null
+++ b/t/async_chunk_app.ru
@@ -0,0 +1,44 @@
+# based on async_examples/async_app.ru by James Tucker
+class DeferrableChunkBody
+  include EventMachine::Deferrable
+
+  def call(*body)
+    body.each do |chunk|
+      @body_callback.call("#{chunk.size.to_s(16)}\r\n")
+      @body_callback.call(chunk)
+      @body_callback.call("\r\n")
+    end
+  end
+
+  def each(&block)
+    @body_callback = block
+  end
+
+  def finish
+    @body_callback.call("0\r\n\r\n")
+  end
+end
+
+class AsyncChunkApp
+  def call(env)
+    body = DeferrableChunkBody.new
+    body.callback { body.finish }
+    headers = {
+      'Content-Type' => 'text/plain',
+      'Transfer-Encoding' => 'chunked',
+    }
+    EM.next_tick {
+      env['async.callback'].call([ 200, headers, body ])
+    }
+    EM.add_timer(1) {
+      body.call "Hello "
+
+      EM.add_timer(1) {
+        body.call "World #{env['PATH_INFO']}\n"
+        body.succeed
+      }
+    }
+    nil
+  end
+end
+run AsyncChunkApp.new