File: test_connection_count.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 (83 lines) | stat: -rw-r--r-- 1,852 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
require_relative 'em_test_helper'

class TestConnectionCount < Test::Unit::TestCase
  def teardown
    EM.epoll = false
    EM.kqueue = false
  end

  def test_idle_connection_count
    count = nil
    EM.run {
      count = EM.connection_count
      EM.stop_event_loop
    }
    assert_equal(0, count)
  end

  # Run this again with epoll enabled (if available)
  def test_idle_connection_count_epoll
    EM.epoll if EM.epoll?

    count = nil
    EM.run {
      count = EM.connection_count
      EM.stop_event_loop
    }
    assert_equal(0, count)
  end

  # Run this again with kqueue enabled (if available)
  def test_idle_connection_count_kqueue
    EM.kqueue if EM.kqueue?

    count = nil
    EM.run {
      count = EM.connection_count
      EM.stop_event_loop
    }
    assert_equal(0, count)
  end

  module Client
    def connection_completed
      $client_conns += 1
      EM.stop if $client_conns == 3
    end
  end

  def test_with_some_connections
    EM.run {
      $client_conns = 0
      $initial_conns = EM.connection_count
      EM.start_server("localhost", 9998)
      $server_conns = EM.connection_count
      3.times { EM.connect("localhost", 9998, Client) }
    }

    assert_equal(0, $initial_conns)
    assert_equal(1, $server_conns)
    assert_equal(4, $client_conns + $server_conns)
  end

  module DoubleCloseClient
    def unbind
      close_connection
      $num_close_scheduled_1 = EM.num_close_scheduled
      EM.next_tick do
        $num_close_scheduled_2 = EM.num_close_scheduled
        EM.stop
      end
    end
  end

  def test_num_close_scheduled
    omit_if(jruby?)
    EM.run {
      assert_equal(0, EM.num_close_scheduled)
      EM.connect("localhost", 9998, DoubleCloseClient) # nothing listening on 9998
    }
    assert_equal(1, $num_close_scheduled_1)
    assert_equal(0, $num_close_scheduled_2)
  end
end