File: test_ltp.rb

package info (click to toggle)
ruby-eventmachine 1.0.3-6%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,000 kB
  • ctags: 3,178
  • sloc: ruby: 8,641; cpp: 5,217; java: 827; makefile: 5
file content (138 lines) | stat: -rw-r--r-- 3,356 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require 'em_test_helper'

class TestLineAndTextProtocol < Test::Unit::TestCase

  class SimpleLineTest < EM::P::LineAndTextProtocol
    def receive_line line
      @line_buffer << line
    end
  end

  module StopClient
    def set_receive_data(&blk)
      @rdb = blk
    end

    def receive_data data
      @rdb.call(data) if @rdb
    end

    def unbind
      EM.add_timer(0.1) { EM.stop }
    end
  end

  def setup
    @port = next_port
  end

  def test_simple_lines
    lines_received = []
    EM.run {
      EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
        conn.instance_eval "@line_buffer = lines_received"
      end
      setup_timeout

      EM.connect "127.0.0.1", @port, StopClient do |c|
        c.send_data "aaa\nbbb\r\nccc\n"
        c.close_connection_after_writing
      end
    }
    assert_equal( %w(aaa bbb ccc), lines_received )
  end

  #--------------------------------------------------------------------

  class SimpleLineTest < EM::P::LineAndTextProtocol
    def receive_error text
      @error_message << text
    end
  end

  def test_overlength_lines
    lines_received = []
    EM.run {
      EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
        conn.instance_eval "@error_message = lines_received"
      end
      setup_timeout

      EM.connect "127.0.0.1", @port, StopClient do |c|
        c.send_data "a" * (16*1024 + 1)
        c.send_data "\n"
        c.close_connection_after_writing
      end

    }
    assert_equal( ["overlength line"], lines_received )
  end


  #--------------------------------------------------------------------

  class LineAndTextTest < EM::P::LineAndTextProtocol
    def receive_line line
      if line =~ /content-length:\s*(\d+)/i
        @content_length = $1.to_i
      elsif line.length == 0
        set_binary_mode @content_length
      end
    end
    def receive_binary_data text
      send_data "received #{text.length} bytes"
      close_connection_after_writing
    end
  end

  def test_lines_and_text
    output = ''
    EM.run {
      EM.start_server( "127.0.0.1", @port, LineAndTextTest )
      setup_timeout

      EM.connect "127.0.0.1", @port, StopClient do |c|
        c.set_receive_data { |data| output << data }
        c.send_data "Content-length: 400\n"
        c.send_data "\n"
        c.send_data "A" * 400
        EM.add_timer(0.1) { c.close_connection_after_writing }
      end
    }
    assert_equal( "received 400 bytes", output )
  end

  #--------------------------------------------------------------------


  class BinaryTextTest < EM::P::LineAndTextProtocol
    def receive_line line
      if line =~ /content-length:\s*(\d+)/i
        set_binary_mode $1.to_i
      else
        raise "protocol error"
      end
    end
    def receive_binary_data text
      send_data "received #{text.length} bytes"
      close_connection_after_writing
    end
  end

  def test_binary_text
    output = ''
    EM.run {
      EM.start_server( "127.0.0.1", @port, BinaryTextTest )
      setup_timeout

      EM.connect "127.0.0.1", @port, StopClient do |c|
        c.set_receive_data { |data| output << data }
        c.send_data "Content-length: 10000\n"
        c.send_data "A" * 10000
        EM.add_timer(0.1) { c.close_connection_after_writing }
      end
    }
    assert_equal( "received 10000 bytes", output )
  end

end