sse-rails-engine 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -0
- data/app/controllers/sse_rails_engine/sse_controller.rb +1 -3
- data/lib/sse-rails-engine.rb +0 -10
- data/lib/sse_rails_engine/manager.rb +14 -24
- data/lib/sse_rails_engine/version.rb +1 -1
- data/test/controllers/sse_rails_engine/sse_controller_test.rb +3 -2
- data/test/dummy/log/test.log +154 -0
- data/test/lib/sse_rails_engine/manager_test.rb +12 -9
- data/test/lib/sse_rails_engine_test.rb +1 -19
- data/test/test_helper.rb +3 -6
- metadata +17 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f447d929f60a969aedc1b5e1dcc666654daad75
|
4
|
+
data.tar.gz: 0b13bcf75030e96034da7be4b6a692ee98bf5b40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 071ca673108b4d6c1e29e4818f4dff978f65dd430dd25e282862351529c9ba985c223a922cabd57f301e012ef6f2550a36adedf72d3cda9fdd5d19ceda397f1b
|
7
|
+
data.tar.gz: 5435274799d1c6ba93579a40a328aa42821d5ec012afbe5021a1590c6806dcf3904ff3f6e3bf1b13888a47f333cce9405884196ab5923ebc612bba682fac4bd2
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/henders/sse-rails-engine.svg?branch=master)](https://travis-ci.org/henders/sse-rails-engine)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/henders/sse-rails-engine/badges/gpa.svg)](https://codeclimate.com/github/henders/sse-rails-engine)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/henders/sse-rails-engine/badges/coverage.svg)](https://codeclimate.com/github/henders/sse-rails-engine)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/sse-rails-engine.svg)](http://badge.fury.io/rb/sse-rails-engine)
|
5
|
+
|
6
|
+
This engine is designed to allow Server-Sent Events to be broadcast to all listeners from anywhere
|
7
|
+
in your Rails app.
|
8
|
+
At the moment it only works with Thread based Rack servers like Puma, Thin, Reel, ...
|
9
|
+
|
10
|
+
Plans are to hook it up to Redis if you need to use process-based servers like Unicorn.
|
11
|
+
|
12
|
+
This has been mostly tested with Ruby v2.2.1 and Rails v4.2.1 + Puma.
|
13
|
+
|
14
|
+
This uses the Rack socket hijacking functionality to avoid having to occupy a thread per connection. Now
|
15
|
+
only 1 extra thread in your app is required.
|
16
|
+
|
17
|
+
# Installation
|
18
|
+
```
|
19
|
+
gem 'sse-rails-engine'
|
20
|
+
```
|
21
|
+
|
22
|
+
# Usage
|
23
|
+
|
24
|
+
Mount the engine in your ```config/routes.rb```:
|
25
|
+
```
|
26
|
+
Rails.application.routes.draw do
|
27
|
+
mount SseRailsEngine::Engine, at: '/sse'
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
To use, initialize the connection on the client side, so add the following javascript:
|
32
|
+
```
|
33
|
+
$(document).ready(function () {
|
34
|
+
var source = new EventSource('/sse');
|
35
|
+
|
36
|
+
source.addEventListener('test', function(e) {
|
37
|
+
// Do something
|
38
|
+
}, true);
|
39
|
+
});
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
Then you can send an event from anywhere in your app:
|
44
|
+
```
|
45
|
+
SseRailsEngine.send_event('test', { foo: 'bar' })
|
46
|
+
```
|
47
|
+
|
48
|
+
# License
|
49
|
+
|
50
|
+
MIT - Have at it! :)
|
@@ -2,11 +2,9 @@ require_dependency "sse_rails_engine/application_controller"
|
|
2
2
|
|
3
3
|
module SseRailsEngine
|
4
4
|
class SseController < ApplicationController
|
5
|
-
include ActionController::Live
|
6
|
-
|
7
5
|
def connect
|
6
|
+
raise 'This Rack server does not support hijacking, ensure you are using >v1.5 of Rack' unless request.env['rack.hijack?']
|
8
7
|
SseRailsEngine.manager.register(response)
|
9
|
-
SseRailsEngine.wait_for_disconnect(response)
|
10
8
|
render json: {}
|
11
9
|
end
|
12
10
|
end
|
data/lib/sse-rails-engine.rb
CHANGED
@@ -2,24 +2,14 @@ require 'sse_rails_engine/engine'
|
|
2
2
|
require 'sse_rails_engine/manager'
|
3
3
|
|
4
4
|
module SseRailsEngine
|
5
|
-
mattr_accessor :disconnect_wait_time
|
6
5
|
mattr_accessor :heartbeat_interval
|
7
6
|
|
8
|
-
@@disconnect_wait_time = 3.seconds
|
9
7
|
@@heartbeat_interval = 5.seconds
|
10
8
|
|
11
9
|
def self.manager
|
12
10
|
@manager ||= Manager.new
|
13
11
|
end
|
14
12
|
|
15
|
-
def self.wait_for_disconnect(response)
|
16
|
-
loop do
|
17
|
-
sleep disconnect_wait_time
|
18
|
-
break unless manager.registered?(response)
|
19
|
-
end
|
20
|
-
true
|
21
|
-
end
|
22
|
-
|
23
13
|
def self.send_event(name, data)
|
24
14
|
manager.send_event(name, data)
|
25
15
|
end
|
@@ -9,24 +9,10 @@ module SseRailsEngine
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def register(response)
|
12
|
-
ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord)
|
13
12
|
response.headers['Content-Type'] = 'text/event-stream'
|
14
13
|
response.headers['Cache-Control'] = 'no-cache'
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def registered?(response)
|
21
|
-
@mutex.synchronize do
|
22
|
-
@connections[response.stream] ? true : false
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def unregister(response)
|
27
|
-
@mutex.synchronize do
|
28
|
-
close_connection(response.stream)
|
29
|
-
end
|
14
|
+
# Perform partial hijack of socket (http://old.blog.phusion.nl/2013/01/23/the-new-rack-socket-hijacking-api/)
|
15
|
+
response.headers['rack.hijack'] = ->(io) { SseRailsEngine.manager.open_connection(io) }
|
30
16
|
end
|
31
17
|
|
32
18
|
def send_event(name, data)
|
@@ -34,33 +20,37 @@ module SseRailsEngine
|
|
34
20
|
@connections.dup.each do |stream, sse|
|
35
21
|
begin
|
36
22
|
sse.write(data, event: name)
|
37
|
-
rescue
|
23
|
+
rescue IOError, Errno::EPIPE, Errno::ETIMEDOUT
|
24
|
+
Rails.logger.debug "SSE Client disconnected: #{stream}"
|
38
25
|
close_connection(stream)
|
39
26
|
rescue => ex
|
40
|
-
Rails.logger.error "Failed to send event to SSE: #{stream} (#{name}, #{data} - #{ex.message}"
|
27
|
+
Rails.logger.error "Failed to send event to SSE: #{stream} (#{name}, #{data} - #{ex.message} (#{ex.class}"
|
41
28
|
end
|
42
29
|
end
|
43
30
|
end
|
44
31
|
end
|
45
32
|
|
33
|
+
def open_connection(io)
|
34
|
+
Rails.logger.debug "New SSE Client connected: #{io}"
|
35
|
+
@mutex.synchronize do
|
36
|
+
@connections[io] = ActionController::Live::SSE.new(io)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
46
40
|
private
|
47
41
|
|
48
42
|
def close_connection(stream)
|
49
43
|
return if @connections[stream].nil?
|
50
44
|
@connections[stream].close
|
51
45
|
@connections.delete(stream)
|
52
|
-
stream.close unless stream.closed?
|
53
|
-
end
|
54
|
-
|
55
|
-
def heartbeat
|
56
|
-
send_event('heartbeat', '')
|
57
46
|
end
|
58
47
|
|
59
48
|
def start_heartbeats
|
49
|
+
Rails.logger.debug 'Starting SSE heartbeat thread!!!!!'
|
60
50
|
Thread.new do
|
61
51
|
loop do
|
62
52
|
sleep SseRailsEngine.heartbeat_interval
|
63
|
-
heartbeat
|
53
|
+
send_event('heartbeat', '')
|
64
54
|
end
|
65
55
|
end
|
66
56
|
end
|
@@ -4,18 +4,19 @@ describe SseRailsEngine::SseController do
|
|
4
4
|
before do
|
5
5
|
SseRailsEngine.instance_variable_set(:@manager, nil)
|
6
6
|
@routes = SseRailsEngine::Engine.routes
|
7
|
+
request.env['rack.hijack?'] = 1
|
7
8
|
end
|
8
9
|
|
9
10
|
it 'registers new connections' do
|
10
|
-
SseRailsEngine.expects(:wait_for_disconnect).returns(true)
|
11
11
|
get :connect
|
12
12
|
assert_response :success
|
13
|
+
response.headers['rack.hijack'].call(response.stream)
|
13
14
|
SseRailsEngine.manager.connections.size.must_equal 1
|
14
15
|
end
|
15
16
|
|
16
17
|
it 'sends event to connection' do
|
17
|
-
SseRailsEngine.expects(:wait_for_disconnect).returns(true)
|
18
18
|
get :connect
|
19
|
+
response.headers['rack.hijack'].call(response.stream)
|
19
20
|
SseRailsEngine.manager.connections.size.must_equal 1
|
20
21
|
ActionController::Live::SSE.any_instance.expects(:write).with('bar', event: 'foo').once
|
21
22
|
SseRailsEngine.send_event('foo', 'bar')
|
data/test/dummy/log/test.log
CHANGED
@@ -355,3 +355,157 @@ SseRailsEngine::SseController: test_0002_sends event to connection
|
|
355
355
|
------------------------------------------------------------------
|
356
356
|
Processing by SseRailsEngine::SseController#connect as HTML
|
357
357
|
Completed 200 OK in 0ms (Views: 0.1ms)
|
358
|
+
Starting SSE heartbeat thread!!!!!
|
359
|
+
------------------------------------------------------------------
|
360
|
+
SseRailsEngine::SseController: test_0002_sends event to connection
|
361
|
+
------------------------------------------------------------------
|
362
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
363
|
+
Completed 500 Internal Server Error in 0ms
|
364
|
+
------------------------------------------------------------------
|
365
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
366
|
+
------------------------------------------------------------------
|
367
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
368
|
+
Completed 500 Internal Server Error in 0ms
|
369
|
+
Starting SSE heartbeat thread!!!!!
|
370
|
+
Starting SSE heartbeat thread!!!!!
|
371
|
+
New SSE Client connected: #<StringIO:0x007fc3b3512498>
|
372
|
+
Starting SSE heartbeat thread!!!!!
|
373
|
+
Starting SSE heartbeat thread!!!!!
|
374
|
+
New SSE Client connected: #<StringIO:0x007f88b3ad3a20>
|
375
|
+
SSE Client disconnected: #<StringIO:0x007f88b3ad3a20>
|
376
|
+
Starting SSE heartbeat thread!!!!!
|
377
|
+
New SSE Client connected: #<StringIO:0x007fb040de4ed0>
|
378
|
+
Starting SSE heartbeat thread!!!!!
|
379
|
+
New SSE Client connected: #<StringIO:0x007f8efcf18b68>
|
380
|
+
Starting SSE heartbeat thread!!!!!
|
381
|
+
New SSE Client connected: #<StringIO:0x007fc1ca020358>
|
382
|
+
New SSE Client connected: #<StringIO:0x007fc1c9dea750>
|
383
|
+
New SSE Client connected: #<StringIO:0x007fc1c9dba9d8>
|
384
|
+
Starting SSE heartbeat thread!!!!!
|
385
|
+
Starting SSE heartbeat thread!!!!!
|
386
|
+
New SSE Client connected: #<StringIO:0x007f8e3b1e31d0>
|
387
|
+
Starting SSE heartbeat thread!!!!!
|
388
|
+
New SSE Client connected: #<StringIO:0x007f8e3b1b3c28>
|
389
|
+
Starting SSE heartbeat thread!!!!!
|
390
|
+
Starting SSE heartbeat thread!!!!!
|
391
|
+
Starting SSE heartbeat thread!!!!!
|
392
|
+
New SSE Client connected: #<StringIO:0x007f8e3b143248>
|
393
|
+
Starting SSE heartbeat thread!!!!!
|
394
|
+
New SSE Client connected: #<StringIO:0x007f8e3b130c60>
|
395
|
+
Starting SSE heartbeat thread!!!!!
|
396
|
+
Starting SSE heartbeat thread!!!!!
|
397
|
+
Starting SSE heartbeat thread!!!!!
|
398
|
+
New SSE Client connected: #<StringIO:0x007fd622d27378>
|
399
|
+
Starting SSE heartbeat thread!!!!!
|
400
|
+
New SSE Client connected: #<StringIO:0x007fd622d04648>
|
401
|
+
Starting SSE heartbeat thread!!!!!
|
402
|
+
New SSE Client connected: #<StringIO:0x007fd622cd4da8>
|
403
|
+
Starting SSE heartbeat thread!!!!!
|
404
|
+
New SSE Client connected: #<StringIO:0x007fd622c94960>
|
405
|
+
Starting SSE heartbeat thread!!!!!
|
406
|
+
Starting SSE heartbeat thread!!!!!
|
407
|
+
New SSE Client connected: #<StringIO:0x007fd1c25a9560>
|
408
|
+
Starting SSE heartbeat thread!!!!!
|
409
|
+
Starting SSE heartbeat thread!!!!!
|
410
|
+
New SSE Client connected: #<StringIO:0x007f840cd439f0>
|
411
|
+
Starting SSE heartbeat thread!!!!!
|
412
|
+
Starting SSE heartbeat thread!!!!!
|
413
|
+
New SSE Client connected: #<StringIO:0x007f7feb5be3a8>
|
414
|
+
Starting SSE heartbeat thread!!!!!
|
415
|
+
New SSE Client connected: #<StringIO:0x007f7feb576288>
|
416
|
+
SSE Client disconnected: #<StringIO:0x007f7feb576288>
|
417
|
+
Starting SSE heartbeat thread!!!!!
|
418
|
+
Starting SSE heartbeat thread!!!!!
|
419
|
+
New SSE Client connected: #<StringIO:0x007f7feb5279d0>
|
420
|
+
Starting SSE heartbeat thread!!!!!
|
421
|
+
New SSE Client connected: #<StringIO:0x007f7feb506b90>
|
422
|
+
------------------------------------------------------------------
|
423
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
424
|
+
------------------------------------------------------------------
|
425
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
426
|
+
Completed 500 Internal Server Error in 0ms
|
427
|
+
------------------------------------------------------------------
|
428
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
429
|
+
------------------------------------------------------------------
|
430
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
431
|
+
Starting SSE heartbeat thread!!!!!
|
432
|
+
Completed 200 OK in 0ms (Views: 0.2ms)
|
433
|
+
------------------------------------------------------------------
|
434
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
435
|
+
------------------------------------------------------------------
|
436
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
437
|
+
Starting SSE heartbeat thread!!!!!
|
438
|
+
Completed 200 OK in 0ms (Views: 0.1ms)
|
439
|
+
------------------------------------------------------------------
|
440
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
441
|
+
------------------------------------------------------------------
|
442
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
443
|
+
Starting SSE heartbeat thread!!!!!
|
444
|
+
Completed 200 OK in 0ms (Views: 0.2ms)
|
445
|
+
New SSE Client connected: #<ActionDispatch::Response::Buffer:0x007ff07459b8b8>
|
446
|
+
------------------------------------------------------------------
|
447
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
448
|
+
------------------------------------------------------------------
|
449
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
450
|
+
Starting SSE heartbeat thread!!!!!
|
451
|
+
Completed 200 OK in 0ms (Views: 0.2ms)
|
452
|
+
New SSE Client connected: #<ActionDispatch::Response::Buffer:0x007fd6c12fbf30>
|
453
|
+
------------------------------------------------------------------
|
454
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
455
|
+
------------------------------------------------------------------
|
456
|
+
------------------------------------------------------------------
|
457
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
458
|
+
------------------------------------------------------------------
|
459
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
460
|
+
Starting SSE heartbeat thread!!!!!
|
461
|
+
Completed 200 OK in 0ms (Views: 0.2ms)
|
462
|
+
New SSE Client connected: #<ActionDispatch::Response::Buffer:0x007fb813d73020>
|
463
|
+
Starting SSE heartbeat thread!!!!!
|
464
|
+
New SSE Client connected: #<StringIO:0x007f93e9fd9e20>
|
465
|
+
Starting SSE heartbeat thread!!!!!
|
466
|
+
New SSE Client connected: #<StringIO:0x007f93e9fd32c8>
|
467
|
+
Starting SSE heartbeat thread!!!!!
|
468
|
+
Starting SSE heartbeat thread!!!!!
|
469
|
+
New SSE Client connected: #<StringIO:0x007f93e9fd11d0>
|
470
|
+
Starting SSE heartbeat thread!!!!!
|
471
|
+
New SSE Client connected: #<StringIO:0x007f93e9fc9818>
|
472
|
+
SSE Client disconnected: #<StringIO:0x007f93e9fc9818>
|
473
|
+
Starting SSE heartbeat thread!!!!!
|
474
|
+
------------------------------------------------------------------
|
475
|
+
SseRailsEngine::SseController: test_0002_sends event to connection
|
476
|
+
------------------------------------------------------------------
|
477
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
478
|
+
Starting SSE heartbeat thread!!!!!
|
479
|
+
Completed 200 OK in 0ms (Views: 0.1ms)
|
480
|
+
New SSE Client connected: #<ActionDispatch::Response::Buffer:0x007f93e9f99d48>
|
481
|
+
------------------------------------------------------------------
|
482
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
483
|
+
------------------------------------------------------------------
|
484
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
485
|
+
Starting SSE heartbeat thread!!!!!
|
486
|
+
Completed 200 OK in 0ms (Views: 0.1ms)
|
487
|
+
New SSE Client connected: #<ActionDispatch::Response::Buffer:0x007f93e9f88700>
|
488
|
+
Starting SSE heartbeat thread!!!!!
|
489
|
+
New SSE Client connected: #<StringIO:0x007fc84d982f48>
|
490
|
+
Starting SSE heartbeat thread!!!!!
|
491
|
+
Starting SSE heartbeat thread!!!!!
|
492
|
+
New SSE Client connected: #<StringIO:0x007fc84ca5a1d8>
|
493
|
+
Starting SSE heartbeat thread!!!!!
|
494
|
+
New SSE Client connected: #<StringIO:0x007fc84d97ba40>
|
495
|
+
Starting SSE heartbeat thread!!!!!
|
496
|
+
New SSE Client connected: #<StringIO:0x007fc84d97a4d8>
|
497
|
+
SSE Client disconnected: #<StringIO:0x007fc84d97a4d8>
|
498
|
+
------------------------------------------------------------------
|
499
|
+
SseRailsEngine::SseController: test_0001_registers new connections
|
500
|
+
------------------------------------------------------------------
|
501
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
502
|
+
Starting SSE heartbeat thread!!!!!
|
503
|
+
Completed 200 OK in 0ms (Views: 0.1ms)
|
504
|
+
New SSE Client connected: #<ActionDispatch::Response::Buffer:0x007fc84d909698>
|
505
|
+
------------------------------------------------------------------
|
506
|
+
SseRailsEngine::SseController: test_0002_sends event to connection
|
507
|
+
------------------------------------------------------------------
|
508
|
+
Processing by SseRailsEngine::SseController#connect as HTML
|
509
|
+
Starting SSE heartbeat thread!!!!!
|
510
|
+
Completed 200 OK in 0ms (Views: 0.1ms)
|
511
|
+
New SSE Client connected: #<ActionDispatch::Response::Buffer:0x007fc84ca50b38>
|
@@ -2,38 +2,41 @@ require_relative '../../test_helper'
|
|
2
2
|
|
3
3
|
describe SseRailsEngine::Manager do
|
4
4
|
let(:response) { Hashie::Mash.new(stream: StringIO.new, headers: {}) }
|
5
|
-
let(:manager) { SseRailsEngine
|
5
|
+
let(:manager) { SseRailsEngine::manager }
|
6
6
|
|
7
|
-
|
8
|
-
manager.must_equal SseRailsEngine.manager
|
9
|
-
end
|
7
|
+
before { SseRailsEngine.instance_variable_set(:@manager, nil) }
|
10
8
|
|
11
9
|
it 'registers new response streams' do
|
10
|
+
manager.connections.size.must_equal 0
|
12
11
|
manager.register(response)
|
13
|
-
|
12
|
+
response.headers['rack.hijack'].call(response.stream)
|
13
|
+
manager.connections.size.must_equal 1
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'does not register new response stream' do
|
17
|
-
manager.
|
17
|
+
manager.connections.size.must_equal 0
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'closes connection when client disconnects' do
|
21
|
-
ActionController::Live::SSE.any_instance.stubs(:write).raises(
|
21
|
+
ActionController::Live::SSE.any_instance.stubs(:write).raises(IOError)
|
22
22
|
|
23
23
|
manager.register(response)
|
24
|
-
|
24
|
+
response.headers['rack.hijack'].call(response.stream)
|
25
|
+
manager.connections.size.must_equal 1
|
25
26
|
manager.send_event('foo', 'bar')
|
26
|
-
manager.
|
27
|
+
manager.connections.size.must_equal 0
|
27
28
|
end
|
28
29
|
|
29
30
|
it 'writes string event to stream' do
|
30
31
|
manager.register(response)
|
32
|
+
response.headers['rack.hijack'].call(response.stream)
|
31
33
|
manager.send_event('foo', 'bar')
|
32
34
|
response.stream.string.must_equal "event: foo\ndata: bar\n\n"
|
33
35
|
end
|
34
36
|
|
35
37
|
it 'writes event object to stream' do
|
36
38
|
manager.register(response)
|
39
|
+
response.headers['rack.hijack'].call(response.stream)
|
37
40
|
manager.send_event('foo', { a: 123, 'b' => 'abc', c: { foo: 'bar' } })
|
38
41
|
response.stream.string.must_equal "event: foo\ndata: {\"a\":123,\"b\":\"abc\",\"c\":{\"foo\":\"bar\"}}\n\n"
|
39
42
|
end
|
@@ -2,11 +2,10 @@ require_relative '../test_helper'
|
|
2
2
|
|
3
3
|
describe SseRailsEngine do
|
4
4
|
before do
|
5
|
-
SseRailsEngine.disconnect_wait_time = 3.seconds
|
6
5
|
SseRailsEngine.heartbeat_interval = 5.seconds
|
7
6
|
end
|
8
7
|
|
9
|
-
it
|
8
|
+
it 'should be a module' do
|
10
9
|
assert_kind_of Module, SseRailsEngine
|
11
10
|
end
|
12
11
|
|
@@ -15,36 +14,19 @@ describe SseRailsEngine do
|
|
15
14
|
end
|
16
15
|
|
17
16
|
it 'should have default config values' do
|
18
|
-
SseRailsEngine.disconnect_wait_time.must_equal 3.seconds
|
19
17
|
SseRailsEngine.heartbeat_interval.must_equal 5.seconds
|
20
18
|
end
|
21
19
|
|
22
20
|
it 'allows usage of setup block' do
|
23
21
|
SseRailsEngine.setup do |config|
|
24
|
-
config.disconnect_wait_time.must_equal 3.seconds
|
25
22
|
config.heartbeat_interval.must_equal 5.seconds
|
26
23
|
end
|
27
24
|
end
|
28
25
|
|
29
26
|
it 'allows config override' do
|
30
27
|
SseRailsEngine.setup do |config|
|
31
|
-
config.disconnect_wait_time = 99.hours
|
32
28
|
config.heartbeat_interval = 1.minute
|
33
29
|
end
|
34
|
-
SseRailsEngine.disconnect_wait_time.must_equal 99.hours
|
35
30
|
SseRailsEngine.heartbeat_interval.must_equal 1.minute
|
36
31
|
end
|
37
|
-
|
38
|
-
it 'disconnects without a registry' do
|
39
|
-
response = Hashie::Mash.new(stream: StringIO.new, headers: {})
|
40
|
-
SseRailsEngine.disconnect_wait_time = 0.1
|
41
|
-
SseRailsEngine.wait_for_disconnect(response).must_equal true
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'disconnects after registry deletion' do
|
45
|
-
SseRailsEngine.manager.expects(:registered?).times(3).returns(true, true, false)
|
46
|
-
response = Hashie::Mash.new(stream: StringIO.new, headers: {})
|
47
|
-
SseRailsEngine.disconnect_wait_time = 0.1
|
48
|
-
SseRailsEngine.wait_for_disconnect(response).must_equal true
|
49
|
-
end
|
50
32
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# Configure Rails Environment
|
2
2
|
ENV['RAILS_ENV'] = 'test'
|
3
3
|
|
4
|
+
require 'codeclimate-test-reporter'
|
5
|
+
CodeClimate::TestReporter.start if ENV['CODECLIMATE_REPO_TOKEN']
|
6
|
+
|
4
7
|
require File.expand_path('../../test/dummy/config/environment.rb', __FILE__)
|
5
8
|
require 'rails/test_help'
|
6
9
|
require 'minitest/rails'
|
7
10
|
require 'mocha/mini_test'
|
8
11
|
require 'hashie'
|
9
|
-
require 'codeclimate-test-reporter'
|
10
|
-
|
11
|
-
CodeClimate::TestReporter.start if ENV['CODECLIMATE_REPO_TOKEN']
|
12
12
|
|
13
13
|
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
14
14
|
# to be shown.
|
@@ -22,6 +22,3 @@ if ActiveSupport::TestCase.respond_to?(:fixture_path=)
|
|
22
22
|
ActiveSupport::TestCase.fixture_path = File.expand_path('../fixtures', __FILE__)
|
23
23
|
ActiveSupport::TestCase.fixtures :all
|
24
24
|
end
|
25
|
-
|
26
|
-
class ActiveSupport::TestCase
|
27
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sse-rails-engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Hender
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -28,66 +28,66 @@ dependencies:
|
|
28
28
|
name: minitest-rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '2.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: mocha
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
47
|
+
version: '1.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
54
|
+
version: '1.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: hashie
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '3.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
68
|
+
version: '3.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: codeclimate-test-reporter
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description:
|
84
|
-
in Rails app
|
83
|
+
description: See the README.md at https://github.com/henders/sse-rails-engine
|
85
84
|
email:
|
86
|
-
-
|
85
|
+
- henders@gmail.com
|
87
86
|
executables: []
|
88
87
|
extensions: []
|
89
88
|
extra_rdoc_files: []
|
90
89
|
files:
|
90
|
+
- README.md
|
91
91
|
- Rakefile
|
92
92
|
- app/controllers/sse_rails_engine/application_controller.rb
|
93
93
|
- app/controllers/sse_rails_engine/sse_controller.rb
|