root/trunk/tests/test_attach.rb

Revision 785, 1.4 kB (checked in by francis, 10 months ago)

Applied a patch from Aman Gupta (tmm1) with contributions from
Riham Aldakkak, adds file-descriptor attach/detach and two new
event notifications, to support file descriptors not generated
internally by the reactor.

  • Property svn:keywords set to Id
Line 
1 # $Id$
2 #
3 #----------------------------------------------------------------------------
4 #
5 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
6 # Gmail: blackhedd
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of either: 1) the GNU General Public License
10 # as published by the Free Software Foundation; either version 2 of the
11 # License, or (at your option) any later version; or 2) Ruby's License.
12 #
13 # See the file COPYING for complete licensing information.
14 #
15 #---------------------------------------------------------------------------
16 #
17
18 $:.unshift "../lib"
19 require 'eventmachine'
20 require 'socket'
21 require 'test/unit'
22
23
24 class TestAttach < Test::Unit::TestCase
25
26   Host = "127.0.0.1"
27   Port = 9550
28
29   class EchoServer < EM::Connection
30     def receive_data data
31       send_data data
32     end
33   end
34
35   class EchoClient < EM::Connection
36     def connection_completed
37       $sock.write("abc\n")
38     end
39
40     def notify_readable
41       $read = $sock.readline
42       $fd = detach
43     end
44
45     def unbind
46       EM.next_tick do
47         $sock.write("def\n")
48         EM.add_timer(0.5){ EM.stop }
49       end
50     end
51   end
52
53   def test_attach
54     EM.run{
55       EM.start_server Host, Port, EchoServer
56       $sock = TCPSocket.new Host, Port
57       EM.attach $sock, EchoClient
58     }
59
60     assert_equal $read, "abc\n"
61     assert_equal $fd, $sock.fileno
62     assert_equal false, $sock.closed?
63     assert_equal $sock.readline, "def\n"
64   end
65
66 end
Note: See TracBrowser for help on using the browser.