Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 62 lines (53 sloc) 1.2 kb
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
require 'em_test_helper'

class TestEMChannel < Test::Unit::TestCase
  def test_channel_subscribe
    s = 0
    EM.run do
      c = EM::Channel.new
      c.subscribe { |v| s = v; EM.stop }
      c << 1
    end
    assert_equal 1, s
  end

  def test_channel_unsubscribe
    s = 0
    EM.run do
      c = EM::Channel.new
      subscription = c.subscribe { |v| s = v }
      c.unsubscribe(subscription)
      c << 1
      EM.next_tick { EM.stop }
    end
    assert_not_equal 1, s
  end

  def test_channel_pop
    s = 0
    EM.run do
      c = EM::Channel.new
      c.pop{ |v| s = v }
      c.push(1,2,3)
      c << 4
      c << 5
      EM.next_tick { EM.stop }
    end
    assert_equal 1, s
  end

  def test_channel_reactor_thread_push
    out = []
    c = EM::Channel.new
    c.subscribe { |v| out << v }
    Thread.new { c.push(1,2,3) }.join
    assert out.empty?

    EM.run { EM.next_tick { EM.stop } }

    assert_equal [1,2,3], out
  end

  def test_channel_reactor_thread_callback
    out = []
    c = EM::Channel.new
    Thread.new { c.subscribe { |v| out << v } }.join
    c.push(1,2,3)
    assert out.empty?

    EM.run { EM.next_tick { EM.stop } }

    assert_equal [1,2,3], out
  end
end
Something went wrong with that request. Please try again.