README: update with extra disclaimer
[raindrops.git] / test / test_linux_middleware.rb
blob7ed20df3d3020c6497176ce4a3cef22cc3505b64
1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
3 require 'test/unit'
4 require 'tempfile'
5 require 'raindrops'
6 require 'socket'
7 $stderr.sync = $stdout.sync = true
9 class TestLinuxMiddleware < Test::Unit::TestCase
11   def setup
12     @resp_headers = { 'Content-Type' => 'text/plain', 'Content-Length' => '0' }
13     @response = [ 200, @resp_headers, [] ]
14     @app = lambda { |env| @response }
15     @to_close = []
16   end
18   def teardown
19     @to_close.each { |io| io.close unless io.closed? }
20   end
22   def test_unix_listener
23     tmp = Tempfile.new("")
24     File.unlink(tmp.path)
25     @to_close << UNIXServer.new(tmp.path)
26     app = Raindrops::Middleware.new(@app, :listeners => [tmp.path])
27     linux_extra = "#{tmp.path} active: 0\n#{tmp.path} queued: 0\n"
28     response = app.call("PATH_INFO" => "/_raindrops")
30     expect = [
31       200,
32       {
33         "Content-Type" => "text/plain",
34         "Content-Length" => (22 + linux_extra.size).to_s
35       },
36       [
37         "calling: 0\nwriting: 0\n#{linux_extra}" \
38       ]
39     ]
40     assert_equal expect, response
41   end
43   def test_unix_listener_queued
44     tmp = Tempfile.new("")
45     File.unlink(tmp.path)
46     @to_close << UNIXServer.new(tmp.path)
47     @to_close << UNIXSocket.new(tmp.path)
48     app = Raindrops::Middleware.new(@app, :listeners => [tmp.path])
49     linux_extra = "#{tmp.path} active: 0\n#{tmp.path} queued: 1\n"
50     response = app.call("PATH_INFO" => "/_raindrops")
52     expect = [
53       200,
54       {
55         "Content-Type" => "text/plain",
56         "Content-Length" => (22 + linux_extra.size).to_s
57       },
58       [
59         "calling: 0\nwriting: 0\n#{linux_extra}" \
60       ]
61     ]
62     assert_equal expect, response
63   end
65 end if RUBY_PLATFORM =~ /linux/