File: block_spec.rb

package info (click to toggle)
ruby-celluloid 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: ruby: 7,579; makefile: 10
file content (110 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
RSpec.describe "Blocks", actor_system: :global do
  class MyBlockActor
    include Celluloid

    def initialize(name, data)
      @name = name
      @data = data
    end
    attr_reader :name

    def ask_for_something(other)
      sender_actor = current_actor
      @data << [:outside, @name, current_actor.name]
      other.do_something_and_callback do |_value|
        @data << [:yielded, @name, current_actor.name]
        @data << receive_result(:self)
        @data << current_actor.receive_result(:current_actor)
        @data << sender_actor.receive_result(:sender)
        :pete_the_polyglot_alien
      end
    end

    def deferred_excecution(value, &_block)
      defer do
        yield(value)
      end
    end

    def deferred_current_actor(&_block)
      defer do
        yield(current_actor.name)
      end
    end

    def defer_for_something(other, &_block)
      sender_actor = current_actor
      defer do
        @data << [:outside, @name, current_actor.name]
        other.do_something_and_callback do |_value|
          @data << [:yielded, @name, current_actor.name]
          @data << receive_result(:self)
          @data << current_actor.receive_result(:current_actor)
          @data << sender_actor.receive_result(:sender)
          :pete_the_polyglot_alien
        end
      end
    end

    def do_something_and_callback
      @data << [:something, @name, current_actor.name]
      @data << yield(:foo)
    end

    def receive_result(result)
      [result, @name, current_actor.name]
    end
  end

  it "work between actors" do
    data = []

    a1 = MyBlockActor.new("one", data)
    a2 = MyBlockActor.new("two", data)

    a1.ask_for_something a2

    expected = [
      [:outside, "one", "one"],
      [:something, "two", "two"],
      [:yielded, "one", "one"],
      [:self, "one", "one"],
      [:current_actor, "one", "one"],
      [:sender, "one", "one"],
      :pete_the_polyglot_alien
    ]

    expect(data).to eq(expected)
  end

  execute_deferred = proc do
    a1 = MyBlockActor.new("one", [])
    expect(a1.deferred_excecution(:pete_the_polyglot_alien) { |v| v })
      .to eq(:pete_the_polyglot_alien)
  end

  xit("can be deferred", &execute_deferred)

  xit "can execute deferred blocks referencing current_actor" do
    a1 = MyBlockActor.new("one", [])
    expect(a1.deferred_current_actor { |v| v }).to be("one")
  end

  xit "can execute deferred blocks with another actor" do
    data = []
    a1 = MyBlockActor.new("one", data)
    a2 = MyBlockActor.new("two", data)
    a1.defer_for_something a2
    expected = [
      [:outside, "one", "one"],
      [:something, "two", "two"],
      [:yielded, "one", "one"],
      [:self, "one", "one"],
      [:current_actor, "one", "one"],
      [:sender, "one", "one"],
      :pete_the_polyglot_alien
    ]

    expect(data).to eq(expected)
  end
end