rpush 1.0.0-java → 2.0.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +37 -22
  4. data/bin/rpush +13 -4
  5. data/lib/generators/rpush_generator.rb +2 -0
  6. data/lib/generators/templates/add_adm.rb +5 -5
  7. data/lib/generators/templates/add_alert_is_json_to_rapns_notifications.rb +1 -1
  8. data/lib/generators/templates/add_app_to_rapns.rb +2 -2
  9. data/lib/generators/templates/add_fail_after_to_rpush_notifications.rb +1 -1
  10. data/lib/generators/templates/add_gcm.rb +32 -32
  11. data/lib/generators/templates/add_rpush.rb +67 -67
  12. data/lib/generators/templates/add_wpns.rb +2 -2
  13. data/lib/generators/templates/create_rapns_apps.rb +5 -5
  14. data/lib/generators/templates/create_rapns_feedback.rb +2 -2
  15. data/lib/generators/templates/create_rapns_notifications.rb +15 -15
  16. data/lib/generators/templates/rpush.rb +28 -7
  17. data/lib/generators/templates/rpush_2_0_0_updates.rb +42 -0
  18. data/lib/rpush/client/active_model/adm/app.rb +23 -0
  19. data/lib/rpush/client/active_model/adm/data_validator.rb +14 -0
  20. data/lib/rpush/client/active_model/adm/notification.rb +28 -0
  21. data/lib/rpush/client/active_model/apns/app.rb +37 -0
  22. data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +16 -0
  23. data/lib/rpush/client/active_model/apns/device_token_format_validator.rb +14 -0
  24. data/lib/rpush/client/active_model/apns/notification.rb +90 -0
  25. data/lib/rpush/client/active_model/gcm/app.rb +19 -0
  26. data/lib/rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +14 -0
  27. data/lib/rpush/client/active_model/gcm/notification.rb +31 -0
  28. data/lib/rpush/client/active_model/notification.rb +26 -0
  29. data/lib/rpush/client/active_model/payload_data_size_validator.rb +13 -0
  30. data/lib/rpush/client/active_model/registration_ids_count_validator.rb +13 -0
  31. data/lib/rpush/client/active_model/wpns/app.rb +13 -0
  32. data/lib/rpush/client/active_model/wpns/notification.rb +17 -0
  33. data/lib/rpush/client/active_model.rb +21 -0
  34. data/lib/rpush/client/active_record/adm/app.rb +11 -0
  35. data/lib/rpush/client/active_record/adm/notification.rb +11 -0
  36. data/lib/rpush/client/active_record/apns/app.rb +11 -0
  37. data/lib/rpush/client/active_record/apns/feedback.rb +22 -0
  38. data/lib/rpush/client/active_record/apns/notification.rb +46 -0
  39. data/lib/rpush/client/active_record/app.rb +17 -0
  40. data/lib/rpush/client/active_record/gcm/app.rb +11 -0
  41. data/lib/rpush/client/active_record/gcm/notification.rb +11 -0
  42. data/lib/rpush/client/active_record/notification.rb +38 -0
  43. data/lib/rpush/client/active_record/wpns/app.rb +11 -0
  44. data/lib/rpush/client/active_record/wpns/notification.rb +11 -0
  45. data/lib/rpush/client/active_record.rb +19 -0
  46. data/lib/rpush/client/redis/adm/app.rb +14 -0
  47. data/lib/rpush/client/redis/adm/notification.rb +11 -0
  48. data/lib/rpush/client/redis/apns/app.rb +11 -0
  49. data/lib/rpush/client/redis/apns/feedback.rb +20 -0
  50. data/lib/rpush/client/redis/apns/notification.rb +11 -0
  51. data/lib/rpush/client/redis/app.rb +24 -0
  52. data/lib/rpush/client/redis/gcm/app.rb +11 -0
  53. data/lib/rpush/client/redis/gcm/notification.rb +11 -0
  54. data/lib/rpush/client/redis/notification.rb +68 -0
  55. data/lib/rpush/client/redis/wpns/app.rb +11 -0
  56. data/lib/rpush/client/redis/wpns/notification.rb +11 -0
  57. data/lib/rpush/client/redis.rb +35 -0
  58. data/lib/rpush/configuration.rb +27 -6
  59. data/lib/rpush/daemon/adm/delivery.rb +56 -55
  60. data/lib/rpush/daemon/apns/delivery.rb +20 -44
  61. data/lib/rpush/daemon/apns/feedback_receiver.rb +11 -8
  62. data/lib/rpush/daemon/apns.rb +6 -5
  63. data/lib/rpush/daemon/app_runner.rb +103 -99
  64. data/lib/rpush/daemon/batch.rb +54 -40
  65. data/lib/rpush/daemon/delivery.rb +13 -3
  66. data/lib/rpush/daemon/delivery_error.rb +10 -2
  67. data/lib/rpush/daemon/dispatcher/apns_tcp.rb +114 -0
  68. data/lib/rpush/daemon/dispatcher/http.rb +3 -3
  69. data/lib/rpush/daemon/dispatcher/tcp.rb +3 -3
  70. data/lib/rpush/daemon/dispatcher_loop.rb +37 -23
  71. data/lib/rpush/daemon/errors.rb +18 -0
  72. data/lib/rpush/daemon/feeder.rb +28 -39
  73. data/lib/rpush/daemon/gcm/delivery.rb +19 -20
  74. data/lib/rpush/daemon/interruptible_sleep.rb +26 -45
  75. data/lib/rpush/daemon/loggable.rb +2 -4
  76. data/lib/rpush/daemon/proc_title.rb +16 -0
  77. data/lib/rpush/daemon/queue_payload.rb +12 -0
  78. data/lib/rpush/daemon/reflectable.rb +3 -5
  79. data/lib/rpush/daemon/retry_header_parser.rb +6 -6
  80. data/lib/rpush/daemon/retryable_error.rb +2 -0
  81. data/lib/rpush/daemon/ring_buffer.rb +16 -0
  82. data/lib/rpush/daemon/service_config_methods.rb +23 -7
  83. data/lib/rpush/daemon/signal_handler.rb +56 -0
  84. data/lib/rpush/daemon/store/active_record/reconnectable.rb +21 -17
  85. data/lib/rpush/daemon/store/active_record.rb +71 -38
  86. data/lib/rpush/daemon/store/interface.rb +19 -0
  87. data/lib/rpush/daemon/store/redis.rb +149 -0
  88. data/lib/rpush/daemon/string_helpers.rb +15 -0
  89. data/lib/rpush/daemon/synchronizer.rb +60 -0
  90. data/lib/rpush/daemon/tcp_connection.rb +6 -11
  91. data/lib/rpush/daemon/wpns/delivery.rb +21 -30
  92. data/lib/rpush/daemon.rb +40 -60
  93. data/lib/rpush/deprecatable.rb +4 -3
  94. data/lib/rpush/deprecation.rb +7 -10
  95. data/lib/rpush/embed.rb +8 -3
  96. data/lib/rpush/logger.rb +11 -15
  97. data/lib/rpush/push.rb +1 -2
  98. data/lib/rpush/reflection.rb +8 -12
  99. data/lib/rpush/version.rb +1 -1
  100. data/lib/rpush.rb +5 -29
  101. data/lib/tasks/quality.rake +35 -0
  102. data/lib/tasks/test.rake +1 -5
  103. data/spec/.rubocop.yml +4 -0
  104. data/spec/functional/adm_spec.rb +3 -6
  105. data/spec/functional/apns_spec.rb +117 -24
  106. data/spec/functional/embed_spec.rb +20 -20
  107. data/spec/functional/gcm_spec.rb +4 -7
  108. data/spec/functional/new_app_spec.rb +59 -0
  109. data/spec/functional/retry_spec.rb +46 -0
  110. data/spec/functional/synchronization_spec.rb +68 -0
  111. data/spec/functional/wpns_spec.rb +3 -6
  112. data/spec/functional_spec_helper.rb +26 -0
  113. data/spec/integration/rpush_spec.rb +13 -0
  114. data/spec/integration/support/gcm_success_response.json +1 -0
  115. data/spec/spec_helper.rb +60 -0
  116. data/spec/support/active_record_setup.rb +48 -0
  117. data/{config → spec/support/config}/database.yml +0 -0
  118. data/spec/support/install.sh +43 -7
  119. data/spec/support/simplecov_helper.rb +9 -5
  120. data/spec/support/simplecov_quality_formatter.rb +10 -6
  121. data/spec/unit/apns_feedback_spec.rb +3 -3
  122. data/spec/unit/{adm → client/active_record/adm}/app_spec.rb +3 -3
  123. data/spec/unit/{adm → client/active_record/adm}/notification_spec.rb +5 -7
  124. data/spec/unit/client/active_record/apns/app_spec.rb +29 -0
  125. data/spec/unit/client/active_record/apns/feedback_spec.rb +9 -0
  126. data/spec/unit/client/active_record/apns/notification_spec.rb +231 -0
  127. data/spec/unit/client/active_record/app_spec.rb +30 -0
  128. data/spec/unit/client/active_record/gcm/app_spec.rb +4 -0
  129. data/spec/unit/{gcm → client/active_record/gcm}/notification_spec.rb +5 -7
  130. data/spec/unit/client/active_record/notification_spec.rb +21 -0
  131. data/spec/unit/client/active_record/wpns/app_spec.rb +4 -0
  132. data/spec/unit/client/active_record/wpns/notification_spec.rb +21 -0
  133. data/spec/unit/configuration_spec.rb +12 -5
  134. data/spec/unit/daemon/adm/delivery_spec.rb +66 -55
  135. data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +3 -3
  136. data/spec/unit/daemon/apns/delivery_spec.rb +90 -83
  137. data/spec/unit/daemon/apns/feedback_receiver_spec.rb +22 -17
  138. data/spec/unit/daemon/app_runner_spec.rb +78 -186
  139. data/spec/unit/daemon/batch_spec.rb +52 -115
  140. data/spec/unit/daemon/delivery_spec.rb +15 -1
  141. data/spec/unit/daemon/dispatcher/http_spec.rb +3 -2
  142. data/spec/unit/daemon/dispatcher/tcp_spec.rb +10 -9
  143. data/spec/unit/daemon/dispatcher_loop_spec.rb +6 -24
  144. data/spec/unit/daemon/feeder_spec.rb +38 -39
  145. data/spec/unit/daemon/gcm/delivery_spec.rb +122 -101
  146. data/spec/unit/daemon/reflectable_spec.rb +2 -2
  147. data/spec/unit/daemon/retryable_error_spec.rb +1 -1
  148. data/spec/unit/daemon/service_config_methods_spec.rb +6 -3
  149. data/spec/unit/daemon/signal_handler_spec.rb +95 -0
  150. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +48 -27
  151. data/spec/unit/daemon/store/active_record_spec.rb +38 -47
  152. data/spec/unit/daemon/tcp_connection_spec.rb +22 -34
  153. data/spec/unit/daemon/wpns/delivery_spec.rb +58 -50
  154. data/spec/unit/daemon_spec.rb +48 -82
  155. data/spec/unit/embed_spec.rb +6 -4
  156. data/spec/unit/logger_spec.rb +35 -43
  157. data/spec/unit/notification_shared.rb +9 -79
  158. data/spec/unit/push_spec.rb +6 -10
  159. data/spec/unit/reflection_spec.rb +25 -25
  160. data/spec/unit/rpush_spec.rb +1 -2
  161. data/spec/unit_spec_helper.rb +33 -88
  162. metadata +126 -76
  163. data/lib/rpush/TODO +0 -3
  164. data/lib/rpush/adm/app.rb +0 -15
  165. data/lib/rpush/adm/data_validator.rb +0 -11
  166. data/lib/rpush/adm/notification.rb +0 -29
  167. data/lib/rpush/apns/app.rb +0 -29
  168. data/lib/rpush/apns/binary_notification_validator.rb +0 -12
  169. data/lib/rpush/apns/device_token_format_validator.rb +0 -12
  170. data/lib/rpush/apns/feedback.rb +0 -16
  171. data/lib/rpush/apns/notification.rb +0 -84
  172. data/lib/rpush/app.rb +0 -18
  173. data/lib/rpush/daemon/apns/certificate_expired_error.rb +0 -20
  174. data/lib/rpush/daemon/apns/disconnection_error.rb +0 -20
  175. data/lib/rpush/daemon/dispatcher_loop_collection.rb +0 -33
  176. data/lib/rpush/daemon/too_many_requests_error.rb +0 -20
  177. data/lib/rpush/gcm/app.rb +0 -11
  178. data/lib/rpush/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +0 -11
  179. data/lib/rpush/gcm/notification.rb +0 -30
  180. data/lib/rpush/notification.rb +0 -69
  181. data/lib/rpush/notifier.rb +0 -52
  182. data/lib/rpush/payload_data_size_validator.rb +0 -10
  183. data/lib/rpush/railtie.rb +0 -11
  184. data/lib/rpush/registration_ids_count_validator.rb +0 -10
  185. data/lib/rpush/wpns/app.rb +0 -9
  186. data/lib/rpush/wpns/notification.rb +0 -26
  187. data/lib/tasks/cane.rake +0 -18
  188. data/lib/tasks/rpush.rake +0 -16
  189. data/spec/unit/apns/app_spec.rb +0 -29
  190. data/spec/unit/apns/feedback_spec.rb +0 -9
  191. data/spec/unit/apns/notification_spec.rb +0 -208
  192. data/spec/unit/app_spec.rb +0 -30
  193. data/spec/unit/daemon/apns/disconnection_error_spec.rb +0 -18
  194. data/spec/unit/daemon/dispatcher_loop_collection_spec.rb +0 -37
  195. data/spec/unit/daemon/interruptible_sleep_spec.rb +0 -68
  196. data/spec/unit/daemon/too_many_requests_error_spec.rb +0 -14
  197. data/spec/unit/gcm/app_spec.rb +0 -4
  198. data/spec/unit/notification_spec.rb +0 -15
  199. data/spec/unit/notifier_spec.rb +0 -49
  200. data/spec/unit/wpns/app_spec.rb +0 -4
  201. data/spec/unit/wpns/notification_spec.rb +0 -30
