File: notifications_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 (140 lines) | stat: -rw-r--r-- 3,501 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
RSpec.describe Celluloid::Notifications, actor_system: :global do
  class Admirer
    include Celluloid
    include Celluloid::Notifications

    attr_reader :mourning
    attr_reader :mourning_count

    def someone_died(_topic, name)
      @mourning = name
      @mourning_count ||= 0
      @mourning_count += 1
    end
  end

  class President
    include Celluloid
    include Celluloid::Notifications

    def die(topic = "death")
      publish(topic, "Mr. President")
    end
  end

  it "notifies relevant subscribers" do
    marilyn = Admirer.new
    jackie = Admirer.new

    marilyn.subscribe("death", :someone_died)
    jackie.subscribe("alive", :someone_died)

    president = President.new

    president.die
    expect(marilyn.mourning).to eq("Mr. President")
    expect(jackie.mourning).not_to eq("Mr. President")
  end

  it "allows multiple subscriptions from the same actor" do
    marilyn = Admirer.new

    marilyn.subscribe("death", :someone_died)
    marilyn.subscribe("death", :someone_died)

    president = President.new

    president.die
    expect(marilyn.mourning_count).to be(2)
  end

  it "notifies subscribers" do
    marilyn = Admirer.new
    jackie = Admirer.new

    marilyn.subscribe("death", :someone_died)
    jackie.subscribe("death", :someone_died)

    president = President.new

    president.die
    expect(marilyn.mourning).to eq("Mr. President")
    expect(jackie.mourning).to eq("Mr. President")
  end

  it "publishes even if there are no subscribers" do
    president = President.new
    president.die
  end

  it "allows symbol subscriptions" do
    marilyn = Admirer.new
    jackie = Admirer.new

    marilyn.subscribe(:death, :someone_died)
    jackie.subscribe("death", :someone_died)

    president = President.new
    president.die(:death)
    expect(marilyn.mourning).to eq("Mr. President")
    expect(jackie.mourning).to eq("Mr. President")
  end

  it "allows regex subscriptions" do
    marilyn = Admirer.new

    marilyn.subscribe(/(death|assassination)/, :someone_died)

    president = President.new
    president.die
    expect(marilyn.mourning).to eq("Mr. President")
  end

  it "matches symbols against regex subscriptions" do
    marilyn = Admirer.new

    marilyn.subscribe(/(death|assassination)/, :someone_died)

    president = President.new
    president.die(:assassination)
    expect(marilyn.mourning).to eq("Mr. President")
  end

  it "allows unsubscribing" do
    marilyn = Admirer.new

    subscription = marilyn.subscribe("death", :someone_died)
    marilyn.unsubscribe(subscription)

    president = President.new
    president.die
    expect(marilyn.mourning).to be_nil
  end

  it "prunes dead subscriptions" do
    marilyn = Admirer.new
    jackie = Admirer.new

    marilyn.subscribe("death", :someone_died)
    jackie.subscribe("death", :someone_died)

    listeners = Celluloid::Notifications.notifier.listeners_for("death").size
    marilyn.terminate
    after_listeners = Celluloid::Notifications.notifier.listeners_for("death").size

    expect(after_listeners).to eq(listeners - 1)
  end

  it "prunes multiple subscriptions from a dead actor" do
    marilyn = Admirer.new

    marilyn.subscribe("death", :someone_died)
    marilyn.subscribe("death", :someone_died)

    listeners = Celluloid::Notifications.notifier.listeners_for("death").size
    marilyn.terminate
    after_listeners = Celluloid::Notifications.notifier.listeners_for("death").size

    expect(after_listeners).to eq(listeners - 2)
  end
end