rpush_extended 3.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +365 -0
- data/LICENSE +7 -0
- data/README.md +393 -0
- data/bin/rpush +4 -0
- data/lib/generators/rpush_config_generator.rb +7 -0
- data/lib/generators/rpush_migration_generator.rb +66 -0
- data/lib/generators/templates/add_adm.rb +23 -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_fail_after_to_rpush_notifications.rb +9 -0
- data/lib/generators/templates/add_gcm.rb +117 -0
- data/lib/generators/templates/add_rpush.rb +402 -0
- data/lib/generators/templates/add_wpns.rb +16 -0
- data/lib/generators/templates/create_rapns_apps.rb +16 -0
- data/lib/generators/templates/create_rapns_feedback.rb +25 -0
- data/lib/generators/templates/create_rapns_notifications.rb +36 -0
- data/lib/generators/templates/rename_rapns_to_rpush.rb +87 -0
- data/lib/generators/templates/rpush.rb +135 -0
- data/lib/generators/templates/rpush_2_0_0_updates.rb +79 -0
- data/lib/generators/templates/rpush_2_1_0_updates.rb +11 -0
- data/lib/generators/templates/rpush_2_6_0_updates.rb +10 -0
- data/lib/generators/templates/rpush_2_7_0_updates.rb +12 -0
- data/lib/generators/templates/rpush_3_0_0_updates.rb +11 -0
- data/lib/generators/templates/rpush_3_0_1_updates.rb +13 -0
- data/lib/generators/templates/rpush_3_1_0_add_pushy.rb +9 -0
- data/lib/generators/templates/rpush_3_1_1_updates.rb +15 -0
- data/lib/generators/templates/rpush_3_2_0_add_apns_p8.rb +15 -0
- data/lib/generators/templates/rpush_3_2_4_updates.rb +9 -0
- data/lib/generators/templates/rpush_3_3_0_updates.rb +9 -0
- data/lib/generators/templates/rpush_3_3_1_updates.rb +11 -0
- data/lib/rpush/apns_feedback.rb +17 -0
- data/lib/rpush/cli.rb +213 -0
- data/lib/rpush/client/active_model/adm/app.rb +23 -0
- data/lib/rpush/client/active_model/adm/data_validator.rb +14 -0
- data/lib/rpush/client/active_model/adm/notification.rb +28 -0
- data/lib/rpush/client/active_model/apns/app.rb +37 -0
- data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +16 -0
- data/lib/rpush/client/active_model/apns/device_token_format_validator.rb +14 -0
- data/lib/rpush/client/active_model/apns/notification.rb +104 -0
- data/lib/rpush/client/active_model/apns2/app.rb +15 -0
- data/lib/rpush/client/active_model/apns2/notification.rb +9 -0
- data/lib/rpush/client/active_model/apnsp8/app.rb +23 -0
- data/lib/rpush/client/active_model/apnsp8/notification.rb +9 -0
- data/lib/rpush/client/active_model/gcm/app.rb +19 -0
- data/lib/rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +14 -0
- data/lib/rpush/client/active_model/gcm/notification.rb +59 -0
- data/lib/rpush/client/active_model/notification.rb +22 -0
- data/lib/rpush/client/active_model/payload_data_size_validator.rb +13 -0
- data/lib/rpush/client/active_model/pushy/app.rb +20 -0
- data/lib/rpush/client/active_model/pushy/notification.rb +31 -0
- data/lib/rpush/client/active_model/pushy/time_to_live_validator.rb +14 -0
- data/lib/rpush/client/active_model/registration_ids_count_validator.rb +13 -0
- data/lib/rpush/client/active_model/wns/app.rb +23 -0
- data/lib/rpush/client/active_model/wns/notification.rb +32 -0
- data/lib/rpush/client/active_model/wpns/app.rb +13 -0
- data/lib/rpush/client/active_model/wpns/notification.rb +28 -0
- data/lib/rpush/client/active_model.rb +34 -0
- data/lib/rpush/client/active_record/adm/app.rb +11 -0
- data/lib/rpush/client/active_record/adm/notification.rb +11 -0
- data/lib/rpush/client/active_record/apns/app.rb +11 -0
- data/lib/rpush/client/active_record/apns/feedback.rb +18 -0
- data/lib/rpush/client/active_record/apns/notification.rb +40 -0
- data/lib/rpush/client/active_record/apns2/app.rb +11 -0
- data/lib/rpush/client/active_record/apns2/notification.rb +10 -0
- data/lib/rpush/client/active_record/apnsp8/app.rb +11 -0
- data/lib/rpush/client/active_record/apnsp8/notification.rb +10 -0
- data/lib/rpush/client/active_record/app.rb +13 -0
- data/lib/rpush/client/active_record/gcm/app.rb +11 -0
- data/lib/rpush/client/active_record/gcm/notification.rb +11 -0
- data/lib/rpush/client/active_record/notification.rb +42 -0
- data/lib/rpush/client/active_record/pushy/app.rb +11 -0
- data/lib/rpush/client/active_record/pushy/notification.rb +11 -0
- data/lib/rpush/client/active_record/wns/app.rb +11 -0
- data/lib/rpush/client/active_record/wns/badge_notification.rb +15 -0
- data/lib/rpush/client/active_record/wns/notification.rb +11 -0
- data/lib/rpush/client/active_record/wns/raw_notification.rb +13 -0
- data/lib/rpush/client/active_record/wpns/app.rb +11 -0
- data/lib/rpush/client/active_record/wpns/notification.rb +11 -0
- data/lib/rpush/client/active_record.rb +33 -0
- data/lib/rpush/client/redis/adm/app.rb +14 -0
- data/lib/rpush/client/redis/adm/notification.rb +11 -0
- data/lib/rpush/client/redis/apns/app.rb +11 -0
- data/lib/rpush/client/redis/apns/feedback.rb +20 -0
- data/lib/rpush/client/redis/apns/notification.rb +11 -0
- data/lib/rpush/client/redis/apns2/app.rb +11 -0
- data/lib/rpush/client/redis/apns2/notification.rb +11 -0
- data/lib/rpush/client/redis/apnsp8/app.rb +11 -0
- data/lib/rpush/client/redis/apnsp8/notification.rb +11 -0
- data/lib/rpush/client/redis/app.rb +29 -0
- data/lib/rpush/client/redis/gcm/app.rb +11 -0
- data/lib/rpush/client/redis/gcm/notification.rb +11 -0
- data/lib/rpush/client/redis/notification.rb +74 -0
- data/lib/rpush/client/redis/pushy/app.rb +16 -0
- data/lib/rpush/client/redis/pushy/notification.rb +18 -0
- data/lib/rpush/client/redis/wns/app.rb +14 -0
- data/lib/rpush/client/redis/wns/badge_notification.rb +15 -0
- data/lib/rpush/client/redis/wns/notification.rb +11 -0
- data/lib/rpush/client/redis/wns/raw_notification.rb +11 -0
- data/lib/rpush/client/redis/wpns/app.rb +11 -0
- data/lib/rpush/client/redis/wpns/notification.rb +11 -0
- data/lib/rpush/client/redis.rb +56 -0
- data/lib/rpush/configuration.rb +115 -0
- data/lib/rpush/daemon/adm/delivery.rb +226 -0
- data/lib/rpush/daemon/adm.rb +9 -0
- data/lib/rpush/daemon/apns/delivery.rb +43 -0
- data/lib/rpush/daemon/apns/feedback_receiver.rb +90 -0
- data/lib/rpush/daemon/apns.rb +17 -0
- data/lib/rpush/daemon/apns2/delivery.rb +127 -0
- data/lib/rpush/daemon/apns2.rb +10 -0
- data/lib/rpush/daemon/apnsp8/delivery.rb +166 -0
- data/lib/rpush/daemon/apnsp8/token.rb +43 -0
- data/lib/rpush/daemon/apnsp8.rb +10 -0
- data/lib/rpush/daemon/app_runner.rb +190 -0
- data/lib/rpush/daemon/batch.rb +138 -0
- data/lib/rpush/daemon/constants.rb +59 -0
- data/lib/rpush/daemon/delivery.rb +46 -0
- data/lib/rpush/daemon/delivery_error.rb +27 -0
- data/lib/rpush/daemon/dispatcher/apns_http2.rb +51 -0
- data/lib/rpush/daemon/dispatcher/apns_tcp.rb +152 -0
- data/lib/rpush/daemon/dispatcher/apnsp8_http2.rb +33 -0
- data/lib/rpush/daemon/dispatcher/http.rb +21 -0
- data/lib/rpush/daemon/dispatcher/tcp.rb +22 -0
- data/lib/rpush/daemon/dispatcher_loop.rb +73 -0
- data/lib/rpush/daemon/errors.rb +18 -0
- data/lib/rpush/daemon/feeder.rb +69 -0
- data/lib/rpush/daemon/gcm/delivery.rb +241 -0
- data/lib/rpush/daemon/gcm.rb +9 -0
- data/lib/rpush/daemon/interruptible_sleep.rb +24 -0
- data/lib/rpush/daemon/loggable.rb +33 -0
- data/lib/rpush/daemon/proc_title.rb +17 -0
- data/lib/rpush/daemon/pushy/delivery.rb +90 -0
- data/lib/rpush/daemon/pushy.rb +9 -0
- data/lib/rpush/daemon/queue_payload.rb +12 -0
- data/lib/rpush/daemon/retry_header_parser.rb +23 -0
- data/lib/rpush/daemon/retryable_error.rb +22 -0
- data/lib/rpush/daemon/ring_buffer.rb +16 -0
- data/lib/rpush/daemon/rpc/client.rb +27 -0
- data/lib/rpush/daemon/rpc/server.rb +82 -0
- data/lib/rpush/daemon/rpc.rb +9 -0
- data/lib/rpush/daemon/service_config_methods.rb +51 -0
- data/lib/rpush/daemon/signal_handler.rb +75 -0
- data/lib/rpush/daemon/store/active_record/reconnectable.rb +80 -0
- data/lib/rpush/daemon/store/active_record.rb +214 -0
- data/lib/rpush/daemon/store/interface.rb +20 -0
- data/lib/rpush/daemon/store/redis.rb +166 -0
- data/lib/rpush/daemon/string_helpers.rb +15 -0
- data/lib/rpush/daemon/synchronizer.rb +62 -0
- data/lib/rpush/daemon/tcp_connection.rb +190 -0
- data/lib/rpush/daemon/wns/badge_request.rb +32 -0
- data/lib/rpush/daemon/wns/delivery.rb +178 -0
- data/lib/rpush/daemon/wns/post_request.rb +33 -0
- data/lib/rpush/daemon/wns/raw_request.rb +22 -0
- data/lib/rpush/daemon/wns/toast_request.rb +54 -0
- data/lib/rpush/daemon/wns.rb +9 -0
- data/lib/rpush/daemon/wpns/delivery.rb +132 -0
- data/lib/rpush/daemon/wpns.rb +9 -0
- data/lib/rpush/daemon.rb +179 -0
- data/lib/rpush/deprecatable.rb +24 -0
- data/lib/rpush/deprecation.rb +26 -0
- data/lib/rpush/embed.rb +41 -0
- data/lib/rpush/logger.rb +92 -0
- data/lib/rpush/multi_json_helper.rb +16 -0
- data/lib/rpush/plugin.rb +44 -0
- data/lib/rpush/push.rb +11 -0
- data/lib/rpush/reflectable.rb +13 -0
- data/lib/rpush/reflection_collection.rb +44 -0
- data/lib/rpush/reflection_public_methods.rb +11 -0
- data/lib/rpush/version.rb +14 -0
- data/lib/rpush.rb +43 -0
- data/lib/tasks/quality.rake +35 -0
- data/lib/tasks/test.rake +69 -0
- data/spec/.rubocop.yml +4 -0
- data/spec/functional/adm_spec.rb +50 -0
- data/spec/functional/apns2_spec.rb +232 -0
- data/spec/functional/apns_spec.rb +162 -0
- data/spec/functional/cli_spec.rb +36 -0
- data/spec/functional/embed_spec.rb +49 -0
- data/spec/functional/gcm_spec.rb +46 -0
- data/spec/functional/new_app_spec.rb +44 -0
- data/spec/functional/pushy_spec.rb +22 -0
- data/spec/functional/retry_spec.rb +42 -0
- data/spec/functional/synchronization_spec.rb +97 -0
- data/spec/functional/wpns_spec.rb +71 -0
- data/spec/functional_spec_helper.rb +32 -0
- data/spec/spec_helper.rb +69 -0
- data/spec/support/active_record_setup.rb +73 -0
- data/spec/support/cert_with_password.pem +90 -0
- data/spec/support/cert_without_password.pem +59 -0
- data/spec/support/config/database.yml +44 -0
- data/spec/support/simplecov_helper.rb +24 -0
- data/spec/support/simplecov_quality_formatter.rb +12 -0
- data/spec/tmp/.gitkeep +0 -0
- data/spec/unit/apns_feedback_spec.rb +28 -0
- data/spec/unit/client/active_record/adm/app_spec.rb +58 -0
- data/spec/unit/client/active_record/adm/notification_spec.rb +43 -0
- data/spec/unit/client/active_record/apns/app_spec.rb +29 -0
- data/spec/unit/client/active_record/apns/feedback_spec.rb +9 -0
- data/spec/unit/client/active_record/apns/notification_spec.rb +324 -0
- data/spec/unit/client/active_record/app_spec.rb +30 -0
- data/spec/unit/client/active_record/gcm/app_spec.rb +4 -0
- data/spec/unit/client/active_record/gcm/notification_spec.rb +67 -0
- data/spec/unit/client/active_record/notification_spec.rb +21 -0
- data/spec/unit/client/active_record/pushy/app_spec.rb +17 -0
- data/spec/unit/client/active_record/pushy/notification_spec.rb +65 -0
- data/spec/unit/client/active_record/wns/badge_notification_spec.rb +15 -0
- data/spec/unit/client/active_record/wns/raw_notification_spec.rb +26 -0
- data/spec/unit/client/active_record/wpns/app_spec.rb +4 -0
- data/spec/unit/client/active_record/wpns/notification_spec.rb +21 -0
- data/spec/unit/configuration_spec.rb +46 -0
- data/spec/unit/daemon/adm/delivery_spec.rb +253 -0
- data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +11 -0
- data/spec/unit/daemon/apns/delivery_spec.rb +108 -0
- data/spec/unit/daemon/apns/feedback_receiver_spec.rb +119 -0
- data/spec/unit/daemon/app_runner_spec.rb +188 -0
- data/spec/unit/daemon/batch_spec.rb +169 -0
- data/spec/unit/daemon/delivery_error_spec.rb +13 -0
- data/spec/unit/daemon/delivery_spec.rb +51 -0
- data/spec/unit/daemon/dispatcher/http_spec.rb +34 -0
- data/spec/unit/daemon/dispatcher/tcp_spec.rb +32 -0
- data/spec/unit/daemon/dispatcher_loop_spec.rb +53 -0
- data/spec/unit/daemon/feeder_spec.rb +96 -0
- data/spec/unit/daemon/gcm/delivery_spec.rb +387 -0
- data/spec/unit/daemon/proc_title_spec.rb +11 -0
- data/spec/unit/daemon/pushy/delivery_spec.rb +159 -0
- data/spec/unit/daemon/retryable_error_spec.rb +14 -0
- data/spec/unit/daemon/service_config_methods_spec.rb +36 -0
- data/spec/unit/daemon/signal_handler_spec.rb +99 -0
- data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +165 -0
- data/spec/unit/daemon/store/active_record_spec.rb +357 -0
- data/spec/unit/daemon/store/redis_spec.rb +365 -0
- data/spec/unit/daemon/tcp_connection_spec.rb +292 -0
- data/spec/unit/daemon/wns/delivery_spec.rb +176 -0
- data/spec/unit/daemon/wns/post_request_spec.rb +117 -0
- data/spec/unit/daemon/wpns/delivery_spec.rb +167 -0
- data/spec/unit/daemon_spec.rb +138 -0
- data/spec/unit/deprecatable_spec.rb +32 -0
- data/spec/unit/deprecation_spec.rb +15 -0
- data/spec/unit/embed_spec.rb +47 -0
- data/spec/unit/logger_spec.rb +127 -0
- data/spec/unit/notification_shared.rb +53 -0
- data/spec/unit/plugin_spec.rb +36 -0
- data/spec/unit/push_spec.rb +34 -0
- data/spec/unit/reflectable_spec.rb +27 -0
- data/spec/unit/reflection_collection_spec.rb +26 -0
- data/spec/unit/rpush_spec.rb +8 -0
- data/spec/unit_spec_helper.rb +26 -0
- metadata +709 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
require 'unit/notification_shared.rb'
|
3
|
+
|
4
|
+
describe Rpush::Client::ActiveRecord::Wpns::Notification do
|
5
|
+
it_should_behave_like 'an Notification subclass'
|
6
|
+
let(:app) { Rpush::Client::ActiveRecord::Wpns::App.create!(name: 'test', auth_key: 'abc') }
|
7
|
+
let(:notification_class) { Rpush::Client::ActiveRecord::Wpns::Notification }
|
8
|
+
let(:notification) { notification_class.new }
|
9
|
+
|
10
|
+
it "should have an url in the uri parameter" do
|
11
|
+
notification = Rpush::Client::ActiveRecord::Wpns::Notification.new(uri: "somthing")
|
12
|
+
notification.valid?
|
13
|
+
expect(notification.errors[:uri]).to include('is invalid')
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be invalid if there's no data" do
|
17
|
+
notification = Rpush::Client::ActiveRecord::Wpns::Notification.new(data: {})
|
18
|
+
notification.valid?
|
19
|
+
expect(notification.errors[:data]).to include("can't be blank")
|
20
|
+
end
|
21
|
+
end if active_record?
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
|
3
|
+
describe Rpush do
|
4
|
+
let(:config) { Rpush.config }
|
5
|
+
|
6
|
+
before do
|
7
|
+
allow(Rpush).to receive_messages(require: nil)
|
8
|
+
allow(Rpush).to receive_messages(config: config)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'yields a configure block' do
|
12
|
+
expect { |b| Rpush.configure(&b) }.to yield_with_args(config)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Rpush::Configuration do
|
17
|
+
let(:config) do
|
18
|
+
Rpush::Deprecation.muted do
|
19
|
+
Rpush::Configuration.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'can be updated' do
|
24
|
+
Rpush::Deprecation.muted do
|
25
|
+
new_config = Rpush::Configuration.new
|
26
|
+
new_config.batch_size = 200
|
27
|
+
expect { config.update(new_config) }.to change(config, :batch_size).to(200)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'sets the pid_file relative if not absolute' do
|
32
|
+
config.pid_file = 'tmp/rpush.pid'
|
33
|
+
expect(config.pid_file).to eq '/tmp/rails_root/tmp/rpush.pid'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'does not alter an absolute pid_file path' do
|
37
|
+
config.pid_file = '/tmp/rpush.pid'
|
38
|
+
expect(config.pid_file).to eq '/tmp/rpush.pid'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'delegate redis_options to Modis' do
|
42
|
+
Rpush.config.client = :redis
|
43
|
+
Rpush.config.redis_options = { hi: :mom }
|
44
|
+
expect(Modis.redis_options).to eq(hi: :mom)
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,253 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
|
3
|
+
describe Rpush::Daemon::Adm::Delivery do
|
4
|
+
let(:app) { Rpush::Adm::App.create!(name: 'MyApp', client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET') }
|
5
|
+
let(:notification) { Rpush::Adm::Notification.create!(app: app, registration_ids: ['xyz'], deliver_after: Time.now, data: { 'message' => 'test' }) }
|
6
|
+
let(:logger) { double(error: nil, info: nil, warn: nil) }
|
7
|
+
let(:response) { double(code: 200, header: {}) }
|
8
|
+
let(:http) { double(shutdown: nil, request: response) }
|
9
|
+
let(:now) { Time.parse('2012-10-14 00:00:00') }
|
10
|
+
let(:batch) { double(mark_failed: nil, mark_delivered: nil, mark_retryable: nil, notification_processed: nil) }
|
11
|
+
let(:delivery) { Rpush::Daemon::Adm::Delivery.new(app, http, notification, batch) }
|
12
|
+
let(:store) { double(create_adm_notification: double(id: 2)) }
|
13
|
+
|
14
|
+
def perform
|
15
|
+
delivery.perform
|
16
|
+
end
|
17
|
+
|
18
|
+
before do
|
19
|
+
app.access_token = 'ACCESS_TOKEN'
|
20
|
+
app.access_token_expiration = Time.now + 1.month
|
21
|
+
|
22
|
+
allow(delivery).to receive_messages(reflect: nil)
|
23
|
+
allow(Rpush::Daemon).to receive_messages(store: store)
|
24
|
+
allow(Time).to receive_messages(now: now)
|
25
|
+
allow(Rpush).to receive_messages(logger: logger)
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'unknown error response' do
|
29
|
+
before do
|
30
|
+
allow(response).to receive_messages(code: 408)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'marks the notification as failed because no successful delivery was made' do
|
34
|
+
allow(response).to receive_messages(body: JSON.dump('reason' => 'InvalidData'))
|
35
|
+
error = Rpush::DeliveryError.new(408, notification.id, 'Request Timeout')
|
36
|
+
expect(delivery).to receive(:mark_failed).with(error)
|
37
|
+
expect { perform }.to raise_error(Rpush::DeliveryError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'a 200 (Ok) response' do
|
42
|
+
before do
|
43
|
+
allow(response).to receive_messages(code: 200)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'marks the notification as delivered if delivered successfully to all devices' do
|
47
|
+
allow(response).to receive_messages(body: JSON.dump('registrationID' => 'xyz'))
|
48
|
+
expect(delivery).to receive(:mark_delivered)
|
49
|
+
perform
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'logs that the notification was delivered' do
|
53
|
+
allow(response).to receive_messages(body: JSON.dump('registrationID' => 'xyz'))
|
54
|
+
expect(logger).to receive(:info).with("[MyApp] #{notification.id} sent to xyz")
|
55
|
+
perform
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'reflects on canonical IDs' do
|
59
|
+
allow(response).to receive_messages(body: JSON.dump('registrationID' => 'canonical123'))
|
60
|
+
allow(notification).to receive_messages(registration_ids: ['1'])
|
61
|
+
expect(delivery).to receive(:reflect).with(:adm_canonical_id, '1', 'canonical123')
|
62
|
+
perform
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'a 400 (Bad Request) response' do
|
67
|
+
before do
|
68
|
+
allow(response).to receive_messages(code: 400)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'marks the notification as failed because no successful delivery was made' do
|
72
|
+
allow(response).to receive_messages(body: JSON.dump('reason' => 'InvalidData'))
|
73
|
+
error = Rpush::DeliveryError.new(nil, notification.id, 'Failed to deliver to all recipients.')
|
74
|
+
expect(delivery).to receive(:mark_failed).with(error)
|
75
|
+
expect { perform }.to raise_error(error)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'logs that the notification was not delivered' do
|
79
|
+
allow(response).to receive_messages(body: JSON.dump('reason' => 'InvalidRegistrationId'))
|
80
|
+
expect(logger).to receive(:warn).with("[MyApp] bad_request: xyz (InvalidRegistrationId)")
|
81
|
+
expect { perform }.to raise_error(Rpush::DeliveryError)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'reflects' do
|
85
|
+
allow(response).to receive_messages(body: JSON.dump('registrationID' => 'canonical123', 'reason' => 'Unregistered'))
|
86
|
+
allow(notification).to receive_messages(registration_ids: ['1'])
|
87
|
+
expect(delivery).to receive(:reflect).with(:adm_failed_to_recipient, notification, '1', 'Unregistered')
|
88
|
+
expect { perform }.to raise_error(Rpush::DeliveryError)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'a 401 (Unauthorized) response' do
|
93
|
+
let(:http) { double(shutdown: nil) }
|
94
|
+
let(:token_response) { double(code: 200, header: {}, body: JSON.dump('access_token' => 'ACCESS_TOKEN', 'expires_in' => 60)) }
|
95
|
+
|
96
|
+
before do
|
97
|
+
allow(response).to receive_messages(code: 401, header: { 'retry-after' => 10 })
|
98
|
+
|
99
|
+
# first request to deliver message that returns unauthorized response
|
100
|
+
adm_uri = URI.parse(format(Rpush::Daemon::Adm::Delivery::AMAZON_ADM_URL, notification.registration_ids.first))
|
101
|
+
expect(http).to receive(:request).with(adm_uri, instance_of(Net::HTTP::Post)).and_return(response)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should retrieve a new access token and mark the notification for retry' do
|
105
|
+
# request for access token
|
106
|
+
expect(http).to receive(:request).with(Rpush::Daemon::Adm::Delivery::AMAZON_TOKEN_URI, instance_of(Net::HTTP::Post)).and_return(token_response)
|
107
|
+
|
108
|
+
expect(store).to receive(:update_app).with(notification.app)
|
109
|
+
expect(delivery).to receive(:mark_retryable).with(notification, now)
|
110
|
+
|
111
|
+
perform
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should update the app with the new access token' do
|
115
|
+
# request for access token
|
116
|
+
expect(http).to receive(:request).with(Rpush::Daemon::Adm::Delivery::AMAZON_TOKEN_URI, instance_of(Net::HTTP::Post)).and_return(token_response)
|
117
|
+
|
118
|
+
expect(store).to receive(:update_app) do |app|
|
119
|
+
expect(app.access_token).to eq 'ACCESS_TOKEN'
|
120
|
+
expect(app.access_token_expiration).to eq now + 60.seconds
|
121
|
+
end
|
122
|
+
expect(delivery).to receive(:mark_retryable).with(notification, now)
|
123
|
+
|
124
|
+
perform
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should log the error and stop retrying if new access token can\'t be retrieved' do
|
128
|
+
allow(token_response).to receive_messages(code: 404, body: "test")
|
129
|
+
# request for access token
|
130
|
+
expect(http).to receive(:request).with(Rpush::Daemon::Adm::Delivery::AMAZON_TOKEN_URI, instance_of(Net::HTTP::Post)).and_return(token_response)
|
131
|
+
|
132
|
+
expect(store).not_to receive(:update_app).with(notification.app)
|
133
|
+
expect(delivery).not_to receive(:mark_retryable)
|
134
|
+
|
135
|
+
expect(logger).to receive(:warn).with("[MyApp] Could not retrieve access token from ADM: test")
|
136
|
+
|
137
|
+
perform
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe 'a 429 (Too Many Request) response' do
|
142
|
+
let(:http) { double(shutdown: nil) }
|
143
|
+
let(:notification) { Rpush::Adm::Notification.create!(app: app, registration_ids: %w(abc xyz), deliver_after: Time.now, collapse_key: 'sync', data: { 'message' => 'test' }) }
|
144
|
+
let(:rate_limited_response) { double(code: 429, header: { 'retry-after' => 3600 }) }
|
145
|
+
|
146
|
+
it 'should retry the entire notification respecting the Retry-After header if none sent out yet' do
|
147
|
+
allow(response).to receive_messages(code: 429, header: { 'retry-after' => 3600 })
|
148
|
+
|
149
|
+
# first request to deliver message that returns too many request response
|
150
|
+
adm_uri = URI.parse(format(Rpush::Daemon::Adm::Delivery::AMAZON_ADM_URL, notification.registration_ids.first))
|
151
|
+
expect(http).to receive(:request).with(adm_uri, instance_of(Net::HTTP::Post)).and_return(response)
|
152
|
+
|
153
|
+
expect(delivery).to receive(:mark_retryable).with(notification, now + 1.hour)
|
154
|
+
perform
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should retry the entire notification using exponential backoff' do
|
158
|
+
allow(response).to receive_messages(code: 429, header: {})
|
159
|
+
|
160
|
+
# first request to deliver message that returns too many request response
|
161
|
+
adm_uri = URI.parse(format(Rpush::Daemon::Adm::Delivery::AMAZON_ADM_URL, notification.registration_ids.first))
|
162
|
+
expect(http).to receive(:request).with(adm_uri, instance_of(Net::HTTP::Post)).and_return(response)
|
163
|
+
|
164
|
+
expect(delivery).to receive(:mark_retryable).with(notification, Time.now + 2**(notification.retries + 1))
|
165
|
+
perform
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'should keep sent reg ids in original notification and create new notification with remaining reg ids for retry' do
|
169
|
+
allow(response).to receive_messages(code: 200, body: JSON.dump('registrationID' => 'abc'))
|
170
|
+
|
171
|
+
# first request to deliver message succeeds
|
172
|
+
adm_uri = URI.parse(format(Rpush::Daemon::Adm::Delivery::AMAZON_ADM_URL, 'abc'))
|
173
|
+
expect(http).to receive(:request).with(adm_uri, instance_of(Net::HTTP::Post)).and_return(response)
|
174
|
+
|
175
|
+
# first request to deliver message that returns too many request response
|
176
|
+
adm_uri = URI.parse(format(Rpush::Daemon::Adm::Delivery::AMAZON_ADM_URL, 'xyz'))
|
177
|
+
expect(http).to receive(:request).with(adm_uri, instance_of(Net::HTTP::Post)).and_return(rate_limited_response)
|
178
|
+
|
179
|
+
expect(store).to receive(:update_notification) do |notif|
|
180
|
+
expect(notif.registration_ids).to include('abc')
|
181
|
+
expect(notif.registration_ids).to_not include('xyz')
|
182
|
+
end
|
183
|
+
|
184
|
+
expect(store).to receive(:create_adm_notification) do |attrs, _notification_data, reg_ids, deliver_after, notification_app|
|
185
|
+
expect(attrs.keys).to include('collapse_key')
|
186
|
+
expect(attrs.keys).to include('delay_while_idle')
|
187
|
+
expect(attrs.keys).to include('app_id')
|
188
|
+
|
189
|
+
expect(reg_ids).to eq ['xyz']
|
190
|
+
expect(deliver_after).to eq now + 1.hour
|
191
|
+
expect(notification_app).to eq notification.app
|
192
|
+
end
|
193
|
+
|
194
|
+
expect(delivery).to receive(:mark_delivered)
|
195
|
+
|
196
|
+
perform
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe 'a 500 (Internal Server Error) response' do
|
201
|
+
before do
|
202
|
+
allow(response).to receive_messages(code: 500)
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'marks the notification as failed because no successful delivery was made' do
|
206
|
+
error = Rpush::DeliveryError.new(nil, notification.id, 'Failed to deliver to all recipients.')
|
207
|
+
expect(delivery).to receive(:mark_failed).with(error)
|
208
|
+
expect { perform }.to raise_error(Rpush::DeliveryError)
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'logs that the notification was not delivered' do
|
212
|
+
expect(logger).to receive(:warn).with("[MyApp] internal_server_error: xyz (Internal Server Error)")
|
213
|
+
expect { perform }.to raise_error(Rpush::DeliveryError)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
describe 'a 503 (Service Unavailable) response' do
|
218
|
+
before do
|
219
|
+
allow(response).to receive_messages(code: 503, header: { 'retry-after' => 10 })
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'should retry the notification respecting the Retry-After header' do
|
223
|
+
expect(delivery).to receive(:mark_retryable).with(notification, now + 10.seconds)
|
224
|
+
perform
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
describe 'some registration ids succeeding and some failing' do
|
229
|
+
let(:http) { double(shutdown: nil) }
|
230
|
+
let(:notification) { Rpush::Adm::Notification.create!(app: app, registration_ids: %w(abc xyz), deliver_after: Time.now, collapse_key: 'sync', data: { 'message' => 'test' }) }
|
231
|
+
let(:bad_request_response) { double(code: 400, body: JSON.dump('reason' => 'InvalidData')) }
|
232
|
+
|
233
|
+
it 'should keep sent reg ids in original notification and create new notification with remaining reg ids for retry' do
|
234
|
+
allow(response).to receive_messages(code: 200, body: JSON.dump('registrationID' => 'abc'))
|
235
|
+
|
236
|
+
# first request to deliver message succeeds
|
237
|
+
adm_uri = URI.parse(format(Rpush::Daemon::Adm::Delivery::AMAZON_ADM_URL, 'abc'))
|
238
|
+
expect(http).to receive(:request).with(adm_uri, instance_of(Net::HTTP::Post)).and_return(response)
|
239
|
+
|
240
|
+
# first request to deliver message that returns too many request response
|
241
|
+
adm_uri = URI.parse(format(Rpush::Daemon::Adm::Delivery::AMAZON_ADM_URL, 'xyz'))
|
242
|
+
expect(http).to receive(:request).with(adm_uri, instance_of(Net::HTTP::Post)).and_return(bad_request_response)
|
243
|
+
|
244
|
+
expect(store).to receive(:update_notification) do |notif|
|
245
|
+
expect(notif.error_description).to eq "Failed to deliver to recipients: \nxyz: InvalidData"
|
246
|
+
end
|
247
|
+
|
248
|
+
expect(delivery).to receive(:mark_delivered)
|
249
|
+
|
250
|
+
perform
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
|
3
|
+
describe Rpush::Daemon::Apns::Delivery do
|
4
|
+
let(:app) { double(name: 'MyApp') }
|
5
|
+
let(:notification1) { double.as_null_object }
|
6
|
+
let(:notification2) { double.as_null_object }
|
7
|
+
let(:batch) { double(mark_all_failed: nil, mark_all_delivered: nil, all_processed: nil) }
|
8
|
+
let(:logger) { double(error: nil, info: nil) }
|
9
|
+
let(:connection) { double(select: false, write: nil, reconnect: nil, close: nil, connect: nil) }
|
10
|
+
let(:delivery) { Rpush::Daemon::Apns::Delivery.new(app, connection, batch) }
|
11
|
+
|
12
|
+
before do
|
13
|
+
allow(batch).to receive(:each_notification) do |&blk|
|
14
|
+
[notification1, notification2].each(&blk)
|
15
|
+
end
|
16
|
+
allow(Rpush).to receive_messages(logger: logger)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'writes the binary batch' do
|
20
|
+
allow(notification1).to receive_messages(to_binary: 'binary1')
|
21
|
+
allow(notification2).to receive_messages(to_binary: 'binary2')
|
22
|
+
expect(connection).to receive(:write).with('binary1binary2')
|
23
|
+
delivery.perform
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'logs the notification deliveries' do
|
27
|
+
allow(notification1).to receive_messages(id: 666, device_token: 'abc123')
|
28
|
+
allow(notification2).to receive_messages(id: 42, device_token: 'abc456')
|
29
|
+
expect(logger).to receive(:info).with('[MyApp] 666 sent to abc123')
|
30
|
+
expect(logger).to receive(:info).with('[MyApp] 42 sent to abc456')
|
31
|
+
delivery.perform
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'marks all notifications as delivered' do
|
35
|
+
expect(delivery).to receive(:mark_batch_delivered)
|
36
|
+
delivery.perform
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'notifies the batch all notifications have been processed' do
|
40
|
+
expect(batch).to receive(:all_processed)
|
41
|
+
delivery.perform
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'when an error is raised' do
|
45
|
+
it 'marks all notifications as failed' do
|
46
|
+
error = StandardError.new
|
47
|
+
allow(connection).to receive(:write).and_raise(error)
|
48
|
+
expect(delivery).to receive(:mark_batch_failed).with(error)
|
49
|
+
expect { delivery.perform }.to raise_error(error)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# describe "when delivery fails" do
|
54
|
+
# before { connection.stub(select: true, read: [8, 4, 69].pack("ccN")) }
|
55
|
+
#
|
56
|
+
# it "marks the notification as failed" do
|
57
|
+
# delivery.should_receive(:mark_failed).with(4, "Unable to deliver notification 69, received error 4 (Missing payload)")
|
58
|
+
# perform
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# it "logs the delivery error" do
|
62
|
+
# # checking for the doublebed error doesn't work in jruby, but checking
|
63
|
+
# # for the exception by class does.
|
64
|
+
#
|
65
|
+
# # error = Rpush::DeliveryError.new(4, 12, "Missing payload")
|
66
|
+
# # Rpush::DeliveryError.stub(new: error)
|
67
|
+
# # expect { delivery.perform }.to raise_error(error)
|
68
|
+
#
|
69
|
+
# expect { delivery.perform }.to raise_error(Rpush::DeliveryError)
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# it "reads 6 bytes from the socket" do
|
73
|
+
# connection.should_receive(:read).with(6).and_return(nil)
|
74
|
+
# perform
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# it "does not attempt to read from the socket if the socket was not selected for reading after the timeout" do
|
78
|
+
# connection.stub(select: nil)
|
79
|
+
# connection.should_not_receive(:read)
|
80
|
+
# perform
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# it "reconnects the socket" do
|
84
|
+
# connection.should_receive(:reconnect)
|
85
|
+
# perform
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# it "logs that the connection is being reconnected" do
|
89
|
+
# Rpush.logger.should_receive(:error).with("[MyApp] Error received, reconnecting...")
|
90
|
+
# perform
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# context "when the APNs disconnects without returning an error" do
|
94
|
+
# before do
|
95
|
+
# connection.stub(read: nil)
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# it 'raises a DisconnectError error if the connection is closed without an error being returned' do
|
99
|
+
# expect { delivery.perform }.to raise_error(Rpush::DisconnectionError)
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# it 'marks the notification as failed' do
|
103
|
+
# delivery.should_receive(:mark_failed).with(nil, "The APNs disconnected without returning an error. This may indicate you are using an invalid certificate for the host.")
|
104
|
+
# perform
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
# end
|
108
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'unit_spec_helper'
|
2
|
+
require 'rpush/daemon/store/active_record'
|
3
|
+
|
4
|
+
describe Rpush::Daemon::Apns::FeedbackReceiver, 'check_for_feedback' do
|
5
|
+
let(:host) { 'feedback.push.apple.com' }
|
6
|
+
let(:port) { 2196 }
|
7
|
+
let(:frequency) { 60 }
|
8
|
+
let(:certificate) { double }
|
9
|
+
let(:password) { double }
|
10
|
+
let(:app) { double(name: 'my_app', password: password, certificate: certificate, environment: 'production') }
|
11
|
+
let(:connection) { double(connect: nil, read: nil, close: nil) }
|
12
|
+
let(:logger) { double(error: nil, info: nil) }
|
13
|
+
let(:receiver) { Rpush::Daemon::Apns::FeedbackReceiver.new(app) }
|
14
|
+
let(:feedback) { double }
|
15
|
+
let(:sleeper) { double(Rpush::Daemon::InterruptibleSleep, sleep: nil, stop: nil) }
|
16
|
+
let(:store) { double(Rpush::Daemon::Store::ActiveRecord, create_apns_feedback: feedback, release_connection: nil) }
|
17
|
+
|
18
|
+
before do
|
19
|
+
Rpush.config.apns.feedback_receiver.frequency = frequency
|
20
|
+
allow(Rpush::Daemon::InterruptibleSleep).to receive_messages(new: sleeper)
|
21
|
+
allow(Rpush).to receive_messages(logger: logger)
|
22
|
+
allow(Rpush::Daemon::TcpConnection).to receive_messages(new: connection)
|
23
|
+
receiver.instance_variable_set("@stop", false)
|
24
|
+
allow(Rpush::Daemon).to receive_messages(store: store)
|
25
|
+
end
|
26
|
+
|
27
|
+
def double_connection_read_with_tuple
|
28
|
+
def connection.read(*)
|
29
|
+
unless @called
|
30
|
+
@called = true
|
31
|
+
"N\xE3\x84\r\x00 \x83OxfU\xEB\x9F\x84aJ\x05\xAD}\x00\xAF1\xE5\xCF\xE9:\xC3\xEA\a\x8F\x1D\xA4M*N\xB0\xCE\x17"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'initializes the sleeper with the feedback polling frequency' do
|
37
|
+
expect(Rpush::Daemon::InterruptibleSleep).to receive_messages(new: sleeper)
|
38
|
+
Rpush::Daemon::Apns::FeedbackReceiver.new(app)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'instantiates a new connection' do
|
42
|
+
expect(Rpush::Daemon::TcpConnection).to receive(:new).with(app, host, port)
|
43
|
+
receiver.check_for_feedback
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'connects to the feeback service' do
|
47
|
+
expect(connection).to receive(:connect)
|
48
|
+
receiver.check_for_feedback
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'closes the connection' do
|
52
|
+
expect(connection).to receive(:close)
|
53
|
+
receiver.check_for_feedback
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'reads from the connection' do
|
57
|
+
expect(connection).to receive(:read).with(38)
|
58
|
+
receiver.check_for_feedback
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'logs the feedback' do
|
62
|
+
double_connection_read_with_tuple
|
63
|
+
expect(Rpush.logger).to receive(:info).with("[my_app] [FeedbackReceiver] Delivery failed at 2011-12-10 16:08:45 UTC for 834f786655eb9f84614a05ad7d00af31e5cfe93ac3ea078f1da44d2a4eb0ce17.")
|
64
|
+
receiver.check_for_feedback
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'creates the feedback' do
|
68
|
+
expect(Rpush::Daemon.store).to receive(:create_apns_feedback).with(Time.at(1_323_533_325), '834f786655eb9f84614a05ad7d00af31e5cfe93ac3ea078f1da44d2a4eb0ce17', app)
|
69
|
+
double_connection_read_with_tuple
|
70
|
+
receiver.check_for_feedback
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'logs errors' do
|
74
|
+
error = StandardError.new('bork!')
|
75
|
+
allow(connection).to receive(:read).and_raise(error)
|
76
|
+
expect(Rpush.logger).to receive(:error).with(error)
|
77
|
+
receiver.check_for_feedback
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'start' do
|
81
|
+
before do
|
82
|
+
allow(Thread).to receive(:new).and_yield
|
83
|
+
allow(receiver).to receive(:loop).and_yield
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'sleeps' do
|
87
|
+
allow(receiver).to receive(:check_for_feedback)
|
88
|
+
expect(sleeper).to receive(:sleep).at_least(:once)
|
89
|
+
receiver.start
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'checks for feedback when started' do
|
93
|
+
expect(receiver).to receive(:check_for_feedback).at_least(:once)
|
94
|
+
receiver.start
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'stop' do
|
99
|
+
it 'interrupts sleep when stopped' do
|
100
|
+
allow(receiver).to receive(:check_for_feedback)
|
101
|
+
expect(sleeper).to receive(:stop)
|
102
|
+
receiver.stop
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'releases the store connection' do
|
106
|
+
allow(Thread).to receive(:new).and_yield
|
107
|
+
allow(receiver).to receive(:loop).and_yield
|
108
|
+
expect(Rpush::Daemon.store).to receive(:release_connection)
|
109
|
+
receiver.start
|
110
|
+
receiver.stop
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'reflects feedback was received' do
|
115
|
+
double_connection_read_with_tuple
|
116
|
+
expect(receiver).to receive(:reflect).with(:apns_feedback, feedback)
|
117
|
+
receiver.check_for_feedback
|
118
|
+
end
|
119
|
+
end
|