rapns 3.0.0.beta.1 → 3.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.md +75 -106
  2. data/bin/rapns +14 -14
  3. data/config/database.yml +8 -0
  4. data/lib/generators/rapns_generator.rb +5 -1
  5. data/lib/generators/templates/add_gcm.rb +8 -0
  6. data/lib/generators/templates/rapns.rb +39 -0
  7. data/lib/rapns.rb +1 -0
  8. data/lib/rapns/apns/notification.rb +2 -0
  9. data/lib/rapns/apns/required_fields_validator.rb +14 -0
  10. data/lib/rapns/configuration.rb +24 -33
  11. data/lib/rapns/daemon.rb +18 -14
  12. data/lib/rapns/daemon/apns/app_runner.rb +1 -1
  13. data/lib/rapns/daemon/apns/delivery.rb +1 -1
  14. data/lib/rapns/daemon/apns/feedback_receiver.rb +1 -1
  15. data/lib/rapns/daemon/app_runner.rb +13 -7
  16. data/lib/rapns/daemon/delivery_handler.rb +0 -4
  17. data/lib/rapns/daemon/feeder.rb +1 -5
  18. data/lib/rapns/daemon/logger.rb +22 -9
  19. data/lib/rapns/version.rb +1 -1
  20. data/lib/tasks/cane.rake +2 -3
  21. data/lib/tasks/test.rake +1 -2
  22. data/spec/unit/apns/notification_spec.rb +12 -2
  23. data/spec/unit/configuration_spec.rb +38 -0
  24. data/spec/unit/daemon/apns/app_runner_spec.rb +2 -0
  25. data/spec/unit/daemon/apns/delivery_spec.rb +10 -4
  26. data/spec/unit/daemon/apns/disconnection_error_spec.rb +18 -0
  27. data/spec/unit/daemon/apns/feedback_receiver_spec.rb +7 -7
  28. data/spec/unit/daemon/app_runner_spec.rb +51 -0
  29. data/spec/unit/daemon/database_reconnectable_spec.rb +2 -0
  30. data/spec/unit/daemon/delivery_error_spec.rb +2 -2
  31. data/spec/unit/daemon/delivery_handler_shared.rb +10 -1
  32. data/spec/unit/daemon/gcm/app_runner_spec.rb +3 -1
  33. data/spec/unit/daemon/logger_spec.rb +10 -2
  34. data/spec/unit/daemon_spec.rb +31 -14
  35. data/spec/unit_spec_helper.rb +3 -7
  36. metadata +10 -4
@@ -75,4 +75,55 @@ describe Rapns::Daemon::AppRunner, 'sync' do
75
75
  runner.should_receive(:stop)
76
76
  Rapns::Daemon::AppRunner.sync
77
77
  end
