File: fiber_iterator_spec.rb

package info (click to toggle)
ruby-em-synchrony 1.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 572 kB
  • sloc: ruby: 3,458; sh: 37; sql: 7; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download | duplicates (2)
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
require "spec/helper/all"
require "em-synchrony/fiber_iterator"

describe EventMachine::Synchrony::FiberIterator do

  it "should wait until the iterator is done and wrap internal block within a fiber" do
    EM.synchrony do

      results = []
      i = EM::Synchrony::FiberIterator.new(1..5, 2).each do |num|
        EM::Synchrony.sleep(0.1)
        results.push num
      end

      results.should == (1..5).to_a
      results.size.should == 5
      EventMachine.stop
    end
  end

  it "works even with nested arrays and iterator" do
    EM.synchrony do
      results = []
      list = [[1, 2], [3, 4]]

      EM::Synchrony::FiberIterator.new(list, 2).each { |sublist, iter| results.push(sublist) }

      expect(results).to eq(list)

      EM.stop
    end
  end

  #
  # it "should sum values within the iterator" do
  #   EM.synchrony do
  #     data = (1..5).to_a
  #     res = EM::Synchrony::FiberIterator.new(data, 2).inject(0) do |total, num, iter|
  #       EM::Synchrony.sleep(0.1)
  #
  #       p [:sync, total, num]
  #       iter.return(total += num)
  #     end
  #
  #     res.should == data.inject(:+)
  #     EventMachine.stop
  #   end
  # end



end