Class: TestBasic

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/ext/eventmachine-0.12.10/tests/test_basic.rb

Defined Under Namespace

Modules: BrsTestCli, BrsTestSrv, Trivial Classes: PostInitError, UnbindError

Constant Summary collapse

TestHost =

TODO! This is an unfinished edge case. EM mishandles uncaught Ruby exceptions that fire from within #unbind handlers. A uncaught Ruby exception results in a call to EM::release_machine (which is in an ensure block in EM::run). But if EM is processing an unbind request, the release_machine call will cause a segmentation fault.

"127.0.0.1"
TestPort =
9070

Instance Method Summary collapse

Instance Method Details

#setupObject



34
35
36
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 34

def setup
  assert(!EM.reactor_running?)
end

#teardownObject



38
39
40
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 38

def teardown
  assert(!EM.reactor_running?)
end

#test_bind_connectObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 229

def test_bind_connect
  local_ip = UDPSocket.open {|s| s.connect('google.com', 80); s.addr.last }

  bind_port = rand(33333)+1025

  test = self
  EM.run do
    EM.start_server(TestHost, TestPort, Module.new do
      define_method :post_init do
        begin
          test.assert_equal bind_port, Socket.unpack_sockaddr_in(get_peername).first
          test.assert_equal local_ip, Socket.unpack_sockaddr_in(get_peername).last
        ensure
          EM.stop_event_loop
        end
      end
    end)
    EM.bind_connect local_ip, bind_port, TestHost, TestPort
  end
end

#test_byte_range_sendObject

From ticket #50



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 217

def test_byte_range_send
  $received = ''
  $sent = (0..255).to_a.pack('C*')
  EM::run {
    EM::start_server TestHost, TestPort, BrsTestSrv
    EM::connect TestHost, TestPort, BrsTestCli

    EM::add_timer(0.5) { assert(false, 'test timed out'); EM.stop; Kernel.warn "test timed out!" }
  }
  assert_equal($sent, $received)
end

#test_emObject



76
77
78
79
80
81
82
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 76

def test_em
  EventMachine.run {
    EventMachine.add_timer 0 do
      EventMachine.stop
    end
  }
end

#test_libtypeObject




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
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 44

def test_libtype
  lt = EventMachine.library_type
  em_lib = (ENV["EVENTMACHINE_LIBRARY"] || $eventmachine_library || :xxx).to_sym

  # Running from test runner, under jruby.
  if RUBY_PLATFORM == 'java'
    unless em_lib == :pure_ruby
      assert_equal( :java, lt )
      return
    end
  end

  case em_lib
  when :pure_ruby
    assert_equal( :pure_ruby, lt )
  when :extension
    assert_equal( :extension, lt )
  when :java
    assert_equal( :java, lt )
  else
    # Running from jruby as a standalone test.
    if RUBY_PLATFORM == 'java'
      assert_equal( :java, lt )
    else
      assert_equal( :extension, lt )
    end
  end
end

#test_reactor_thread?Boolean

Returns:

  • (Boolean)


250
251
252
253
254
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 250

def test_reactor_thread?
  assert !EM.reactor_thread?
  EM.run { assert EM.reactor_thread?; EM.stop }
  assert !EM.reactor_thread?
end

#test_run_blockObject

EventMachine#run_block starts the reactor loop, runs the supplied block, and then STOPS the loop automatically. Contrast with EventMachine#run, which keeps running the reactor even after the supplied block completes.



118
119
120
121
122
123
124
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 118

def test_run_block
  assert !EM.reactor_running?
  a = nil
  EM.run_block { a = "Worked" }
  assert a
  assert !EM.reactor_running?
end

#test_schedule_from_threadObject



265
266
267
268
269
270
271
272
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 265

def test_schedule_from_thread
  x = false
  assert !x
  EM.run do
    Thread.new { EM.schedule { x = true; EM.stop } }.join
  end
  assert x
end

#test_schedule_on_reactor_threadObject



256
257
258
259
260
261
262
263
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 256

def test_schedule_on_reactor_thread
  x = false
  EM.run do
    EM.schedule { x = true }
    EM.stop
  end
  assert x
end

#test_serverObject



105
106
107
108
109
110
111
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 105

def test_server
  EventMachine.run {
    EventMachine.start_server "localhost", 9000, Trivial
    EventMachine.connect "localhost", 9000
  }
  assert( true ) # make sure it halts
end

#test_set_heartbeat_intervalObject



274
275
276
277
278
279
280
281
282
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 274

def test_set_heartbeat_interval
  interval = 0.5
  EM.run {
    EM.set_heartbeat_interval interval
    $interval = EM.get_heartbeat_interval
    EM.stop
  }
  assert_equal(interval, $interval)
end

#test_timerObject




86
87
88
89
90
91
92
93
94
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 86

def test_timer
  n = 0
  EventMachine.run {
    EventMachine.add_periodic_timer(0.1) {
      n += 1
      EventMachine.stop if n == 2
    }
  }
end

#xxx_test_post_init_errorObject

This test causes issues, the machine becomes unreleasable after release_machine suffers an exception in event_callback.



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 187

def xxx_test_post_init_error
  assert_raises( EventMachine::ConnectionNotBound ) {
    EM.run {
      EM::Timer.new(1) {EM.stop}
      EM.start_server TestHost, TestPort
      EM.connect TestHost, TestPort, PostInitError
    }
  }
  EM.run {
    EM.stop
  }
  assert !EM.reactor_running?
end

#xxx_test_unbind_errorObject



151
152
153
154
155
156
157
158
# File 'lib/ext/eventmachine-0.12.10/tests/test_basic.rb', line 151

def xxx_test_unbind_error
  assert_raises( RuntimeError ) {
    EM.run {
      EM.start_server TestHost, TestPort
      EM.connect TestHost, TestPort, UnbindError
    }
  }
end