pubsubstub 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47ff717bde3d2e41574d94a5f95e7a01a086c425
4
- data.tar.gz: d14af107a0b1be58b2afa83f2a221def5d2a436c
3
+ metadata.gz: a0ea373d5be1e2b83c65048f8d24f34c942ebabc
4
+ data.tar.gz: b0c58d16d5c69ccc221527e134a1ab74172c8ec3
5
5
  SHA512:
6
- metadata.gz: 38de78410010ea79af53323418bc7804b47b3b07a3900de2199f7692209b3e16b5e4e317ad48b275dce723c68ebf780bc95e605fae2eda6e7b9a36e5ae5d8ab6
7
- data.tar.gz: 0911311b184fce34bf5d3845ebccb9a59e05a8a4ae878fd741b4f656db0bf665c4bd5130446b01257a56fecc1520cab9eb2ecd2ce355165cec899db43d2f5645
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
- redis = if EventMachine.reactor_running?
24
- self.class.nonblocking_redis
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
- redis.zrangebyscore(key('scrollback'), "(#{since_event_id.to_i}", '+inf') do |events|
30
- events.each do |json|
31
- yield Pubsubstub::Event.from_json(json)
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
- @heartbeat = Thread.new do
66
- loop do
67
- sleep Pubsubstub.heartbeat_frequency
68
- event = heartbeat_frequency.to_message
69
- @connections.each { |connection| connection << event }
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
 
@@ -1,3 +1,3 @@
1
1
  module Pubsubstub
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -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
- it "yields the events in the scrollback" do
82
- redis = double('redis')
83
- expect(redis).to receive(:zrangebyscore).with('test.scrollback', '(1234', '+inf').and_yield([event1.to_json, event2.to_json])
84
- expect(Pubsubstub::RedisPubSub).to receive(:blocking_redis).and_return(redis)
85
- expect { |block| subject.scrollback(1234, &block) }.to yield_successive_args(event1, event2)
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
@@ -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
- expect_any_instance_of(Pubsubstub::Channel).to receive(:scrollback).and_return([event])
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 "subscribes the connection to the channel" do
54
- event = Pubsubstub::Event.new('ping')
55
- channel = Pubsubstub::Channel.new(:default)
56
- allow_any_instance_of(Pubsubstub::StreamAction).to receive(:with_each_channel).and_yield(channel)
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.11
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-29 00:00:00.000000000 Z
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra