sinatra-cometio 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sinatra-cometio.gemspec
4
4
  gemspec
5
+ gem 'minitest'
6
+ gem 'thin'
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.1.5 2013-3-3
2
+
3
+ * add tests with ruby client
4
+
1
5
  === 0.1.4 2013-3-2
2
6
 
3
7
  * add CometIO::Client
data/README.md CHANGED
@@ -140,6 +140,16 @@ chat app
140
140
  - https://github.com/shokai/sinatra-cometio/tree/master/sample
141
141
 
142
142
 
143
+ Test
144
+ ----
145
+
146
+ % gem install bundler
147
+ % bundle install
148
+ % export PORT=5000
149
+ % export PID_FILE=/tmp/sinatra-cometio-test.pid
150
+ % rake test
151
+
152
+
143
153
  Contributing
144
154
  ------------
145
155
  1. Fork it
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/test_*.rb"
6
+ end
7
+
8
+ task :default => :test
@@ -1,3 +1,3 @@
1
1
  module SinatraCometIO
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -22,5 +22,6 @@
22
22
 
23
23
  %hr
24
24
  %div#footer
25
+ sinatra-cometio v#{SinatraCometIO::VERSION} -
25
26
  - src_url = 'https://github.com/shokai/sinatra-cometio'
26
27
  %a{:href => src_url} #{src_url}
