aboutsummaryrefslogtreecommitdiff
path: root/vendor/supply_drop/test/async_test.rb
blob: 0b9bc3aab6a64ff280964f83aaedde977c8466a6 (plain)
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
require 'test/unit'
require File.expand_path('../../lib/supply_drop/util', __FILE__)
require File.expand_path('../../lib/supply_drop/async_enumerable', __FILE__)
require File.expand_path('../../lib/supply_drop/thread_pool', __FILE__)

class AsyncTest < Test::Unit::TestCase
  def teardown
    SupplyDrop::Util.thread_pool_size = SupplyDrop::Util::DEFAULT_THREAD_POOL_SIZE
  end

  def test_can_enumerate_asynchronously
    collection = (1..10).to_a
    elapsed = timed do
      SupplyDrop::Util.optionally_async(collection, true).each do |item|
        sleep 0.1
      end
    end
    assert elapsed < 1
  end

  def test_can_enumerate_asynchronously_with_map
    collection = (1..10).to_a
    returned_values = nil
    elapsed = timed do
      returned_values = SupplyDrop::Util.optionally_async(collection, true).map do |item|
        sleep 0.1
        :somevalue
      end
    end
    assert returned_values == Array.new(10, :somevalue)
    assert elapsed < 1
  end

  def test_can_enumerate_normally_and_does_not_leak_after_extending
    collection = (1..10).to_a
    elapsed = timed do
      SupplyDrop::Util.optionally_async(collection, true).each do |item|
        sleep 0.1
      end
    end
    assert elapsed < 1

    elapsed = timed do
      SupplyDrop::Util.optionally_async(collection, false).each do |item|
        sleep 0.1
      end
    end
    assert elapsed >= 1
  end

  def test_can_configure_thread_pool_size
    collection = (1..10).to_a
    SupplyDrop::Util.thread_pool_size = 1
    elapsed = timed do
      returned_values = SupplyDrop::Util.optionally_async(collection, true).map do |item|
        sleep 0.1
      end
    end
    assert elapsed >= 1
  end

  def timed
    start = Time.now
    yield
    Time.now - start
  end
end