File: test_threaded_resource.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 (68 lines) | stat: -rw-r--r-- 1,561 bytes parent folder | download | duplicates (3)
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
require_relative 'em_test_helper'

class TestThreadedResource < Test::Unit::TestCase
  def object
    @object ||= {}
  end

  def resource
    @resource = EM::ThreadedResource.new do
      object
    end
  end

  def teardown
    resource.shutdown
  end

  def test_dispatch_completion
    EM.run do
      EM.add_timer(3) do
        EM.stop
        if ENV['CI'].casecmp('true').zero? and RUBY_PLATFORM[/darwin/]
          notify "Intermittent Travis MacOS: Resource dispatch timed out"
          return
        else
          assert false, 'Resource dispatch timed out'
        end
      end
      completion = resource.dispatch do |o|
        o[:foo] = :bar
        :foo
      end
      completion.callback do |result|
        assert_equal :foo, result
        EM.stop
      end
      completion.errback do |error|
        EM.stop
        assert false, "Unexpected error: #{error.message}"
      end
    end
    assert_equal :bar, object[:foo]
  end

  def test_dispatch_failure
    completion = resource.dispatch do |o|
      raise 'boom'
    end
    completion.errback do |error|
      assert_kind_of RuntimeError, error
      assert_equal 'boom', error.message
    end
  end

  def test_dispatch_threading
    main = Thread.current
    resource.dispatch do |o|
      o[:dispatch_thread] = Thread.current
    end
    assert_not_equal main, object[:dispatch_thread]
  end

  def test_shutdown
    # This test should get improved sometime. The method returning thread is
    # NOT an api that will be maintained.
    assert !resource.shutdown.alive?
  end
end