pubsubstub 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pubsubstub/redis_pub_sub.rb +12 -9
- data/lib/pubsubstub/stream_action.rb +5 -6
- data/lib/pubsubstub/version.rb +1 -1
- data/spec/redis_pub_sub_spec.rb +23 -5
- data/spec/stream_action_spec.rb +50 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0ea373d5be1e2b83c65048f8d24f34c942ebabc
|
4
|
+
data.tar.gz: b0c58d16d5c69ccc221527e134a1ab74172c8ec3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e27fd24735fbcd7ef96be80f9dcbbb925bf322c17a3902c76f5a9ad04a37039dc6dacd0c1661eec07a5cc400b9757ed938b93f45d6f61e80f8fb8f1885dd006
|
7
|
+
data.tar.gz: 16ad3bfc0d0fc4c73ca135715f3ce73f4bd374897d830d1f9a564a6edaa59787cd280c14f3acb77bea61813a32ebd2fdfebfb02f6b6d3cbd2a133020e2d4c667
|
@@ -20,21 +20,24 @@ module Pubsubstub
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def scrollback(since_event_id)
|
23
|
-
|
24
|
-
|
25
|
-
else
|
26
|
-
self.class.blocking_redis
|
23
|
+
redis_scrollback(since_event_id) do |json|
|
24
|
+
yield Pubsubstub::Event.from_json(json)
|
27
25
|
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def redis_scrollback(since_event_id, &block)
|
31
|
+
args = [key('scrollback'), "(#{since_event_id.to_i}", '+inf']
|
32
|
+
if EventMachine.reactor_running?
|
33
|
+
self.class.nonblocking_redis.zrangebyscore(*args) do |events|
|
34
|
+
events.each(&block)
|
32
35
|
end
|
36
|
+
else
|
37
|
+
self.class.blocking_redis.zrangebyscore(*args).each(&block)
|
33
38
|
end
|
34
39
|
end
|
35
40
|
|
36
|
-
private
|
37
|
-
|
38
41
|
def key(purpose)
|
39
42
|
[@channel_name, purpose].join(".")
|
40
43
|
end
|
@@ -62,12 +62,11 @@ module Pubsubstub
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def start_heartbeat
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
65
|
+
return unless EventMachine.reactor_running?
|
66
|
+
EventMachine::PeriodicTimer.new(Pubsubstub.heartbeat_frequency) do
|
67
|
+
sleep Pubsubstub.heartbeat_frequency
|
68
|
+
event = heartbeat_event.to_message
|
69
|
+
@connections.each { |connection| connection << event }
|
71
70
|
end
|
72
71
|
end
|
73
72
|
|
data/lib/pubsubstub/version.rb
CHANGED
data/spec/redis_pub_sub_spec.rb
CHANGED
@@ -78,11 +78,29 @@ describe Pubsubstub::RedisPubSub do
|
|
78
78
|
let(:event1) { Pubsubstub::Event.new("toto", id: 1235) }
|
79
79
|
let(:event2) { Pubsubstub::Event.new("toto", id: 1236) }
|
80
80
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
81
|
+
describe "without EventMachine" do
|
82
|
+
it "yields the events in the scrollback" do
|
83
|
+
redis = double('redis')
|
84
|
+
expect(redis).to receive(:zrangebyscore)
|
85
|
+
.with('test.scrollback', '(1234', '+inf')
|
86
|
+
.and_return([event1.to_json, event2.to_json])
|
87
|
+
|
88
|
+
expect(Pubsubstub::RedisPubSub).to receive(:blocking_redis).and_return(redis)
|
89
|
+
expect { |block| subject.scrollback(1234, &block) }.to yield_successive_args(event1, event2)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "with EventMachine" do
|
94
|
+
it "yields the events in the scrollback" do
|
95
|
+
allow(EventMachine).to receive(:reactor_running?).and_return(true)
|
96
|
+
redis = double('redis')
|
97
|
+
expect(redis).to receive(:zrangebyscore)
|
98
|
+
.with('test.scrollback', '(1234', '+inf')
|
99
|
+
.and_yield([event1.to_json, event2.to_json])
|
100
|
+
|
101
|
+
expect(Pubsubstub::RedisPubSub).to receive(:nonblocking_redis).and_return(redis)
|
102
|
+
expect { |block| subject.scrollback(1234, &block) }.to yield_successive_args(event1, event2)
|
103
|
+
end
|
86
104
|
end
|
87
105
|
end
|
88
106
|
end
|
data/spec/stream_action_spec.rb
CHANGED
@@ -25,9 +25,10 @@ describe "Pubsubstub::StreamAction without EventMachine" do
|
|
25
25
|
|
26
26
|
it "returns the content of the scrollback" do
|
27
27
|
event = Pubsubstub::Event.new("test")
|
28
|
-
|
28
|
+
Pubsubstub::RedisPubSub.publish(:default, event)
|
29
29
|
|
30
30
|
get "/", {}, 'HTTP_LAST_EVENT_ID' => 1
|
31
|
+
expect(last_response.body).to eq(event.to_message)
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
@@ -50,10 +51,10 @@ describe "Pubsubstub::StreamAction with EventMachine" do
|
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
53
|
-
it "
|
54
|
-
event = Pubsubstub::Event.new(
|
55
|
-
|
56
|
-
|
54
|
+
it "returns the content of the scrollback right away" do
|
55
|
+
event = Pubsubstub::Event.new("test")
|
56
|
+
Pubsubstub::RedisPubSub.publish(:default, event)
|
57
|
+
expect_any_instance_of(EventMachine::Hiredis::Client).to receive(:zrangebyscore).and_yield([event.to_json])
|
57
58
|
|
58
59
|
em do
|
59
60
|
env = current_session.send(:env_for, "/", 'HTTP_LAST_EVENT_ID' => 1)
|
@@ -61,7 +62,6 @@ describe "Pubsubstub::StreamAction with EventMachine" do
|
|
61
62
|
status, headers, body = app.call(request.env)
|
62
63
|
|
63
64
|
response = Rack::MockResponse.new(status, headers, body, env["rack.errors"].flush)
|
64
|
-
channel.send(:broadcast, event.to_json)
|
65
65
|
|
66
66
|
EM.next_tick {
|
67
67
|
body.close
|
@@ -72,4 +72,48 @@ describe "Pubsubstub::StreamAction with EventMachine" do
|
|
72
72
|
}
|
73
73
|
end
|
74
74
|
end
|
75
|
+
|
76
|
+
it "subscribes the connection to the channel" do
|
77
|
+
em do
|
78
|
+
redis = spy('Redis Pubsub')
|
79
|
+
allow(Pubsubstub::RedisPubSub).to receive(:sub).and_return(redis)
|
80
|
+
expect(redis).to receive(:subscribe).with("default.pubsub", anything)
|
81
|
+
|
82
|
+
env = current_session.send(:env_for, "/", 'HTTP_LAST_EVENT_ID' => 1)
|
83
|
+
request = Rack::Request.new(env)
|
84
|
+
status, headers, body = app.call(request.env)
|
85
|
+
|
86
|
+
response = Rack::MockResponse.new(status, headers, body, env["rack.errors"].flush)
|
87
|
+
|
88
|
+
EM.next_tick {
|
89
|
+
body.close
|
90
|
+
response.finish
|
91
|
+
EM.stop
|
92
|
+
}
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
it "sends heartbeat events every now and then" do
|
97
|
+
allow(Pubsubstub).to receive(:heartbeat_frequency).and_return(0.001)
|
98
|
+
|
99
|
+
Timecop.freeze do
|
100
|
+
em do
|
101
|
+
env = current_session.send(:env_for, "/", 'HTTP_LAST_EVENT_ID' => 1)
|
102
|
+
request = Rack::Request.new(env)
|
103
|
+
status, headers, body = app.call(request.env)
|
104
|
+
|
105
|
+
response = Rack::MockResponse.new(status, headers, body, env["rack.errors"].flush)
|
106
|
+
|
107
|
+
event = Pubsubstub::Event.new('ping', name: 'heartbeat', retry_after: Pubsubstub::StreamAction::RECONNECT_TIMEOUT)
|
108
|
+
|
109
|
+
EM.add_timer(0.001) {
|
110
|
+
body.close
|
111
|
+
response.finish
|
112
|
+
|
113
|
+
expect(response.body).to eq(event.to_message)
|
114
|
+
EM.stop
|
115
|
+
}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
75
119
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pubsubstub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guillaume Malette
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05
|
11
|
+
date: 2015-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|