78
+
79
+ it 'logs an error if the app could not be started' do
80
+ Rapns::App.stub(:all => [app, new_app])
81
+ new_runner = stub
82
+ Rapns::Daemon::Apns::AppRunner.should_receive(:new).with(new_app).and_return(new_runner)
83
+ new_runner.stub(:start).and_raise(StandardError)
84
+ Rapns::Daemon.logger.should_receive(:error).any_number_of_times
85
+ Rapns::Daemon::AppRunner.sync
86
+ end
87
+ end
88
+
89
+ describe Rapns::Daemon::AppRunner, 'debug' do
90
+ let!(:app) { Rapns::Apns::App.create!(:name => 'test', :connections => 1,
91
+ :environment => 'development', :certificate => TEST_CERT) }
92
+ let(:logger) { stub(:info => nil) }
93
+
94
+ before do
95
+ Rapns::Daemon.stub(:config => {})
96
+ Rapns::Daemon::Apns::FeedbackReceiver.stub(:new => stub.as_null_object)
97
+ Rapns::Daemon::Apns::Connection.stub(:new => stub.as_null_object)
98
+ Rapns::Daemon.stub(:logger => logger)
99
+ Rapns::Daemon::AppRunner.sync
100
+ end
101
+
102
+ after { Rapns::Daemon::AppRunner.runners.clear }
103
+
104
+ it 'prints debug app states to the log' do
105
+ Rapns::Daemon.logger.should_receive(:info).with("\ntest:\n handlers: 1\n queued: 0\n idle: true\n")
106
+ Rapns::Daemon::AppRunner.debug
107
+ end
108
+ end
109
+
110
+ describe Rapns::Daemon::AppRunner, 'idle' do
111
+ let!(:app) { Rapns::Apns::App.create!(:name => 'test', :connections => 1,
112
+ :environment => 'development', :certificate => TEST_CERT) }
113
+ let(:logger) { stub(:info => nil) }
114
+
115
+ before do
116
+ Rapns::Daemon.stub(:config => {})
117
+ Rapns::Daemon::Apns::FeedbackReceiver.stub(:new => stub.as_null_object)
118
+ Rapns::Daemon::Apns::Connection.stub(:new => stub.as_null_object)
119
+ Rapns::Daemon.stub(:logger => logger)
120
+ Rapns::Daemon::AppRunner.sync
121
+ end
122
+
123
+ after { Rapns::Daemon::AppRunner.runners.clear }
124
+
125
+ it 'returns idle runners' do
126
+ runner = Rapns::Daemon::AppRunner.runners[app.id]
127
+ Rapns::Daemon::AppRunner.idle.should == [runner]
128
+ end
78
129
  end
@@ -30,6 +30,8 @@ describe Rapns::Daemon::DatabaseReconnectable do
30
30
  Mysql2::Error
31
31
  when 'jdbcpostgresql'
32
32
  ActiveRecord::JDBCError
33
+ when 'jdbcmysql'
34
+ ActiveRecord::JDBCError
33
35
  else
34
36
  raise "Please update #{__FILE__} for adapter #{$adapter}"
35
37
  end
@@ -4,10 +4,10 @@ describe Rapns::DeliveryError do
4
4
  let(:error) { Rapns::DeliveryError.new(4, 12, "Missing payload") }
5
5
 
6
6
  it "returns an informative message" do
7
- error.message.should == "Unable to deliver notification 12, received error 4 (Missing payload)"
7
+ error.to_s.should == "Unable to deliver notification 12, received error 4 (Missing payload)"
8
8
  end
9
9
 
10
10
  it "returns the error code" do
11
11
  error.code.should == 4
12
12
  end
13
- end
13
+ end
@@ -1,4 +1,13 @@
1
1
  shared_examples_for 'an DeliveryHandler subclass' do
2
+ it 'logs all delivery errors' do
3
+ logger = stub
4
+ Rapns::Daemon.stub(:logger => logger)
5
+ error = StandardError.new
6
+ delivery_handler.stub(:deliver).and_raise(error)
7
+ Rapns::Daemon.logger.should_receive(:error).with(error)
8
+ delivery_handler.send(:handle_next_notification)
9
+ end
10
+
2
11
  it "instructs the queue to wakeup the thread when told to stop" do
3
12
  thread = stub(:join => nil)
4
13
  Thread.stub(:new => thread)
@@ -16,4 +25,4 @@ shared_examples_for 'an DeliveryHandler subclass' do
16
25
  delivery_handler.send(:handle_next_notification)
17
26
  end
18
27
  end
19
- end
28
+ end
@@ -8,8 +8,10 @@ describe Rapns::Daemon::Gcm::AppRunner do
8
8
  let(:app) { app_class.new }
9
9
  let(:runner) { Rapns::Daemon::Gcm::AppRunner.new(app) }
10
10
  let(:handler) { stub(:start => nil, :stop => nil, :queue= => nil) }
11
+ let(:logger) { stub(:info => nil) }
11
12
 
12
13
  before do
14
+ Rapns::Daemon.stub(:logger => logger)
13
15
  Rapns::Daemon::Gcm::DeliveryHandler.stub(:new => handler)
14
16
  end
15
- end
17
+ end
@@ -31,6 +31,14 @@ describe Rapns::Daemon::Logger do
31
31
  ActiveSupport::BufferedLogger.stub(:new).and_return(@buffered_logger)