@@ -1,23 +1,15 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush::Daemon::Batch do
4
- let(:notification1) { double(:notification1, :id => 1) }
5
- let(:notification2) { double(:notification2, :id => 2) }
4
+ let(:notification1) { double(:notification1, id: 1) }
5
+ let(:notification2) { double(:notification2, id: 2) }
6
6
  let(:batch) { Rpush::Daemon::Batch.new([notification1, notification2]) }
7
7
  let(:store) { double.as_null_object }
8
8
  let(:time) { Time.now }
9
9
 
10
10
  before do
11
- Time.stub(:now => time)
12
- Rpush::Daemon.stub(:store => store)
13
- end
14
-
15
- it 'exposes the notifications' do
16
- batch.notifications.should eq [notification1, notification2]
17
- end
18
-
19
- it 'exposes the number notifications' do
20
- batch.num_notifications.should eq 2
11
+ Time.stub(now: time)
12
+ Rpush::Daemon.stub(store: store)
21
13
  end
22
14
 
23
15
  it 'exposes the number notifications processed' do
@@ -25,125 +17,85 @@ describe Rpush::Daemon::Batch do
25
17
  end
26
18
 
27
19
  it 'increments the processed notifications count' do
28
- expect { batch.notification_dispatched }.to change(batch, :num_processed).to(1)
20
+ expect { batch.notification_processed }.to change(batch, :num_processed).to(1)
29
21
  end
30
22
 
31
23
  it 'completes the batch when all notifications have been processed' do
32
24
  batch.should_receive(:complete)
33
- 2.times { batch.notification_dispatched }
34
- end
35
-
36
- it 'can be described' do
37
- batch.describe.should eq '1, 2'
25
+ 2.times { batch.notification_processed }
38
26
  end
39
27
 
40
28
  describe 'mark_delivered' do
41
- describe 'batching is disabled' do
42
- before { Rpush.config.batch_storage_updates = false }
43
-
44
- it 'marks the notification as delivered immediately' do
45
- store.should_receive(:mark_delivered).with(notification1, time)
46
- batch.mark_delivered(notification1)
47
- end
48
-
49
- it 'reflects the notification was delivered' do
50
- batch.should_receive(:reflect).with(:notification_delivered, notification1)
51
- batch.mark_delivered(notification1)
52
- end
29
+ it 'marks the notification as delivered immediately without persisting' do
30
+ store.should_receive(:mark_delivered).with(notification1, time, persist: false)
31
+ batch.mark_delivered(notification1)
53
32
  end
54
33
 
55
- describe 'batching is enabled' do
56
- before { Rpush.config.batch_storage_updates = true }
34
+ it 'defers persisting' do
35
+ batch.mark_delivered(notification1)
36
+ batch.delivered.should eq [notification1]
37
+ end
38
+ end
57
39
 
