root/trunk/tests/test_ltp.rb

Revision 788, 5.2 kB (checked in by raggi, 9 months ago)

Merge of branches/raggi
Most notable work and patches by Aman Gupta, Roger Pack, and James Tucker.
Patches / Tickets also submitted by: Jeremy Evans, aanand, darix, mmmurf,
danielaquino, macournoyer.

  • Moved docs into docs/ dir
  • Major refactor of rakefile, added generic rakefile helpers in tasks
  • Added example CPP build rakefile in tasks/cpp.rake
  • Moved rake tests out to tasks/tests.rake
  • Added svn ignores where appropriate
  • Fixed jruby build on older java platforms
  • Gem now builds from Rakefile rather than directly via extconf
  • Gem unified for jruby, C++ and pure ruby.
  • Correction for pure C++ build, removing ruby dependency
  • Fix for CYGWIN builds on ipv6
  • Major refactor for extconf.rb
  • Working mingw builds
  • extconf optionally uses pkg_config over manual configuration
  • extconf builds for 1.9 on any system that has 1.9
  • extconf no longer links pthread explicitly
  • looks for kqueue on all *nix systems
  • better error output on std::runtime_error, now says where it came from
  • Fixed some tests on jruby
  • Added test for general send_data flaw, required for a bugfix in jruby build
  • Added timeout to epoll tests
  • Added fixes for java reactor ruby api
  • Small addition of some docs in httpclient.rb and httpcli2.rb
  • Some refactor and fixes in smtpserver.rb
  • Added parenthesis where possible to avoid excess ruby warnings
  • Refactor of $eventmachine_library logic for accuracy and maintenance, jruby
  • EM::start_server now supports unix sockets
  • EM::connect now supports unix sockets
  • EM::defer @threadqueue now handled more gracefully
  • Added better messages on exceptions raised
  • Fix edge case in timer fires
  • Explicitly require buftok.rb
  • Add protocols to autoload, rather than require them all immediately
  • Fix a bug in pr_eventmachine for outbound_q
  • Refactors to take some of the use of defer out of tests.
  • Fixes in EM.defer under start/stop conditions. Reduced scope of threads.
  • Property svn:keywords set to Id
Line 
1 # $Id$
2 #
3 # Author:: Francis Cianfrocca (gmail: blackhedd)
4 # Homepage::  http://rubyeventmachine.com
5 # Date:: 8 April 2006
6 #
7 # See EventMachine and EventMachine::Connection for documentation and
8 # usage examples.
9 #
10 #----------------------------------------------------------------------------
11 #
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13 # Gmail: blackhedd
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of either: 1) the GNU General Public License
17 # as published by the Free Software Foundation; either version 2 of the
18 # License, or (at your option) any later version; or 2) Ruby's License.
19 #
20 # See the file COPYING for complete licensing information.
21 #
22 #---------------------------------------------------------------------------
23 #
24 #
25 #
26 #
27
28 require 'eventmachine'
29 require 'test/unit'
30
31 class TestLineAndTextProtocol < Test::Unit::TestCase
32
33   TestHost = "127.0.0.1"
34   TestPort = 8905
35
36
37   #--------------------------------------------------------------------
38
39   class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
40     def receive_line line
41       @line_buffer << line
42     end
43   end
44
45   module StopClient
46     def set_receive_data(&blk)
47       @rdb = blk
48     end
49    
50     def receive_data data
51       @rdb.call(data) if @rdb
52     end
53    
54     def unbind
55       EM.add_timer(0.1) { EM.stop }
56     end
57   end
58
59
60   def test_simple_lines
61     # THIS TEST CURRENTLY FAILS IN JRUBY.
62     assert( RUBY_PLATFORM !~ /java/ )
63
64     lines_received = []
65     Thread.abort_on_exception = true
66     EventMachine.run {
67       EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
68         conn.instance_eval "@line_buffer = lines_received"
69       end
70       EventMachine.add_timer(4) {assert(false, "test timed out")}
71
72       EventMachine.connect TestHost, TestPort, StopClient do |c|
73         c.send_data "aaa\nbbb\r\nccc\n"
74         c.close_connection_after_writing
75       end
76     }
77     assert_equal( %w(aaa bbb ccc), lines_received )
78   end
79
80   #--------------------------------------------------------------------
81
82   class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
83     def receive_error text
84       @error_message << text
85     end
86   end
87
88   def test_overlength_lines
89     # THIS TEST CURRENTLY FAILS IN JRUBY.
90     assert( RUBY_PLATFORM !~ /java/ )
91
92     lines_received = []
93     Thread.abort_on_exception = true
94     EventMachine.run {
95       EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
96         conn.instance_eval "@error_message = lines_received"
97       end
98       EventMachine.add_timer(4) {assert(false, "test timed out")}
99
100       EventMachine.connect TestHost, TestPort, StopClient do |c|
101         c.send_data "a" * (16*1024 + 1)
102         c.send_data "\n"
103         c.close_connection_after_writing
104       end
105
106     }
107     assert_equal( ["overlength line"], lines_received )
108   end
109
110
111   #--------------------------------------------------------------------
112
113   class LineAndTextTest < EventMachine::Protocols::LineAndTextProtocol
114     def post_init
115     end
116     def receive_line line
117       if line =~ /content-length:\s*(\d+)/i
118         @content_length = $1.to_i
119       elsif line.length == 0
120         set_binary_mode @content_length
121       end
122     end
123     def receive_binary_data text
124       send_data "received #{text.length} bytes"
125       close_connection_after_writing
126     end
127   end
128
129   def test_lines_and_text
130     output = ''
131     lines_received = []
132     text_received = []
133     Thread.abort_on_exception = true
134     EventMachine.run {
135       EventMachine.start_server( TestHost, TestPort, LineAndTextTest ) do |conn|
136         conn.instance_eval "@lines = lines_received; @text = text_received"
137       end
138       EventMachine.add_timer(4) {assert(false, "test timed out")}
139
140       EventMachine.connect TestHost, TestPort, StopClient do |c|
141         c.set_receive_data { |data| output << data }
142         c.send_data "Content-length: 400\n"
143         c.send_data "\n"
144         c.send_data "A" * 400
145         EM.add_timer(0.1) { c.close_connection_after_writing }
146       end
147     }
148     assert_equal( "received 400 bytes", output )
149   end
150
151   #--------------------------------------------------------------------
152
153
154   class BinaryTextTest < EventMachine::Protocols::LineAndTextProtocol
155     def post_init
156     end
157     def receive_line line
158       if line =~ /content-length:\s*(\d+)/i
159         set_binary_mode $1.to_i
160       else
161         raise "protocol error"
162       end
163     end
164     def receive_binary_data text
165       send_data "received #{text.length} bytes"
166       close_connection_after_writing
167     end
168   end
169
170   def test_binary_text
171     output = ''
172     lines_received = []
173     text_received = []
174     Thread.abort_on_exception = true
175     EventMachine.run {
176       EventMachine.start_server( TestHost, TestPort, BinaryTextTest ) do |conn|
177         conn.instance_eval "@lines = lines_received; @text = text_received"
178       end
179       EventMachine.add_timer(4) {assert(false, "test timed out")}
180
181       EventMachine.connect TestHost, TestPort, StopClient do |c|
182         c.set_receive_data { |data| output << data }
183         c.send_data "Content-length: 10000\n"
184         c.send_data "A" * 10000
185         EM.add_timer(0.2) { c.close_connection_after_writing }
186       end
187     }
188     assert_equal( "received 10000 bytes", output )
189   end
190
191   #--------------------------------------------------------------------
192 end
Note: See TracBrowser for help on using the browser.