32
32
  Rapns::Daemon.stub(:config => config)
33
33
  File.stub(:open => log)
34
+ STDERR.stub(:puts)
35
+ end
36
+
37
+ it "disables logging if the log file cannot be opened" do
38
+ File.stub(:open).and_raise(Errno::ENOENT)
39
+ STDERR.should_receive(:puts).with(/No such file or directory/)
40
+ STDERR.should_receive(:puts).with(/Logging disabled/)
41
+ Rapns::Daemon::Logger.new(:foreground => true)
34
42
  end
35
43
 
36
44
  it "should open the a log file in the Rails log directory" do
@@ -50,13 +58,13 @@ describe Rapns::Daemon::Logger do
50
58
 
51
59
  it "should print out the msg if running in the foreground" do
52
60
  logger = Rapns::Daemon::Logger.new(:foreground => true)
53
- logger.should_receive(:puts).with(/hi mom/)
61
+ STDOUT.should_receive(:puts).with(/hi mom/)
54
62
  logger.info("hi mom")
55
63
  end
56
64
 
57
65
  it "should not print out the msg if not running in the foreground" do
58
66
  logger = Rapns::Daemon::Logger.new(:foreground => false)
59
- logger.should_not_receive(:puts).with(/hi mom/)
67
+ STDOUT.should_not_receive(:puts).with(/hi mom/)
60
68
  logger.info("hi mom")
61
69
  end
62
70
 
@@ -9,6 +9,7 @@ describe Rapns::Daemon, "when starting" do
9
9
  let(:logger) { stub(:info => nil, :error => nil, :warn => nil) }
10
10
 
11
11
  before do
12
+ Rapns.stub(:config => config)
12
13
  Rapns::Daemon::Feeder.stub(:start)
13
14
  Rapns::Daemon::Logger.stub(:new).and_return(logger)
14
15
  Rapns::Daemon::AppRunner.stub(:sync => nil, :stop => nil)
@@ -21,65 +22,65 @@ describe Rapns::Daemon, "when starting" do
21
22
  config.stub(:foreground => false)
22
23
  ActiveRecord::Base.stub(:establish_connection)
23
24
  Rapns::Daemon.should_receive(:daemonize)
24
- Rapns::Daemon.start(config)
25
+ Rapns::Daemon.start
25
26
  end
26
27
 
27
28
  it "does not fork into a daemon if the foreground option is true" do
28
29
  config.stub(:foreground => true)
29
30
  Rapns::Daemon.should_not_receive(:daemonize)
30
- Rapns::Daemon.start(config)
31
+ Rapns::Daemon.start
31
32
  end
32
33
 
33
34
  it "writes the process ID to the PID file" do
34
35
  Rapns::Daemon.should_receive(:write_pid_file)
35
- Rapns::Daemon.start(config)
36
+ Rapns::Daemon.start
36
37
  end
37
38
 
38
39
  it "logs an error if the PID file could not be written" do
39
40
  config.stub(:pid_file => '/rails_root/rapns.pid')
40
41
  File.stub(:open).and_raise(Errno::ENOENT)
41
42
  logger.should_receive(:error).with("Failed to write PID to '/rails_root/rapns.pid': #<Errno::ENOENT: No such file or directory>")
42
- Rapns::Daemon.start(config)
43
+ Rapns::Daemon.start
43
44
  end
44
45
 
45
46
  it "starts the feeder" do
46
47
  Rapns::Daemon::Feeder.should_receive(:start).with(2)
47
- Rapns::Daemon.start(config)
48
+ Rapns::Daemon.start
48
49
  end
49
50
 
50
51
  it "syncs apps" do
51
52
  Rapns::Daemon::AppRunner.should_receive(:sync)
52
- Rapns::Daemon.start(config)
53
+ Rapns::Daemon.start
53
54
  end
54
55
 
55
56
  it "sets up the logger" do
56
57
  config.stub(:airbrake_notify => true)