58
- it 'marks the notification as delivered immediately without persisting' do
59
- store.should_receive(:mark_delivered).with(notification1, time, :persist => false)
60
- batch.mark_delivered(notification1)
61
- end
40
+ describe 'mark_all_delivered' do
41
+ it 'marks the notifications as delivered immediately without persisting' do
42
+ store.should_receive(:mark_delivered).with(notification1, time, persist: false)
43
+ store.should_receive(:mark_delivered).with(notification2, time, persist: false)
44
+ batch.mark_all_delivered
45
+ end
62
46
 
63
- it 'defers persisting' do
64
- batch.mark_delivered(notification1)
65
- batch.delivered.should eq [notification1]
66
- end
47
+ it 'defers persisting' do
48
+ batch.mark_all_delivered
49
+ batch.delivered.should eq [notification1, notification2]
67
50
  end
68
51
  end
69
52
 
70
53
  describe 'mark_failed' do
71
- describe 'batching is disabled' do
72
- before { Rpush.config.batch_storage_updates = false }
73
-
74
- it 'marks the notification as failed' do
75
- store.should_receive(:mark_failed).with(notification1, 1, 'an error', time)
76
- batch.mark_failed(notification1, 1, 'an error')
77
- end
78
-
79
- it 'reflects the notification failed' do
80
- batch.should_receive(:reflect).with(:notification_delivered, notification1)
81
- batch.mark_delivered(notification1)
82
- end
54
+ it 'marks the notification as failed without persisting' do
55
+ store.should_receive(:mark_failed).with(notification1, 1, 'an error', time, persist: false)
56
+ batch.mark_failed(notification1, 1, 'an error')
83
57
  end
84
58
 
85
- describe 'batching is enabled' do
86
- before { Rpush.config.batch_storage_updates = true }
59
+ it 'defers persisting' do
60
+ batch.mark_failed(notification1, 1, 'an error')
61
+ batch.failed.should eq([1, 'an error'] => [notification1])
62
+ end
63
+ end
87
64
 
88
- it 'marks the notification as failed without persisting' do
89
- store.should_receive(:mark_failed).with(notification1, 1, 'an error', time, :persist => false)
90
- batch.mark_failed(notification1, 1, 'an error')
91
- end
65
+ describe 'mark_failed' do
66
+ it 'marks the notification as failed without persisting' do
67
+ store.should_receive(:mark_failed).with(notification1, 1, 'an error', time, persist: false)
68
+ store.should_receive(:mark_failed).with(notification2, 1, 'an error', time, persist: false)
69
+ batch.mark_all_failed(1, 'an error')
70
+ end
92
71
 
