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