data/test/app.rb ADDED
@@ -0,0 +1,57 @@
1
+ class App
2
+
3
+ def self.running
4
+ @running ? true : false
5
+ end
6
+
7
+ def self.port
8
+ ENV['PORT'] || 5000
9
+ end
10
+
11
+ def self.cometio_url
12
+ "http://localhost:#{port}/cometio/io"
13
+ end
14
+
15
+ def self.pid_file
16
+ ENV['PID_FILE'] || "/tmp/sinatra-cometio-testapp.pid"
17
+ end
18
+
19
+ def self.app_dir
20
+ File.expand_path 'app', File.dirname(__FILE__)
21
+ end
22
+
23
+ def self.pid
24
+ return unless @running
25
+ File.open(pid_file) do |f|
26
+ pid = f.gets.strip.to_i
27
+ return pid if pid > 0
28
+ end
29
+ return
30
+ end
31
+
32
+ def self.start
33
+ return if @running
34
+ File.delete pid_file if File.exists? pid_file
35
+ Thread.new do
36
+ IO::popen "cd #{app_dir} && PID_FILE=#{pid_file} rackup config.ru -p #{port} > /dev/null 2>&1"
37
+ end
38
+ @running = true
39
+ 100.times do
40
+ if File.exists? pid_file
41
+ sleep 1
42
+ return true
43
+ end
44
+ sleep 0.1
45
+ end
46
+ @running = false
47
+ return false
48
+ end
49
+
50
+ def self.stop
51
+ return unless @running
52
+ system "kill #{pid}"
53
+ File.delete pid_file if File.exists? pid_file
54
+ @running = false
55
+ end
56
+
57
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require
4
+ $stdout.sync = true
5
+ $:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
6
+ require 'sinatra'
7
+ require 'sinatra/cometio'
8
+ require File.dirname(__FILE__)+'/main'
9
+
10
+ run Sinatra::Application
data/test/app/main.rb ADDED
@@ -0,0 +1,27 @@
1
+ pid_file = ENV['PID_FILE'] || "/tmp/sinatra-cometio-test-pid"
2
+ File.open(pid_file, "w+") do |f|
3
+ f.write Process.pid.to_s
4
+ end
5
+
6
+ get '/' do
7
+ "sinatra-cometio v#{SinatraCometIO::VERSION}"
8
+ end
9
+
10
+ CometIO.on :connect do |session|
11
+ puts "new client <#{session}>"
12
+ end
13
+
14
+ CometIO.on :disconnect do |session|
15
+ puts "disconnect client <#{session}>"
16
+ end
17
+
18
+ CometIO.on :broadcast do |data, from|
19
+ puts from
20
+ puts "broadcast <#{from}> - #{data.to_json}"
21
+ push :broadcast, data
22
+ end
23
+
24
+ CometIO.on :message do |data, from|
25
+ puts "message <#{from}> - #{data.to_json}"
26
+ push :message, data, :to => data['to']
27
+ end
@@ -0,0 +1,99 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestCometio < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ App.start
7
+ end
8
+
9
+ def teardown
10
+ App.stop
11
+ end
12
+
13
+ def test_simple
14
+ client = CometIO::Client.new(App.cometio_url).connect
15
+ post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
16
+ res = nil
17
+ client.on :broadcast do |data|
18
+ res = data
19
+ end
20
+
21
+ client.on :connect do |session|
22
+ push :broadcast, post_data
23
+ end
24
+
25
+ 30.times do
26
+ break if res != nil
27
+ sleep 0.1
28
+ end
29
+ assert res != nil, 'server not respond'
30
+ assert res["time"] == post_data[:time]
31
+ assert res["msg"] == post_data[:msg]
32
+ end
33
+
34
+ def test_client_to_client2
35
+ ## client --> server --> client2
36
+
37
+ client = CometIO::Client.new(App.cometio_url).connect
38
+ post_data = {:time => Time.now.to_s, :msg => 'hello!!', :to => nil}
39
+ res = nil
40
+ client.on :message do |data|
41
+ res = data
42
+ end
43
+
44
+ res2 = nil
45
+ client.on :connect do |session|
46
+ client2 = CometIO::Client.new(App.cometio_url).connect
47
+ client2.on :connect do |session2|
48
+ post_data['to'] = session2
49
+ client.push :message, post_data
50
+ end
51
+ client2.on :message do |data|
52
+ res2 = data
53
+ end
54
+ end
55
+
56
+ 30.times do
57
+ break if res != nil
58
+ sleep 0.1
59
+ end
60
+ assert res2 != nil, 'server not respond'
61
+ assert res2["time"] == post_data[:time]
62
+ assert res2["msg"] == post_data[:msg]
63
+ assert res == nil
64
+ end
65
+
66
+
67
+ def test_broadcast
68
+ ## client --> server --> client&client2
69
+ client = CometIO::Client.new(App.cometio_url).connect
70
+ post_data = {:time => Time.now.to_s, :msg => 'hello!!'}
71
+ res = nil
72
+ client.on :broadcast do |data|
73
+ res = data
74
+ end
75
+
76
+ res2 = nil
77
+ client.on :connect do |session|
78
+ client2 = CometIO::Client.new(App.cometio_url).connect
79
+ client2.on :connect do |session2|
80
+ client.push :broadcast, post_data
81
+ end
82
+ client2.on :broadcast do |data|
83
+ res2 = data
84
+ end
85
+ end
86
+
87
+ 30.times do
88
+ break if res != nil
89
+ sleep 0.1
90
+ end
91
+ assert res != nil, 'server not respond'
92
+ assert res["time"] == post_data[:time]
93
+ assert res["msg"] == post_data[:msg]
94
+ assert res2 != nil
95
+ assert res2["time"] == post_data[:time]
96
+ assert res2["msg"] == post_data[:msg]
97
+ end
98
+
99
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'sinatra/cometio/client'
4
+ require File.expand_path 'app', File.dirname(__FILE__)
5
+
6
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
7
+
8
+ ['SIGHUP', 'SIGINT', 'SIGKILL', 'SIGTERM'].each do |sig|
9
+ Kernel.trap sig do
10
+ App.stop
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-cometio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-01 00:00:00.000000000 Z
12
+ date: 2013-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -155,6 +155,11 @@ files:
155
155
  - sample/views/index.haml
156
156
  - sample/views/main.scss
157
157
  - sinatra-cometio.gemspec
158
+ - test/app.rb
159
+ - test/app/config.ru
160
+ - test/app/main.rb
161
+ - test/test_cometio.rb
162
+ - test/test_helper.rb
158
163
  homepage: http://shokai.github.com/sinatra-cometio
159
164
  licenses: []
160
165
  post_install_message:
@@ -179,4 +184,9 @@ rubygems_version: 1.8.24
179
184
  signing_key:
180
185
  specification_version: 3
181
186
  summary: Node.js like Comet I/O plugin for Sinatra.
182
- test_files: []
187
+ test_files:
188
+ - test/app.rb
189
+ - test/app/config.ru
190
+ - test/app/main.rb
191
+ - test/test_cometio.rb
192
+ - test/test_helper.rb