93
- it 'defers persisting' do
94
- Rpush.config.batch_storage_updates = true
95
- batch.mark_failed(notification1, 1, 'an error')
96
- batch.failed.should eq({[1, 'an error'] => [notification1]})
97
- end
72
+ it 'defers persisting' do
73
+ batch.mark_all_failed(1, 'an error')
74
+ batch.failed.should eq([1, 'an error'] => [notification1, notification2])
98
75
  end
99
76
  end
100
77
 
101
78
  describe 'mark_retryable' do
102
- describe 'batching is disabled' do
103
- before { Rpush.config.batch_storage_updates = false }
104
-
105
- it 'marks the notification as retryable' do
106
- store.should_receive(:mark_retryable).with(notification1, time)
107
- batch.mark_retryable(notification1, time)
108
- end
109
-
110
- it 'reflects the notification will be retried' do
111
- batch.should_receive(:reflect).with(:notification_will_retry, notification1)
112
- batch.mark_retryable(notification1, time)
113
- end
79
+ it 'marks the notification as retryable without persisting' do
80
+ store.should_receive(:mark_retryable).with(notification1, time, persist: false)
81
+ batch.mark_retryable(notification1, time)
114
82
  end
115
83
 
116
- describe 'batching is enabled' do
117
- before { Rpush.config.batch_storage_updates = true }
118
-
119
- it 'marks the notification as retryable without persisting' do
120
- store.should_receive(:mark_retryable).with(notification1, time, :persist => false)
121
- batch.mark_retryable(notification1, time)
122
- end
123
-
124
- it 'defers persisting' do
125
- batch.mark_retryable(notification1, time)
126
- batch.retryable.should eq({time => [notification1]})
127
- end
84
+ it 'defers persisting' do
85
+ batch.mark_retryable(notification1, time)
86
+ batch.retryable.should eq(time => [notification1])
128
87
  end
129
88
  end
130
89
 
131
90
  describe 'complete' do
132
91
  before do
133
- Rpush.config.batch_storage_updates = true
134
- Rpush.stub(:logger => double.as_null_object)
92
+ Rpush.stub(logger: double.as_null_object)
135
93
  batch.stub(:reflect)
136
94
  end
137
95
 
138
- it 'clears the notifications' do
139
- expect do
140
- 2.times { batch.notification_dispatched }
141
- end.to change(batch, :notifications).to([])
142
- end
143
-
144
96
  it 'identifies as complete' do
145
97
  expect do
146
- 2.times { batch.notification_dispatched }
98
+ 2.times { batch.notification_processed }
147
99
  end.to change(batch, :complete?).to(be_true)
148
100
  end
149
101
 
@@ -151,14 +103,14 @@ describe Rpush::Daemon::Batch do
151
103
  e = StandardError.new
152
104
  batch.stub(:complete_delivered).and_raise(e)
153
105
  batch.should_receive(:reflect).with(:error, e)
154
- 2.times { batch.notification_dispatched }
106
+ 2.times { batch.notification_processed }
155
107
  end
156
108
 
157
109
  describe 'delivered' do
158
110
  def complete
159
111
  [notification1, notification2].each do |n|
160
112
  batch.mark_delivered(n)
161
- batch.notification_dispatched
113
+ batch.notification_processed
162
114
  end
163
115
  end
164
116
 
@@ -172,18 +124,13 @@ describe Rpush::Daemon::Batch do
172
124
  batch.should_receive(:reflect).with(:notification_delivered, notification2)
173
125
  complete
174
126
  end
175
-
176
- it 'clears the delivered notifications' do
177
- complete
178
- batch.delivered.should eq([])
179
- end
180
127
  end
181
128
 
182
129
  describe 'failed' do
183
130
  def complete
184
131
  [notification1, notification2].each do |n|
185
132
  batch.mark_failed(n, 1, 'an error')
186
- batch.notification_dispatched
133
+ batch.notification_processed
187
134
  end
188
135
  end
189
136
 
@@ -197,18 +144,13 @@ describe Rpush::Daemon::Batch do
197
144
  batch.should_receive(:reflect).with(:notification_failed, notification2)
198
145
  complete
199
146
  end
200
-
201
- it 'clears the failed notifications' do
202
- complete
203
- batch.failed.should eq({})
204
- end
205
147
  end
206
148
 
207
149
  describe 'retryable' do
208
150
  def complete
209
151
  [notification1, notification2].each do |n|
210
152
  batch.mark_retryable(n, time)
211
- batch.notification_dispatched
153
+ batch.notification_processed
212
154
  end
213
155
  end
214
156
 
@@ -222,11 +164,6 @@ describe Rpush::Daemon::Batch do
222
164
  batch.should_receive(:reflect).with(:notification_will_retry, notification2)