57
58
  Rapns::Daemon::Logger.should_receive(:new).with(:foreground => true, :airbrake_notify => true)
58
- Rapns::Daemon.start(config)
59
+ Rapns::Daemon.start
59
60
  end
60
61
 
61
62
  it "makes the logger accessible" do
62
- Rapns::Daemon.start(config)
63
+ Rapns::Daemon.start
63
64
  Rapns::Daemon.logger.should == logger
64
65
  end
65
66
 
66
67
  it 'prints a warning if there are no apps' do
67
68
  Rapns::App.stub(:count => 0)
68
69
  logger.should_receive(:warn).any_number_of_times
69
- Rapns::Daemon.start(config)
70
+ Rapns::Daemon.start
70
71
  end
71
72
 
72
73
  it 'prints a warning exists if rapns has not been upgraded' do
73
74
  Rapns::App.stub(:count).and_raise(ActiveRecord::StatementInvalid)
74
75
  Rapns::Daemon.should_receive(:puts).any_number_of_times
75
76
  Rapns::Daemon.should_receive(:exit).with(1)
76
- Rapns::Daemon.start(config)
77
+ Rapns::Daemon.start
77
78
  end
78
79
 
79
80
  it 'warns if rapns.yml still exists' do
80
81
  File.should_receive(:exists?).with('/rails_root/config/rapns/rapns.yml').and_return(true)
81
- logger.should_receive(:warn).with("Since 2.0.0 rapns uses command-line options instead of a configuration file. Please remove config/rapns/rapns.yml.")
82
- Rapns::Daemon.start(config)
82
+ logger.should_receive(:warn).with("Since 2.0.0 rapns uses command-line options and a Ruby based configuration file.\nPlease run 'rails g rapns' to generate a new configuration file into config/initializers.\nRemove config/rapns/rapns.yml to avoid this warning.\n")
83
+ Rapns::Daemon.start
83
84
  end
84
85
  end
85
86
 
@@ -87,11 +88,27 @@ describe Rapns::Daemon, "when being shutdown" do
87
88
  let(:config) { stub(:pid_file => '/rails_root/rapns.pid') }
88
89
 
89
90
  before do
90
- Rapns::Daemon.stub(:config => config, :puts => nil)
91
+ Rapns.stub(:config => config)
92
+ Rapns::Daemon.stub(:puts => nil)
91
93
  Rapns::Daemon::Feeder.stub(:stop)
92
94
  Rapns::Daemon::AppRunner.stub(:stop)
93
95
  end
94
96
 
97
+ # These tests do not work on JRuby.
98
+ unless defined? JRUBY_VERSION
99
+ it "shuts down when signaled signaled SIGINT" do
100
+ Rapns::Daemon.setup_signal_hooks
101
+ Rapns::Daemon.should_receive(:shutdown)
102
+ Process.kill("SIGINT", Process.pid)
103
+ end
104
+
105
+ it "shuts down when signaled signaled SIGTERM" do
106
+ Rapns::Daemon.setup_signal_hooks
107
+ Rapns::Daemon.should_receive(:shutdown)
108
+ Process.kill("SIGTERM", Process.pid)
109
+ end
110
+ end
111
+
95
112
  it "stops the feeder" do
96
113
  Rapns::Daemon::Feeder.should_receive(:stop)
97
114
  Rapns::Daemon.send(:shutdown)
@@ -119,4 +136,4 @@ describe Rapns::Daemon, "when being shutdown" do
119
136
  File.should_not_receive(:delete)
120
137
  Rapns::Daemon.send(:shutdown)
121
138
  end
122
- end
139
+ end
@@ -3,7 +3,7 @@ ENV['RAILS_ENV'] = 'test'
3
3
  begin
4
4
  require './spec/support/simplecov_helper'
5
5
  include SimpleCovHelper
6
- start_simple_cov('unit')
6
+ start_simple_cov("unit-#{RUBY_VERSION}")
7
7
  rescue LoadError
8
8
  puts "Coverage disabled."
9
9
  end
