File: test_pause.rb

package info (click to toggle)
ruby-eventmachine 1.3~pre20201020-b50c135-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,768 kB
  • sloc: ruby: 10,597; cpp: 6,032; java: 1,136; makefile: 12
file content (109 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download
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
require_relative 'em_test_helper'

class TestPause < Test::Unit::TestCase
  if EM.respond_to? :pause_connection
    def setup
      @port = next_port
    end

    def teardown
      assert(!EM.reactor_running?)
    end

    def test_pause_resume
      server = nil

      s_rx = c_rx = 0

      test_server = Module.new do
        define_method :post_init do
          server = self
        end

        define_method :receive_data do |data|
          s_rx += 1

          EM.add_periodic_timer(0.01) { send_data 'hi' }
          send_data 'hi'

          # pause server, now no outgoing data will actually
          # be sent and no more incoming data will be received
          pause
        end
      end

      test_client = Module.new do
        def post_init
          EM.add_periodic_timer(0.01) do
            send_data 'hello'
          end
        end

        define_method :receive_data do |data|
          c_rx += 1
        end
      end

      EM.run do
        EM.start_server "localhost", @port, test_server
        EM.connect "localhost", @port, test_client

        tmr = darwin? ? 0.10 : 0.05

        EM.add_timer(tmr) do
          assert_equal 1, s_rx
          assert_equal 0, c_rx
          assert server.paused?

          # resume server, queued outgoing and incoming data will be flushed
          server.resume

          assert !server.paused?

          EM.add_timer(tmr) do
            assert server.paused?
            assert s_rx > 1
            assert c_rx > 0
            EM.stop
          end
        end
      end
    end

    def test_pause_in_receive_data
      incoming = []

      test_server = Module.new do
        define_method(:receive_data) do |data|
          incoming << data
          pause
          EM.add_timer(0.5){ close_connection }
        end
        define_method(:unbind) do
          EM.stop
        end
      end

      buf = 'a' * 1024

      EM.run do
        EM.start_server "localhost", @port, test_server
        cli = EM.connect "localhost", @port
        128.times do
          cli.send_data buf
        end
      end

      assert_equal 1, incoming.size
      assert incoming[0].bytesize > buf.bytesize
      assert incoming[0].bytesize < buf.bytesize * 128
    end
  else
    warn "EM.pause_connection not implemented, skipping tests in #{__FILE__}"

    # Because some rubies will complain if a TestCase class has no tests
    def test_em_pause_connection_not_implemented
      assert true
    end
  end
end