223
165
  complete
224
166
  end
225
-
226
- it 'clears the retryable notifications' do
227
- complete
228
- batch.retryable.should eq({})
229
- end
230
167
  end
231
168
  end
232
169
  end
@@ -16,7 +16,6 @@ describe Rpush::Daemon::Delivery do
16
16
  before { Time.stub(now: now) }
17
17
 
18
18
  describe 'mark_retryable' do
19
-
20
19
  it 'does not retry a notification with an expired fail_after' do
21
20
  batch.should_receive(:mark_failed).with(notification, nil, "Notification failed to be delivered before 2014-10-13 23:00:00.")
22
21
  notification.fail_after = Time.now - 1.hour
@@ -35,4 +34,19 @@ describe Rpush::Daemon::Delivery do
35
34
  delivery.mark_retryable(notification, Time.now + 1.hour)
36
35
  end
37
36
  end
37
+
38
+ describe 'mark_batch_delivered' do
39
+ it 'marks all notifications as delivered' do
40
+ batch.should_receive(:mark_all_delivered)
41
+ delivery.mark_batch_delivered
42
+ end
43
+ end
44
+
45
+ describe 'mark_batch_failed' do
46
+ it 'marks all notifications as delivered' do
47
+ error = Rpush::DeliveryError.new(1, 42, 'an error')
48
+ batch.should_receive(:mark_all_failed).with(1, 'Unable to deliver notification 42, received error 1 (an error)')
49
+ delivery.mark_batch_failed(error)
50
+ end
51
+ end
38
52
  end
@@ -6,9 +6,10 @@ describe Rpush::Daemon::Dispatcher::Http do
6
6
  let(:notification) { double }
7
7
  let(:batch) { double }
8
8
  let(:http) { double }
9
+ let(:queue_payload) { Rpush::Daemon::QueuePayload.new(batch, notification) }
9
10
  let(:dispatcher) { Rpush::Daemon::Dispatcher::Http.new(app, delivery_class) }
10
11
 
11
- before { Net::HTTP::Persistent.stub(:new => http) }
12
+ before { Net::HTTP::Persistent.stub(new: http) }
12
13
 
13
14
  it 'constructs a new persistent connection' do
14
15
  Net::HTTP::Persistent.should_receive(:new)
@@ -20,7 +21,7 @@ describe Rpush::Daemon::Dispatcher::Http do
20
21
  delivery = double
21
22
  delivery_class.should_receive(:new).with(app, http, notification, batch).and_return(delivery)
22
23
  delivery.should_receive(:perform)
23
- dispatcher.dispatch(notification, batch)
24
+ dispatcher.dispatch(queue_payload)
24
25
  end
25
26
  end
26
27
 
@@ -2,35 +2,36 @@ require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush::Daemon::Dispatcher::Tcp do
4
4
  let(:app) { double }
5
- let(:delivery) { double(:perform => nil) }
6
- let(:delivery_class) { double(:new => delivery) }
5
+ let(:delivery) { double(perform: nil) }
6
+ let(:delivery_class) { double(new: delivery) }
7
7
  let(:notification) { double }
8
8
  let(:batch) { double }
9
- let(:connection) { double(Rpush::Daemon::TcpConnection, :connect => nil) }
9
+ let(:connection) { double(Rpush::Daemon::TcpConnection, connect: nil) }
10
10
  let(:host) { 'localhost' }
11
11
  let(:port) { 1234 }
12
- let(:host_proc) { Proc.new { |app| [host, port] } }
13
- let(:dispatcher) { Rpush::Daemon::Dispatcher::Tcp.new(app, delivery_class, :host => host_proc) }
12
+ let(:host_proc) { proc { [host, port] } }
13
+ let(:queue_payload) { Rpush::Daemon::QueuePayload.new(batch, notification) }
14
+ let(:dispatcher) { Rpush::Daemon::Dispatcher::Tcp.new(app, delivery_class, host: host_proc) }
14
15
 
15
- before { Rpush::Daemon::TcpConnection.stub(:new => connection) }
16
+ before { Rpush::Daemon::TcpConnection.stub(new: connection) }
16
17
 
17
18
  describe 'dispatch' do
18
19
  it 'lazily connects the socket' do
19
20
  Rpush::Daemon::TcpConnection.should_receive(:new).with(app, host, port).and_return(connection)
20
21
  connection.should_receive(:connect)
21
- dispatcher.dispatch(notification, batch)
22
+ dispatcher.dispatch(queue_payload)
22
23
  end
23
24
 
24
25
  it 'delivers the notification' do
25
26
  delivery_class.should_receive(:new).with(app, connection, notification, batch).and_return(delivery)
26
27
  delivery.should_receive(:perform)
27
- dispatcher.dispatch(notification, batch)
28
+ dispatcher.dispatch(queue_payload)
28
29
  end
29
30
  end
30
31
 
31
32
  describe 'cleanup' do
