rapns_rails_2 3.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGELOG.md +83 -0
- data/LICENSE +7 -0
- data/README.md +168 -0
- data/bin/rapns +37 -0
- data/config/database.yml +44 -0
- data/lib/generators/rapns_generator.rb +25 -0
- data/lib/generators/templates/add_alert_is_json_to_rapns_notifications.rb +9 -0
- data/lib/generators/templates/add_app_to_rapns.rb +11 -0
- data/lib/generators/templates/add_gcm.rb +95 -0
- data/lib/generators/templates/create_rapns_apps.rb +16 -0
- data/lib/generators/templates/create_rapns_feedback.rb +15 -0
- data/lib/generators/templates/create_rapns_notifications.rb +26 -0
- data/lib/generators/templates/rapns.rb +87 -0
- data/lib/rapns/TODO +3 -0
- data/lib/rapns/apns/app.rb +25 -0
- data/lib/rapns/apns/binary_notification_validator.rb +12 -0
- data/lib/rapns/apns/device_token_format_validator.rb +12 -0
- data/lib/rapns/apns/feedback.rb +16 -0
- data/lib/rapns/apns/notification.rb +91 -0
- data/lib/rapns/apns_feedback.rb +13 -0
- data/lib/rapns/app.rb +16 -0
- data/lib/rapns/configuration.rb +89 -0
- data/lib/rapns/daemon/apns/app_runner.rb +26 -0
- data/lib/rapns/daemon/apns/certificate_expired_error.rb +20 -0
- data/lib/rapns/daemon/apns/connection.rb +142 -0
- data/lib/rapns/daemon/apns/delivery.rb +64 -0
- data/lib/rapns/daemon/apns/delivery_handler.rb +35 -0
- data/lib/rapns/daemon/apns/disconnection_error.rb +20 -0
- data/lib/rapns/daemon/apns/feedback_receiver.rb +89 -0
- data/lib/rapns/daemon/app_runner.rb +179 -0
- data/lib/rapns/daemon/batch.rb +112 -0
- data/lib/rapns/daemon/delivery.rb +23 -0
- data/lib/rapns/daemon/delivery_error.rb +19 -0
- data/lib/rapns/daemon/delivery_handler.rb +52 -0
- data/lib/rapns/daemon/delivery_handler_collection.rb +33 -0
- data/lib/rapns/daemon/feeder.rb +65 -0
- data/lib/rapns/daemon/gcm/app_runner.rb +13 -0
- data/lib/rapns/daemon/gcm/delivery.rb +228 -0
- data/lib/rapns/daemon/gcm/delivery_handler.rb +20 -0
- data/lib/rapns/daemon/interruptible_sleep.rb +65 -0
- data/lib/rapns/daemon/reflectable.rb +13 -0
- data/lib/rapns/daemon/store/active_record/reconnectable.rb +66 -0
- data/lib/rapns/daemon/store/active_record.rb +128 -0
- data/lib/rapns/daemon.rb +129 -0
- data/lib/rapns/deprecatable.rb +23 -0
- data/lib/rapns/deprecation.rb +23 -0
- data/lib/rapns/embed.rb +28 -0
- data/lib/rapns/gcm/app.rb +7 -0
- data/lib/rapns/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +11 -0
- data/lib/rapns/gcm/notification.rb +37 -0
- data/lib/rapns/gcm/payload_data_size_validator.rb +13 -0
- data/lib/rapns/gcm/registration_ids_count_validator.rb +13 -0
- data/lib/rapns/logger.rb +76 -0
- data/lib/rapns/multi_json_helper.rb +16 -0
- data/lib/rapns/notification.rb +62 -0
- data/lib/rapns/notifier.rb +35 -0
- data/lib/rapns/push.rb +17 -0
- data/lib/rapns/rails-2-compatibility.rb +34 -0
- data/lib/rapns/reflection.rb +44 -0
- data/lib/rapns/upgraded.rb +31 -0
- data/lib/rapns/version.rb +3 -0
- data/lib/rapns_rails_2.rb +67 -0
- data/lib/tasks/cane.rake +18 -0
- data/lib/tasks/test.rake +38 -0
- data/spec/support/cert_with_password.pem +90 -0
- data/spec/support/cert_without_password.pem +59 -0
- data/spec/support/simplecov_helper.rb +13 -0
- data/spec/support/simplecov_quality_formatter.rb +8 -0
- data/spec/tmp/.gitkeep +0 -0
- data/spec/unit/apns/app_spec.rb +29 -0
- data/spec/unit/apns/feedback_spec.rb +9 -0
- data/spec/unit/apns/notification_spec.rb +215 -0
- data/spec/unit/apns_feedback_spec.rb +21 -0
- data/spec/unit/app_spec.rb +16 -0
- data/spec/unit/configuration_spec.rb +55 -0
- data/spec/unit/daemon/apns/app_runner_spec.rb +45 -0
- data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +11 -0
- data/spec/unit/daemon/apns/connection_spec.rb +287 -0
- data/spec/unit/daemon/apns/delivery_handler_spec.rb +59 -0
- data/spec/unit/daemon/apns/delivery_spec.rb +101 -0
- data/spec/unit/daemon/apns/disconnection_error_spec.rb +18 -0
- data/spec/unit/daemon/apns/feedback_receiver_spec.rb +134 -0
- data/spec/unit/daemon/app_runner_shared.rb +83 -0
- data/spec/unit/daemon/app_runner_spec.rb +170 -0
- data/spec/unit/daemon/batch_spec.rb +219 -0
- data/spec/unit/daemon/delivery_error_spec.rb +13 -0
- data/spec/unit/daemon/delivery_handler_collection_spec.rb +37 -0
- data/spec/unit/daemon/delivery_handler_shared.rb +45 -0
- data/spec/unit/daemon/feeder_spec.rb +81 -0
- data/spec/unit/daemon/gcm/app_runner_spec.rb +19 -0
- data/spec/unit/daemon/gcm/delivery_handler_spec.rb +44 -0
- data/spec/unit/daemon/gcm/delivery_spec.rb +289 -0
- data/spec/unit/daemon/interruptible_sleep_spec.rb +68 -0
- data/spec/unit/daemon/reflectable_spec.rb +27 -0
- data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +114 -0
- data/spec/unit/daemon/store/active_record_spec.rb +281 -0
- data/spec/unit/daemon_spec.rb +157 -0
- data/spec/unit/deprecatable_spec.rb +32 -0
- data/spec/unit/deprecation_spec.rb +15 -0
- data/spec/unit/embed_spec.rb +50 -0
- data/spec/unit/gcm/app_spec.rb +4 -0
- data/spec/unit/gcm/notification_spec.rb +52 -0
- data/spec/unit/logger_spec.rb +180 -0
- data/spec/unit/notification_shared.rb +45 -0
- data/spec/unit/notification_spec.rb +4 -0
- data/spec/unit/notifier_spec.rb +32 -0
- data/spec/unit/push_spec.rb +44 -0
- data/spec/unit/rapns_spec.rb +9 -0
- data/spec/unit/reflection_spec.rb +30 -0
- data/spec/unit/upgraded_spec.rb +40 -0
- data/spec/unit_spec_helper.rb +137 -0
- metadata +232 -0
@@ -0,0 +1,281 @@
|
|
1
|
+
require File.expand_path("spec/unit_spec_helper")
|
2
|
+
require 'rapns/daemon/store/active_record'
|
3
|
+
|
4
|
+
describe Rapns::Daemon::Store::ActiveRecord do
|
5
|
+
let(:app) { Rapns::Apns::App.create!(:name => 'my_app', :environment => 'development', :certificate => TEST_CERT) }
|
6
|
+
let(:notification) { Rapns::Apns::Notification.create!(:device_token => "a" * 64, :app => app) }
|
7
|
+
let(:store) { Rapns::Daemon::Store::ActiveRecord.new }
|
8
|
+
let(:now) { Time.now.utc }
|
9
|
+
|
10
|
+
before { Time.stub(:now => now) }
|
11
|
+
|
12
|
+
it 'reconnects after daemonize' do
|
13
|
+
store.should_receive(:reconnect_database)
|
14
|
+
store.after_daemonize
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'deliverable_notifications' do
|
18
|
+
it 'checks for new notifications with the ability to reconnect the database' do
|
19
|
+
store.should_receive(:with_database_reconnect_and_retry)
|
20
|
+
store.deliverable_notifications(app)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'loads notifications in batches' do
|
24
|
+
Rapns.config.batch_size = 5000
|
25
|
+
Rapns.config.push = false
|
26
|
+
relation = double.as_null_object
|
27
|
+
relation.should_receive(:find).with(:all, :limit => 5000)
|
28
|
+
Rapns::Notification.stub(:ready_for_delivery => relation)
|
29
|
+
store.deliverable_notifications([app])
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'does not load notification in batches if in push mode' do
|
33
|
+
Rapns.config.push = true
|
34
|
+
relation = double.as_null_object
|
35
|
+
relation.should_not_receive(:limit)
|
36
|
+
Rapns::Notification.stub(:ready_for_delivery => relation)
|
37
|
+
store.deliverable_notifications([app])
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'loads an undelivered notification without deliver_after set' do
|
41
|
+
notification.update_attributes!(:delivered => false, :deliver_after => nil)
|
42
|
+
store.deliverable_notifications([app]).should == [notification]
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'loads an notification with a deliver_after time in the past' do
|
46
|
+
notification.update_attributes!(:delivered => false, :deliver_after => 1.hour.ago)
|
47
|
+
store.deliverable_notifications([app]).should == [notification]
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'does not load an notification with a deliver_after time in the future' do
|
51
|
+
notification.update_attributes!(:delivered => false, :deliver_after => 1.hour.from_now)
|
52
|
+
store.deliverable_notifications([app]).should be_empty
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'does not load a previously delivered notification' do
|
56
|
+
notification.update_attributes!(:delivered => true, :delivered_at => Time.now)
|
57
|
+
store.deliverable_notifications([app]).should be_empty
|
58
|
+
end
|
59
|
+
|
60
|
+
it "does not enqueue a notification that has previously failed delivery" do
|
61
|
+
notification.update_attributes!(:delivered => false, :failed => true)
|
62
|
+
store.deliverable_notifications([app]).should be_empty
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'does not load notifications for apps that are still processing the previous batch' do
|
66
|
+
notification
|
67
|
+
store.deliverable_notifications([]).should be_empty
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe 'mark_retryable' do
|
72
|
+
it 'increments the retry count' do
|
73
|
+
expect do
|
74
|
+
store.mark_retryable(notification, now)
|
75
|
+
end.to change(notification, :retries).by(1)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'sets the deliver after timestamp' do
|
79
|
+
deliver_after = now + 10.seconds
|
80
|
+
expect do
|
81
|
+
store.mark_retryable(notification, deliver_after)
|
82
|
+
end.to change(notification, :deliver_after).to(deliver_after)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'saves the notification without validation' do
|
86
|
+
notification.should_receive(:save).with(false)
|
87
|
+
store.mark_retryable(notification, now)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'mark_batch_retryable' do
|
92
|
+
let(:deliver_after) { now + 10.seconds }
|
93
|
+
|
94
|
+
it 'sets the attributes on the object for use in reflections' do
|
95
|
+
store.mark_batch_retryable([notification], deliver_after)
|
96
|
+
notification.deliver_after.should == deliver_after
|
97
|
+
notification.retries.should == 1
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'increments the retired count' do
|
101
|
+
expect do
|
102
|
+
store.mark_batch_retryable([notification], deliver_after)
|
103
|
+
notification.reload
|
104
|
+
end.to change(notification, :retries).by(1)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'sets the deliver after timestamp' do
|
108
|
+
expect do
|
109
|
+
store.mark_batch_retryable([notification], deliver_after)
|
110
|
+
notification.reload
|
111
|
+
end.to change { notification.deliver_after.try(:utc).to_s }.to(deliver_after.utc.to_s)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe 'mark_delivered' do
|
116
|
+
it 'marks the notification as delivered' do
|
117
|
+
expect do
|
118
|
+
store.mark_delivered(notification)
|
119
|
+
end.to change(notification, :delivered).to(true)
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'sets the time the notification was delivered' do
|
123
|
+
expect do
|
124
|
+
store.mark_delivered(notification)
|
125
|
+
notification.reload
|
126
|
+
end.to change { notification.delivered_at.try(:utc).to_s }.to(now.to_s)
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'saves the notification without validation' do
|
130
|
+
notification.should_receive(:save).with(false)
|
131
|
+
store.mark_delivered(notification)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'mark_batch_delivered' do
|
136
|
+
it 'sets the attributes on the object for use in reflections' do
|
137
|
+
store.mark_batch_delivered([notification])
|
138
|
+
notification.delivered_at.should == now
|
139
|
+
notification.delivered.should be_true
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'marks the notifications as delivered' do
|
143
|
+
expect do
|
144
|
+
store.mark_batch_delivered([notification])
|
145
|
+
notification.reload
|
146
|
+
end.to change(notification, :delivered).to(true)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'sets the time the notifications were delivered' do
|
150
|
+
expect do
|
151
|
+
store.mark_batch_delivered([notification])
|
152
|
+
notification.reload
|
153
|
+
end.to change { notification.delivered_at.try(:utc).to_s }.to(now.to_s)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe 'mark_failed' do
|
158
|
+
it 'marks the notification as not delivered' do
|
159
|
+
store.mark_failed(notification, nil, '')
|
160
|
+
notification.delivered.should be_false
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'marks the notification as failed' do
|
164
|
+
expect do
|
165
|
+
store.mark_failed(notification, nil, '')
|
166
|
+
notification.reload
|
167
|
+
end.to change(notification, :failed).to(true)
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'sets the time the notification delivery failed' do
|
171
|
+
expect do
|
172
|
+
store.mark_failed(notification, nil, '')
|
173
|
+
notification.reload
|
174
|
+
end.to change { notification.failed_at.try(:utc).to_s }.to(now.to_s)
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'sets the error code' do
|
178
|
+
expect do
|
179
|
+
store.mark_failed(notification, 42, '')
|
180
|
+
end.to change(notification, :error_code).to(42)
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'sets the error description' do
|
184
|
+
expect do
|
185
|
+
store.mark_failed(notification, 42, 'Weeee')
|
186
|
+
end.to change(notification, :error_description).to('Weeee')
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'saves the notification without validation' do
|
190
|
+
notification.should_receive(:save).with(false)
|
191
|
+
store.mark_failed(notification, nil, '')
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe 'mark_batch_failed' do
|
196
|
+
it 'sets the attributes on the object for use in reflections' do
|
197
|
+
store.mark_batch_failed([notification], 123, 'an error')
|
198
|
+
notification.failed_at.should == now
|
199
|
+
notification.delivered_at.should be_nil
|
200
|
+
notification.delivered.should be_false
|
201
|
+
notification.failed.should be_true
|
202
|
+
notification.error_code.should == 123
|
203
|
+
notification.error_description.should == 'an error'
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'marks the notification as not delivered' do
|
207
|
+
store.mark_batch_failed([notification], nil, '')
|
208
|
+
notification.reload
|
209
|
+
notification.delivered.should be_false
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'marks the notification as failed' do
|
213
|
+
expect do
|
214
|
+
store.mark_batch_failed([notification], nil, '')
|
215
|
+
notification.reload
|
216
|
+
end.to change(notification, :failed).to(true)
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'sets the time the notification delivery failed' do
|
220
|
+
expect do
|
221
|
+
store.mark_batch_failed([notification], nil, '')
|
222
|
+
notification.reload
|
223
|
+
end.to change { notification.failed_at.try(:utc).to_s }.to(now.to_s)
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'sets the error code' do
|
227
|
+
expect do
|
228
|
+
store.mark_batch_failed([notification], 42, '')
|
229
|
+
notification.reload
|
230
|
+
end.to change(notification, :error_code).to(42)
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'sets the error description' do
|
234
|
+
expect do
|
235
|
+
store.mark_batch_failed([notification], 42, 'Weeee')
|
236
|
+
notification.reload
|
237
|
+
end.to change(notification, :error_description).to('Weeee')
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
describe 'create_apns_feedback' do
|
242
|
+
it 'creates the Feedback record' do
|
243
|
+
Rapns::Apns::Feedback.should_receive(:create!).with(
|
244
|
+
:failed_at => now, :device_token => 'ab' * 32, :app => app)
|
245
|
+
store.create_apns_feedback(now, 'ab' * 32, app)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
describe 'create_gcm_notification' do
|
250
|
+
let(:data) { { :data => true } }
|
251
|
+
let(:attributes) { { :device_token => 'ab' * 32 } }
|
252
|
+
let(:registration_ids) { ['123', '456'] }
|
253
|
+
let(:deliver_after) { now + 10.seconds }
|
254
|
+
let(:args) { [attributes, data, registration_ids, deliver_after, app] }
|
255
|
+
|
256
|
+
it 'sets the given attributes' do
|
257
|
+
new_notification = store.create_gcm_notification(*args)
|
258
|
+
new_notification.device_token.should == 'ab' * 32
|
259
|
+
end
|
260
|
+
|
261
|
+
it 'sets the given data' do
|
262
|
+
new_notification = store.create_gcm_notification(*args)
|
263
|
+
new_notification.data['data'].should be_true
|
264
|
+
end
|
265
|
+
|
266
|
+
it 'sets the given registration IDs' do
|
267
|
+
new_notification = store.create_gcm_notification(*args)
|
268
|
+
new_notification.registration_ids.should == registration_ids
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'sets the deliver_after timestamp' do
|
272
|
+
new_notification = store.create_gcm_notification(*args)
|
273
|
+
new_notification.deliver_after.to_s.should == deliver_after.to_s
|
274
|
+
end
|
275
|
+
|
276
|
+
it 'saves the new notification' do
|
277
|
+
new_notification = store.create_gcm_notification(*args)
|
278
|
+
new_notification.new_record?.should be_false
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require File.expand_path("spec/unit_spec_helper")
|
2
|
+
require 'rapns/daemon/store/active_record'
|
3
|
+
|
4
|
+
describe Rapns::Daemon, "when starting" do
|
5
|
+
module Rails; end
|
6
|
+
|
7
|
+
let(:certificate) { double }
|
8
|
+
let(:password) { double }
|
9
|
+
let(:config) { double(:pid_file => nil, :airbrake_notify => false,
|
10
|
+
:foreground => true, :embedded => false, :push => false,
|
11
|
+
:store => :active_record, :logger => nil) }
|
12
|
+
let(:logger) { double(:logger, :info => nil, :error => nil, :warn => nil) }
|
13
|
+
|
14
|
+
before do
|
15
|
+
Rapns.stub(:config => config, :logger => logger)
|
16
|
+
Rapns::Daemon::Feeder.stub(:start)
|
17
|
+
Rapns::Daemon::AppRunner.stub(:sync => nil, :stop => nil)
|
18
|
+
Rapns::Daemon.stub(:daemonize => nil, :exit => nil, :puts => nil)
|
19
|
+
File.stub(:open)
|
20
|
+
Rails.stub(:root).and_return("/rails_root")
|
21
|
+
end
|
22
|
+
|
23
|
+
unless Rapns.jruby?
|
24
|
+
it "forks into a daemon if the foreground option is false" do
|
25
|
+
config.stub(:foreground => false)
|
26
|
+
Rapns::Daemon.initialize_store
|
27
|
+
Rapns::Daemon.store.stub(:after_daemonize => nil)
|
28
|
+
Rapns::Daemon.should_receive(:daemonize)
|
29
|
+
Rapns::Daemon.start
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'notifies the store after forking' do
|
33
|
+
config.stub(:foreground => false)
|
34
|
+
Rapns::Daemon.initialize_store
|
35
|
+
Rapns::Daemon.store.should_receive(:after_daemonize)
|
36
|
+
Rapns::Daemon.start
|
37
|
+
end
|
38
|
+
|
39
|
+
it "does not fork into a daemon if the foreground option is true" do
|
40
|
+
config.stub(:foreground => true)
|
41
|
+
Rapns::Daemon.should_not_receive(:daemonize)
|
42
|
+
Rapns::Daemon.start
|
43
|
+
end
|
44
|
+
|
45
|
+
it "does not fork into a daemon if the push option is true" do
|
46
|
+
config.stub(:push => true)
|
47
|
+
Rapns::Daemon.should_not_receive(:daemonize)
|
48
|
+
Rapns::Daemon.start
|
49
|
+
end
|
50
|
+
|
51
|
+
it "does not fork into a daemon if the embedded option is true" do
|
52
|
+
config.stub(:embedded => true)
|
53
|
+
Rapns::Daemon.should_not_receive(:daemonize)
|
54
|
+
Rapns::Daemon.start
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'sets up setup signal traps' do
|
59
|
+
Rapns::Daemon.should_receive(:setup_signal_traps)
|
60
|
+
Rapns::Daemon.start
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'does not setup signal traps when embedded' do
|
64
|
+
config.stub(:embedded => true)
|
65
|
+
Rapns::Daemon.should_not_receive(:setup_signal_traps)
|
66
|
+
Rapns::Daemon.start
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'instantiates the store' do
|
70
|
+
config.stub(:store => :active_record)
|
71
|
+
Rapns::Daemon.start
|
72
|
+
Rapns::Daemon.store.should be_kind_of(Rapns::Daemon::Store::ActiveRecord)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'logs an error if the store cannot be loaded' do
|
76
|
+
config.stub(:store => :foo_bar)
|
77
|
+
Rapns.logger.should_receive(:error).with(kind_of(LoadError))
|
78
|
+
Rapns::Daemon.start
|
79
|
+
end
|
80
|
+
|
81
|
+
it "writes the process ID to the PID file" do
|
82
|
+
Rapns::Daemon.should_receive(:write_pid_file)
|
83
|
+
Rapns::Daemon.start
|
84
|
+
end
|
85
|
+
|
86
|
+
it "logs an error if the PID file could not be written" do
|
87
|
+
config.stub(:pid_file => '/rails_root/rapns.pid')
|
88
|
+
File.stub(:open).and_raise(Errno::ENOENT)
|
89
|
+
logger.should_receive(:error).with("Failed to write PID to '/rails_root/rapns.pid': #<Errno::ENOENT: No such file or directory>")
|
90
|
+
Rapns::Daemon.start
|
91
|
+
end
|
92
|
+
|
93
|
+
it "starts the feeder" do
|
94
|
+
Rapns::Daemon::Feeder.should_receive(:start)
|
95
|
+
Rapns::Daemon.start
|
96
|
+
end
|
97
|
+
|
98
|
+
it "syncs apps" do
|
99
|
+
Rapns::Daemon::AppRunner.should_receive(:sync)
|
100
|
+
Rapns::Daemon.start
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe Rapns::Daemon, "when being shutdown" do
|
105
|
+
let(:config) { double(:pid_file => '/rails_root/rapns.pid') }
|
106
|
+
let(:logger) { double(:info => nil, :error => nil, :warn => nil) }
|
107
|
+
|
108
|
+
before do
|
109
|
+
Rapns.stub(:config => config)
|
110
|
+
Rapns::Daemon.stub(:puts => nil)
|
111
|
+
Rapns::Daemon::Feeder.stub(:stop)
|
112
|
+
Rapns::Daemon::AppRunner.stub(:stop)
|
113
|
+
end
|
114
|
+
|
115
|
+
# These tests do not work on JRuby.
|
116
|
+
unless Rapns.jruby?
|
117
|
+
it "shuts down when signaled signaled SIGINT" do
|
118
|
+
Rapns::Daemon.setup_signal_traps
|
119
|
+
Rapns::Daemon.should_receive(:shutdown)
|
120
|
+
Process.kill("SIGINT", Process.pid)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "shuts down when signaled signaled SIGTERM" do
|
124
|
+
Rapns::Daemon.setup_signal_traps
|
125
|
+
Rapns::Daemon.should_receive(:shutdown)
|
126
|
+
Process.kill("SIGTERM", Process.pid)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
it "stops the feeder" do
|
131
|
+
Rapns::Daemon::Feeder.should_receive(:stop)
|
132
|
+
Rapns::Daemon.shutdown
|
133
|
+
end
|
134
|
+
|
135
|
+
it "stops the app runners" do
|
136
|
+
Rapns::Daemon::AppRunner.should_receive(:stop)
|
137
|
+
Rapns::Daemon.shutdown
|
138
|
+
end
|
139
|
+
|
140
|
+
it "removes the PID file if one was written" do
|
141
|
+
File.stub(:exists?).and_return(true)
|
142
|
+
File.should_receive(:delete).with("/rails_root/rapns.pid")
|
143
|
+
Rapns::Daemon.shutdown
|
144
|
+
end
|
145
|
+
|
146
|
+
it "does not attempt to remove the PID file if it does not exist" do
|
147
|
+
File.stub(:exists?).and_return(false)
|
148
|
+
File.should_not_receive(:delete)
|
149
|
+
Rapns::Daemon.shutdown
|
150
|
+
end
|
151
|
+
|
152
|
+
it "does not attempt to remove the PID file if one was not written" do
|
153
|
+
config.stub(:pid_file).and_return(nil)
|
154
|
+
File.should_not_receive(:delete)
|
155
|
+
Rapns::Daemon.shutdown
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path("spec/unit_spec_helper")
|
2
|
+
|
3
|
+
describe Rapns::Deprecatable do
|
4
|
+
class HasDeprecatedMethod
|
5
|
+
include Rapns::Deprecatable
|
6
|
+
|
7
|
+
def original_called?
|
8
|
+
@called == true
|
9
|
+
end
|
10
|
+
|
11
|
+
def deprecated_method
|
12
|
+
@called = true
|
13
|
+
end
|
14
|
+
deprecated(:deprecated_method, '4.0')
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:klass) { HasDeprecatedMethod.new }
|
18
|
+
|
19
|
+
before do
|
20
|
+
Rapns::Deprecation.stub(:warn)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'warns the method is deprecated when called' do
|
24
|
+
Rapns::Deprecation.should_receive(:warn).with("deprecated_method is deprecated and will be removed from Rapns 4.0.")
|
25
|
+
klass.deprecated_method
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'calls the original method' do
|
29
|
+
klass.deprecated_method
|
30
|
+
klass.original_called?.should be_true
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path("spec/unit_spec_helper")
|
2
|
+
|
3
|
+
describe Rapns::Deprecation do
|
4
|
+
it 'prints a warning' do
|
5
|
+
STDERR.should_receive(:puts).with("DEPRECATION WARNING: msg")
|
6
|
+
Rapns::Deprecation.warn("msg")
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'does not print a warning when muted' do
|
10
|
+
STDERR.should_not_receive(:puts)
|
11
|
+
Rapns::Deprecation.muted do
|
12
|
+
Rapns::Deprecation.warn("msg")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.expand_path("spec/unit_spec_helper")
|
2
|
+
|
3
|
+
describe Rapns, 'embed' do
|
4
|
+
before do
|
5
|
+
Rapns::Daemon.stub(:start)
|
6
|
+
Kernel.stub(:at_exit)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'sets the embedded config option to true' do
|
10
|
+
Rapns.embed
|
11
|
+
Rapns.config.embedded.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'starts the daemon' do
|
15
|
+
Rapns::Daemon.should_receive(:start)
|
16
|
+
Rapns.embed
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'overrides the default config options with those given as a hash' do
|
20
|
+
Rapns.config.push_poll = 4
|
21
|
+
expect { Rapns.embed(:push_poll => 2) }.to change(Rapns.config, :push_poll).to(2)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe Rapns, 'shutdown' do
|
26
|
+
before { Rapns.config.embedded = true }
|
27
|
+
|
28
|
+
it 'shuts down the daemon' do
|
29
|
+
Rapns::Daemon.should_receive(:shutdown)
|
30
|
+
Rapns.shutdown
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe Rapns, 'sync' do
|
35
|
+
before { Rapns.config.embedded = true }
|
36
|
+
|
37
|
+
it 'syncs the AppRunner' do
|
38
|
+
Rapns::Daemon::AppRunner.should_receive(:sync)
|
39
|
+
Rapns.sync
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe Rapns, 'debug' do
|
44
|
+
before { Rapns.config.embedded = true }
|
45
|
+
|
46
|
+
it 'debugs the AppRunner' do
|
47
|
+
Rapns::Daemon::AppRunner.should_receive(:debug)
|
48
|
+
Rapns.debug
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path("spec/unit_spec_helper")
|
2
|
+
require 'spec/unit/notification_shared.rb'
|
3
|
+
|
4
|
+
describe Rapns::Gcm::Notification do
|
5
|
+
it_should_behave_like 'an Notification subclass'
|
6
|
+
|
7
|
+
let(:app) { Rapns::Gcm::App.create!(:name => 'test', :auth_key => 'abc') }
|
8
|
+
let(:notification_class) { Rapns::Gcm::Notification }
|
9
|
+
let(:notification) { notification_class.new }
|
10
|
+
let(:data_setter) { 'data=' }
|
11
|
+
let(:data_getter) { 'data' }
|
12
|
+
|
13
|
+
it "has a 'data' payload limit of 4096 bytes" do
|
14
|
+
notification.data = { :key => "a" * 4096 }
|
15
|
+
notification.valid?.should be_false
|
16
|
+
notification.errors[:base].should == "GCM notification payload data cannot be larger than 4096 bytes."
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'allows assignment of many registration IDs' do
|
20
|
+
notification.app = app
|
21
|
+
notification.registration_ids = ['a', 'b']
|
22
|
+
notification.save!
|
23
|
+
reloaded_notification = notification_class.find(notification.id)
|
24
|
+
reloaded_notification.registration_ids.should == ['a', 'b']
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'num of registration Ids limit of 1000' do
|
28
|
+
notification.registration_ids = ['a']*(1000+1)
|
29
|
+
notification.valid?.should be_false
|
30
|
+
notification.errors[:base].should == "GCM notification number of registration_ids cannot be larger than 1000"
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'allows assignment of a single registration ID' do
|
34
|
+
notification.app = app
|
35
|
+
notification.registration_ids = 'a'
|
36
|
+
notification.save!
|
37
|
+
reloaded_notification = notification_class.find(notification.id)
|
38
|
+
reloaded_notification.registration_ids.should == ['a']
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'validates expiry is present if collapse_key is set' do
|
42
|
+
notification.collapse_key = 'test'
|
43
|
+
notification.expiry = nil
|
44
|
+
notification.valid?.should be_false
|
45
|
+
notification.errors[:expiry].should == 'must be set when using a collapse_key'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'includes time_to_live in the payload' do
|
49
|
+
notification.expiry = 100
|
50
|
+
notification.as_json['time_to_live'].should == 100
|
51
|
+
end
|
52
|
+
end
|