@@ -12,12 +12,8 @@ require 'active_record'
12
12
 
13
13
  jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
14
14
 
15
- $adapter = ENV['ADAPTER'] ||
16
- if jruby
17
- 'jdbcpostgresql'
18
- else
19
- 'postgresql'
20
- end
15
+ $adapter = ENV['ADAPTER'] || 'postgresql'
16
+ $adapter = 'jdbc' + $adapter if jruby
21
17
 
22
18
  DATABASE_CONFIG = YAML.load_file(File.expand_path("../config/database.yml", File.dirname(__FILE__)))
23
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapns
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.beta.1
4
+ version: 3.0.0.rc.1
5
5
  prerelease: 6
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: 2012-12-04 00:00:00.000000000 Z
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -43,7 +43,7 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
- description: Easy to use, full featured APNs daemon for Rails 3
46
+ description: Professional grade APNs and GCM daemon
47
47
  email:
48
48
  - port001@gmail.com
49
49
  executables:
@@ -61,12 +61,14 @@ files:
61
61
  - lib/generators/templates/create_rapns_apps.rb
62
62
  - lib/generators/templates/create_rapns_feedback.rb
63
63
  - lib/generators/templates/create_rapns_notifications.rb
64
+ - lib/generators/templates/rapns.rb
64
65
  - lib/rapns.rb
65
66
  - lib/rapns/apns/app.rb
66
67
  - lib/rapns/apns/binary_notification_validator.rb
67
68
  - lib/rapns/apns/device_token_format_validator.rb
68
69
  - lib/rapns/apns/feedback.rb
69
70
  - lib/rapns/apns/notification.rb
71
+ - lib/rapns/apns/required_fields_validator.rb
70
72
  - lib/rapns/app.rb
71
73
  - lib/rapns/configuration.rb
72
74
  - lib/rapns/daemon.rb
@@ -111,10 +113,12 @@ files:
111
113
  - spec/unit/apns/feedback_spec.rb
112
114
  - spec/unit/apns/notification_spec.rb
113
115
  - spec/unit/app_spec.rb
116
+ - spec/unit/configuration_spec.rb
114
117
  - spec/unit/daemon/apns/app_runner_spec.rb
115
118
  - spec/unit/daemon/apns/connection_spec.rb
116
119
  - spec/unit/daemon/apns/delivery_handler_spec.rb
117
120
  - spec/unit/daemon/apns/delivery_spec.rb
121
+ - spec/unit/daemon/apns/disconnection_error_spec.rb
118
122
  - spec/unit/daemon/apns/feedback_receiver_spec.rb
119
123
  - spec/unit/daemon/app_runner_shared.rb
120
124
  - spec/unit/daemon/app_runner_spec.rb
@@ -158,7 +162,7 @@ rubyforge_project:
158
162
  rubygems_version: 1.8.23
159
163
  signing_key:
160
164
  specification_version: 3
161
- summary: Easy to use, full featured APNs daemon for Rails 3
165
+ summary: Professional grade APNs and GCM daemon
162
166
  test_files:
163
167
  - config/database.yml
164
168
  - spec/acceptance/gcm_upgrade_spec.rb
@@ -169,10 +173,12 @@ test_files:
169
173
  - spec/unit/apns/feedback_spec.rb
170
174
  - spec/unit/apns/notification_spec.rb
171
175
  - spec/unit/app_spec.rb
176
+ - spec/unit/configuration_spec.rb
172
177
  - spec/unit/daemon/apns/app_runner_spec.rb
173
178
  - spec/unit/daemon/apns/connection_spec.rb
174
179
  - spec/unit/daemon/apns/delivery_handler_spec.rb
175
180
  - spec/unit/daemon/apns/delivery_spec.rb
181
+ - spec/unit/daemon/apns/disconnection_error_spec.rb
176
182
  - spec/unit/daemon/apns/feedback_receiver_spec.rb
177
183
  - spec/unit/daemon/app_runner_shared.rb
178
184
  - spec/unit/daemon/app_runner_spec.rb