32
33
  it 'closes the connection' do
33
- dispatcher.dispatch(notification, batch) # lazily initialize connection
34
+ dispatcher.dispatch(queue_payload) # lazily initialize connection
34
35
  connection.should_receive(:close)
35
36
  dispatcher.cleanup
36
37
  end
@@ -4,25 +4,23 @@ describe Rpush::Daemon::DispatcherLoop do
4
4
  def run_dispatcher_loop
5
5
  dispatcher_loop.start
6
6
  dispatcher_loop.stop
7
- dispatcher_loop.wakeup
8
- dispatcher_loop.wait
9
7
  end
10
8
 
11
9
  let(:notification) { double }
12
- let(:batch) { double(:notification_dispatched => nil) }
10
+ let(:batch) { double(notification_processed: nil) }
13
11
  let(:queue) { Queue.new }
14
- let(:dispatcher) { double(:dispatch => nil, :cleanup => nil) }
12
+ let(:dispatcher) { double(dispatch: nil, cleanup: nil) }
15
13
  let(:dispatcher_loop) { Rpush::Daemon::DispatcherLoop.new(queue, dispatcher) }
16
- let(:store) { double(Rpush::Daemon::Store::ActiveRecord, release_connection: nil)}
14
+ let(:store) { double(Rpush::Daemon::Store::ActiveRecord, release_connection: nil) }
17
15
 
18
16
  before do
19
- Rpush::Daemon.stub(:store => store)
17
+ Rpush::Daemon.stub(store: store)
20
18
  queue.push([notification, batch])
21
19
  end
22
20
 
23
21
  it 'logs errors' do
24
22
  logger = double
25
- Rpush.stub(:logger => logger)
23
+ Rpush.stub(logger: logger)
26
24
  error = StandardError.new
27
25
  dispatcher.stub(:dispatch).and_raise(error)
28
26
  Rpush.logger.should_receive(:error).with(error)
@@ -30,32 +28,16 @@ describe Rpush::Daemon::DispatcherLoop do
30
28
  end
31
29
 
32
30
  it 'reflects an exception' do
33
- Rpush.stub(:logger => double(:error => nil))
31
+ Rpush.stub(logger: double(error: nil))
34
32
  error = StandardError.new
35
33
  dispatcher.stub(:dispatch).and_raise(error)
36
34
  dispatcher_loop.should_receive(:reflect).with(:error, error)
37
35
  run_dispatcher_loop
38
36
  end
39
37
 
40
- it 'instructs the batch that the notification has been processed' do
41
- batch.should_receive(:notification_dispatched)
42
- run_dispatcher_loop
43
- end
44
-
45
- it "instructs the queue to wakeup the thread when told to stop" do
46
- queue.should_receive(:push).with(Rpush::Daemon::DispatcherLoop::WAKEUP).and_call_original
47
- run_dispatcher_loop
48
- end
49
-
50
38
  describe 'stop' do
51
39
  before do
52
40
  queue.clear
53
- queue.push(Rpush::Daemon::DispatcherLoop::WAKEUP)
54
- end
55
-
56
- it 'does not attempt to dispatch when a WAKEUP is dequeued' do
57
- dispatcher.should_not_receive(:dispatch)
58
- run_dispatcher_loop
59
41
  end
60
42
 
61
43
  it 'instructs the dispatcher to cleanup' do
@@ -1,25 +1,23 @@
1
1
  require "unit_spec_helper"
2
2
 
3
3
  describe Rpush::Daemon::Feeder do
4
- let(:config) { double(:batch_size => 5000,
5
- :push_poll => 0,
6
- :embedded => false,
7
- :push => false,
8
- :wakeup => nil) }
9
- let!(:app) { Rpush::Apns::App.create!(:name => 'my_app', :environment => 'development', :certificate => TEST_CERT) }
10
- let(:notification) { Rpush::Apns::Notification.create!(:device_token => "a" * 64, :app => app) }
4
+ let!(:app) { Rpush::Apns::App.create!(name: 'my_app', environment: 'development', certificate: TEST_CERT) }
5
+ let(:notification) { Rpush::Apns::Notification.create!(device_token: "a" * 64, app: app) }
11
6
  let(:logger) { double }
12
- let(:interruptible_sleep) { double(:sleep => nil, :interrupt_sleep => nil) }
13
- let(:store) { double(Rpush::Daemon::Store::ActiveRecord,
14
- deliverable_notifications: [notification], release_connection: nil) }
7
+ let(:interruptible_sleeper) { double(sleep: nil, stop: nil, start: nil) }
8
+ let(:store) { double(Rpush::Daemon::Store::ActiveRecord, deliverable_notifications: [notification], release_connection: nil) }
15
9
 
16
10
  before do
17
- Rpush.stub(:config => config,:logger => logger)
18
- Rpush::Daemon.stub(:store => store)
19
- Rpush::Daemon::Feeder.stub(:stop? => true)
20
- Rpush::Daemon::AppRunner.stub(:enqueue => nil, :idle => [double(:app => app)])
21
- Rpush::Daemon::InterruptibleSleep.stub(:new => interruptible_sleep)
22
- Rpush::Daemon::Feeder.instance_variable_set('@interruptible_sleeper', nil)
11
+ Rpush.configure do |config|
12
+ config.batch_size = 5000
13
+ config.push_poll = 0
14
+ config.push = false
15
+ end
16
+
17
+ Rpush.stub(logger: logger)
18
+ Rpush::Daemon.stub(store: store)
19
+ Rpush::Daemon::Feeder.stub(should_stop: true, interruptible_sleeper: interruptible_sleeper)
20
+ Rpush::Daemon::AppRunner.stub(enqueue: nil, num_queued: 0)
23
21
  end
24
22
 
25
23
  def start_and_stop
@@ -27,26 +25,26 @@ describe Rpush::Daemon::Feeder do
27
25
  Rpush::Daemon::Feeder.stop
28
26
  end
29
27
 
30
- it 'starts the loop in a new thread if embedded' do
31
- config.stub(:embedded => true)
32
- Thread.should_receive(:new).and_yield
33
- Rpush::Daemon::Feeder.should_receive(:feed_forever)
28
+ it 'loads deliverable notifications' do
29
+ Rpush::Daemon.store.should_receive(:deliverable_notifications).with(Rpush.config.batch_size)
34
30
  start_and_stop
35
31
  end
36
32
 
37
- it 'loads deliverable notifications' do
38
- Rpush::Daemon.store.should_receive(:deliverable_notifications).with([app])
33
+ it 'does not load more notifications if the total queue size is equal to the batch size' do
34
+ Rpush::Daemon::AppRunner.stub(total_queued: Rpush.config.batch_size)
35
+ Rpush::Daemon.store.should_not_receive(:deliverable_notifications)
39
36
  start_and_stop
40
37
  end
41
38
 
42
- it 'does not attempt to load deliverable notifications if there are no idle runners' do
43
- Rpush::Daemon::AppRunner.stub(:idle => [])
44
- Rpush::Daemon.store.should_not_receive(:deliverable_notifications)
39
+ it 'limits the batch size if some runners are still processing notifications' do
40
+ Rpush.config.stub(batch_size: 10)
41
+ Rpush::Daemon::AppRunner.stub(total_queued: 6)
42
+ Rpush::Daemon.store.should_receive(:deliverable_notifications).with(4)
45
43
  start_and_stop
46
44
  end
47
45
 
48
46
  it 'enqueues notifications without looping if in push mode' do
49
- config.stub(:push => true)
47
+ Rpush.config.push = true
50
48
  Rpush::Daemon::Feeder.should_not_receive(:feed_forever)
51
49
  Rpush::Daemon::Feeder.should_receive(:enqueue_notifications)
52
50
  start_and_stop
@@ -65,34 +63,35 @@ describe Rpush::Daemon::Feeder do
65
63
  end
66
64
 
67
65
  describe 'stop' do
68
- it 'interrupts sleep when stopped' do
69
- Rpush::Daemon::Feeder.should_receive(:interrupt_sleep)
66
+ it 'interrupts sleep' do
67
+ interruptible_sleeper.should_receive(:stop)
70
68
  start_and_stop
71
69
  end
72
70
 
73
- it 'releases the store connection when stopped' do
71
+ it 'releases the store connection' do
74
72
  Rpush::Daemon.store.should_receive(:release_connection)
75
73
  start_and_stop
76
74
  end
77
75
  end
78
76
 
79
- it "enqueues notifications when started" do
77
+ it 'enqueues notifications when started' do
80
78
  Rpush::Daemon::Feeder.should_receive(:enqueue_notifications).at_least(:once)
81
79
  Rpush::Daemon::Feeder.stub(:loop).and_yield
82
80
  start_and_stop
83
81
  end
84
82
 
85
- it "sleeps for the given period" do
86
- config.stub(:push_poll => 2)
87
- interruptible_sleep.should_receive(:sleep).with(2)
83
+ it 'sleeps' do
84
+ interruptible_sleeper.should_receive(:sleep)
88
85
  start_and_stop
89
86
  end
90
87
 
91
- it "creates the wakeup socket" do
92
- bind = '127.0.0.1'
93
- port = 12345
94
- config.stub(:wakeup => { :bind => bind, :port => port})
95
- interruptible_sleep.should_receive(:enable_wake_on_udp).with(bind, port)
96
- start_and_stop
88
+ describe 'wakeup' do
89
+ it 'interrupts sleep' do
90
+ interruptible_sleeper.should_receive(:wakeup)
91
+ Rpush::Daemon::Feeder.start
92
+ Rpush::Daemon::Feeder.wakeup
93
+ end
94
+
95
+ after { Rpush::Daemon::Feeder.stop }
97
96
  end
98
97
  end