rpush 1.0.0 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +12 -14
  4. data/bin/rpush +11 -2
  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 +10 -7
  17. data/lib/generators/templates/rpush_2_0_0_updates.rb +23 -0
  18. data/lib/rpush.rb +4 -28
  19. data/lib/rpush/client/active_model.rb +21 -0
  20. data/lib/rpush/client/active_model/adm/app.rb +23 -0
  21. data/lib/rpush/client/active_model/adm/data_validator.rb +14 -0
  22. data/lib/rpush/client/active_model/adm/notification.rb +28 -0
  23. data/lib/rpush/client/active_model/apns/app.rb +37 -0
  24. data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +16 -0
  25. data/lib/rpush/client/active_model/apns/device_token_format_validator.rb +14 -0
  26. data/lib/rpush/client/active_model/apns/notification.rb +90 -0
  27. data/lib/rpush/client/active_model/gcm/app.rb +19 -0
  28. data/lib/rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +14 -0
  29. data/lib/rpush/client/active_model/gcm/notification.rb +31 -0
  30. data/lib/rpush/client/active_model/notification.rb +26 -0
  31. data/lib/rpush/client/active_model/payload_data_size_validator.rb +13 -0
  32. data/lib/rpush/client/active_model/registration_ids_count_validator.rb +13 -0
  33. data/lib/rpush/client/active_model/wpns/app.rb +13 -0
  34. data/lib/rpush/client/active_model/wpns/notification.rb +17 -0
  35. data/lib/rpush/client/active_record.rb +19 -0
  36. data/lib/rpush/client/active_record/adm/app.rb +11 -0
  37. data/lib/rpush/client/active_record/adm/notification.rb +11 -0
  38. data/lib/rpush/client/active_record/apns/app.rb +11 -0
  39. data/lib/rpush/client/active_record/apns/feedback.rb +20 -0
  40. data/lib/rpush/client/active_record/apns/notification.rb +46 -0
  41. data/lib/rpush/client/active_record/app.rb +17 -0
  42. data/lib/rpush/client/active_record/gcm/app.rb +11 -0
  43. data/lib/rpush/client/active_record/gcm/notification.rb +11 -0
  44. data/lib/rpush/client/active_record/notification.rb +38 -0
  45. data/lib/rpush/client/active_record/wpns/app.rb +11 -0
  46. data/lib/rpush/client/active_record/wpns/notification.rb +11 -0
  47. data/lib/rpush/client/redis.rb +35 -0
  48. data/lib/rpush/client/redis/adm/app.rb +14 -0
  49. data/lib/rpush/client/redis/adm/notification.rb +11 -0
  50. data/lib/rpush/client/redis/apns/app.rb +11 -0
  51. data/lib/rpush/client/redis/apns/feedback.rb +20 -0
  52. data/lib/rpush/client/redis/apns/notification.rb +11 -0
  53. data/lib/rpush/client/redis/app.rb +22 -0
  54. data/lib/rpush/client/redis/gcm/app.rb +11 -0
  55. data/lib/rpush/client/redis/gcm/notification.rb +11 -0
  56. data/lib/rpush/client/redis/notification.rb +68 -0
  57. data/lib/rpush/client/redis/wpns/app.rb +11 -0
  58. data/lib/rpush/client/redis/wpns/notification.rb +11 -0
  59. data/lib/rpush/configuration.rb +27 -6
  60. data/lib/rpush/daemon.rb +36 -56
  61. data/lib/rpush/daemon/adm/delivery.rb +50 -52
  62. data/lib/rpush/daemon/apns.rb +6 -5
  63. data/lib/rpush/daemon/apns/delivery.rb +20 -44
  64. data/lib/rpush/daemon/apns/feedback_receiver.rb +11 -8
  65. data/lib/rpush/daemon/app_runner.rb +67 -60
  66. data/lib/rpush/daemon/batch.rb +54 -40
  67. data/lib/rpush/daemon/delivery.rb +13 -3
  68. data/lib/rpush/daemon/delivery_error.rb +10 -2
  69. data/lib/rpush/daemon/dispatcher/apns_tcp.rb +106 -0
  70. data/lib/rpush/daemon/dispatcher/http.rb +3 -3
  71. data/lib/rpush/daemon/dispatcher/tcp.rb +3 -3
  72. data/lib/rpush/daemon/dispatcher_loop.rb +15 -6
  73. data/lib/rpush/daemon/errors.rb +18 -0
  74. data/lib/rpush/daemon/feeder.rb +28 -39
  75. data/lib/rpush/daemon/gcm/delivery.rb +19 -20
  76. data/lib/rpush/daemon/interruptible_sleep.rb +26 -45
  77. data/lib/rpush/daemon/loggable.rb +2 -4
  78. data/lib/rpush/daemon/proc_title.rb +16 -0
  79. data/lib/rpush/daemon/queue_payload.rb +12 -0
  80. data/lib/rpush/daemon/reflectable.rb +3 -5
  81. data/lib/rpush/daemon/retry_header_parser.rb +6 -6
  82. data/lib/rpush/daemon/ring_buffer.rb +16 -0
  83. data/lib/rpush/daemon/service_config_methods.rb +23 -7
  84. data/lib/rpush/daemon/signal_handler.rb +51 -0
  85. data/lib/rpush/daemon/store/active_record.rb +71 -38
  86. data/lib/rpush/daemon/store/active_record/reconnectable.rb +15 -15
  87. data/lib/rpush/daemon/store/interface.rb +19 -0
  88. data/lib/rpush/daemon/store/redis.rb +149 -0
  89. data/lib/rpush/daemon/tcp_connection.rb +6 -11
  90. data/lib/rpush/daemon/wpns/delivery.rb +21 -30
  91. data/lib/rpush/deprecatable.rb +4 -3
  92. data/lib/rpush/deprecation.rb +7 -10
  93. data/lib/rpush/embed.rb +7 -2
  94. data/lib/rpush/logger.rb +11 -15
  95. data/lib/rpush/push.rb +0 -1
  96. data/lib/rpush/reflection.rb +6 -12
  97. data/lib/rpush/version.rb +1 -1
  98. data/lib/tasks/quality.rake +34 -0
  99. data/spec/.rubocop.yml +4 -0
  100. data/spec/functional/adm_spec.rb +3 -6
  101. data/spec/functional/apns_spec.rb +118 -24
  102. data/spec/functional/embed_spec.rb +22 -20
  103. data/spec/functional/gcm_spec.rb +4 -7
  104. data/spec/functional/new_app_spec.rb +61 -0
  105. data/spec/functional/retry_spec.rb +46 -0
  106. data/spec/functional/wpns_spec.rb +3 -6
  107. data/spec/functional_spec_helper.rb +26 -0
  108. data/spec/integration/rpush_spec.rb +13 -0
  109. data/spec/integration/support/gcm_success_response.json +1 -0
  110. data/spec/spec_helper.rb +60 -0
  111. data/spec/support/active_record_setup.rb +48 -0
  112. data/{config → spec/support/config}/database.yml +0 -0
  113. data/spec/support/install.sh +43 -7
  114. data/spec/support/simplecov_helper.rb +9 -5
  115. data/spec/support/simplecov_quality_formatter.rb +10 -6
  116. data/spec/unit/apns_feedback_spec.rb +3 -3
  117. data/spec/unit/{adm → client/active_record/adm}/app_spec.rb +3 -3
  118. data/spec/unit/{adm → client/active_record/adm}/notification_spec.rb +5 -7
  119. data/spec/unit/client/active_record/apns/app_spec.rb +29 -0
  120. data/spec/unit/client/active_record/apns/feedback_spec.rb +9 -0
  121. data/spec/unit/client/active_record/apns/notification_spec.rb +231 -0
  122. data/spec/unit/client/active_record/app_spec.rb +30 -0
  123. data/spec/unit/client/active_record/gcm/app_spec.rb +4 -0
  124. data/spec/unit/{gcm → client/active_record/gcm}/notification_spec.rb +5 -7
  125. data/spec/unit/client/active_record/notification_spec.rb +15 -0
  126. data/spec/unit/client/active_record/wpns/app_spec.rb +4 -0
  127. data/spec/unit/client/active_record/wpns/notification_spec.rb +21 -0
  128. data/spec/unit/configuration_spec.rb +12 -5
  129. data/spec/unit/daemon/adm/delivery_spec.rb +57 -54
  130. data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +3 -3
  131. data/spec/unit/daemon/apns/delivery_spec.rb +90 -83
  132. data/spec/unit/daemon/apns/feedback_receiver_spec.rb +24 -17
  133. data/spec/unit/daemon/app_runner_spec.rb +66 -123
  134. data/spec/unit/daemon/batch_spec.rb +52 -115
  135. data/spec/unit/daemon/delivery_spec.rb +15 -1
  136. data/spec/unit/daemon/dispatcher/http_spec.rb +3 -2
  137. data/spec/unit/daemon/dispatcher/tcp_spec.rb +10 -9
  138. data/spec/unit/daemon/dispatcher_loop_spec.rb +7 -12
  139. data/spec/unit/daemon/feeder_spec.rb +40 -39
  140. data/spec/unit/daemon/gcm/delivery_spec.rb +108 -89
  141. data/spec/unit/daemon/reflectable_spec.rb +2 -2
  142. data/spec/unit/daemon/retryable_error_spec.rb +1 -1
  143. data/spec/unit/daemon/service_config_methods_spec.rb +6 -3
  144. data/spec/unit/daemon/signal_handler_spec.rb +72 -0
  145. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +9 -9
  146. data/spec/unit/daemon/store/active_record_spec.rb +38 -47
  147. data/spec/unit/daemon/tcp_connection_spec.rb +22 -34
  148. data/spec/unit/daemon/too_many_requests_error_spec.rb +1 -1
  149. data/spec/unit/daemon/wpns/delivery_spec.rb +61 -50
  150. data/spec/unit/daemon_spec.rb +46 -81
  151. data/spec/unit/embed_spec.rb +4 -2
  152. data/spec/unit/logger_spec.rb +30 -40
  153. data/spec/unit/notification_shared.rb +9 -79
  154. data/spec/unit/push_spec.rb +3 -8
  155. data/spec/unit/reflection_spec.rb +25 -25
  156. data/spec/unit/rpush_spec.rb +1 -2
  157. data/spec/unit_spec_helper.rb +33 -88
  158. metadata +119 -67
  159. data/lib/rpush/TODO +0 -3
  160. data/lib/rpush/adm/app.rb +0 -15
  161. data/lib/rpush/adm/data_validator.rb +0 -11
  162. data/lib/rpush/adm/notification.rb +0 -29
  163. data/lib/rpush/apns/app.rb +0 -29
  164. data/lib/rpush/apns/binary_notification_validator.rb +0 -12
  165. data/lib/rpush/apns/device_token_format_validator.rb +0 -12
  166. data/lib/rpush/apns/feedback.rb +0 -16
  167. data/lib/rpush/apns/notification.rb +0 -84
  168. data/lib/rpush/app.rb +0 -18
  169. data/lib/rpush/daemon/apns/certificate_expired_error.rb +0 -20
  170. data/lib/rpush/daemon/apns/disconnection_error.rb +0 -20
  171. data/lib/rpush/gcm/app.rb +0 -11
  172. data/lib/rpush/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +0 -11
  173. data/lib/rpush/gcm/notification.rb +0 -30
  174. data/lib/rpush/notification.rb +0 -69
  175. data/lib/rpush/notifier.rb +0 -52
  176. data/lib/rpush/payload_data_size_validator.rb +0 -10
  177. data/lib/rpush/railtie.rb +0 -11
  178. data/lib/rpush/registration_ids_count_validator.rb +0 -10
  179. data/lib/rpush/wpns/app.rb +0 -9
  180. data/lib/rpush/wpns/notification.rb +0 -26
  181. data/lib/tasks/cane.rake +0 -18
  182. data/lib/tasks/rpush.rake +0 -16
  183. data/spec/unit/apns/app_spec.rb +0 -29
  184. data/spec/unit/apns/feedback_spec.rb +0 -9
  185. data/spec/unit/apns/notification_spec.rb +0 -208
  186. data/spec/unit/app_spec.rb +0 -30
  187. data/spec/unit/daemon/apns/disconnection_error_spec.rb +0 -18
  188. data/spec/unit/daemon/interruptible_sleep_spec.rb +0 -68
  189. data/spec/unit/gcm/app_spec.rb +0 -4
  190. data/spec/unit/notification_spec.rb +0 -15
  191. data/spec/unit/notifier_spec.rb +0 -49
  192. data/spec/unit/wpns/app_spec.rb +0 -4
  193. data/spec/unit/wpns/notification_spec.rb +0 -30
@@ -1,7 +1,7 @@
1
1
  require "unit_spec_helper"
2
2
 
3
3
  describe Rpush::TooManyRequestsError do
4
- let(:response) { double(:code => 429, :header => { 'retry-after' => 3600 }) }
4
+ let(:response) { double(code: 429, header: { 'retry-after' => 3600 }) }
5
5
  let(:error) { Rpush::TooManyRequestsError.new(429, 12, "Too Many Requests", response) }
6
6
 
7
7
  it "returns an informative message" do
@@ -1,37 +1,44 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush::Daemon::Wpns::Delivery do
4
- let(:app) { Rpush::Wpns::App.new(:name => "MyApp") }
5
- let(:notification) { Rpush::Wpns::Notification.create!(:app => app,:alert => "test",
6
- :uri => "http://some.example/",
7
- :deliver_after => Time.now) }
8
- let(:logger) { double(:error => nil, :info => nil, :warn => nil) }
9
- let(:response) { double(:code => 200, :header => {}) }
10
- let(:http) { double(:shutdown => nil, :request => response) }
4
+ let(:app) { Rpush::Wpns::App.create!(name: "MyApp") }
5
+ let(:notification) do Rpush::Wpns::Notification.create!(app: app, alert: "test",
6
+ uri: "http://some.example/",
7
+ deliver_after: Time.now)
8
+ end
9
+ let(:logger) { double(error: nil, info: nil, warn: nil) }
10
+ let(:response) { double(code: 200, header: {}) }
11
+ let(:http) { double(shutdown: nil, request: response) }
11
12
  let(:now) { Time.parse('2012-10-14 00:00:00') }
12
- let(:batch) { double(mark_failed: nil, mark_delivered: nil, mark_retryable: nil) }
13
+ let(:batch) { double(mark_failed: nil, mark_delivered: nil, mark_retryable: nil, notification_processed: nil) }
13
14
  let(:delivery) { Rpush::Daemon::Wpns::Delivery.new(app, http, notification, batch) }
14
- let(:store) { double(:create_wpns_notification => double(:id => 2)) }
15
+ let(:store) { double(create_wpns_notification: double(id: 2)) }
15
16
 
16
17
  def perform
17
18
  delivery.perform
18
19
  end
19
20
 
21
+ def perform_with_rescue
22
+ expect { perform }.to raise_error
23
+ end
24
+
20
25
  before do
21
- delivery.stub(:reflect => nil)
22
- Rpush::Daemon.stub(:store => store)
23
- Time.stub(:now => now)
24
- Rpush.stub(:logger => logger)
26
+ delivery.stub(reflect: nil)
27
+ Rpush::Daemon.stub(store: store)
28
+ Time.stub(now: now)
29
+ Rpush.stub(logger: logger)
25
30
  end
26
31
 
27
32
  shared_examples_for "an notification with some delivery faliures" do
28
33
  let(:new_notification) { Rpush::Wpns::Notification.where('id != ?', notification.id).first }
29
34
 
30
- before { response.stub(:body => JSON.dump(body)) }
35
+ before { response.stub(body: JSON.dump(body)) }
31
36
 
32
37
  it "marks the original notification falied" do
33
- delivery.should_receive(:mark_failed).with(notification, nil, error_description)
34
- perform rescue Rpush::DeliveryError
38
+ delivery.should_receive(:mark_failed) do |error|
39
+ error.message.should =~ error_description
40
+ end
41
+ perform_with_rescue
35
42
  end
36
43
 
37
44
  it "raises a DeliveryError" do
@@ -41,99 +48,102 @@ describe Rpush::Daemon::Wpns::Delivery do
41
48
 
42
49
  describe "an 200 response" do
43
50
  before do
44
- response.stub(:code => 200)
51
+ response.stub(code: 200)
45
52
  end
46
53
 
47
54
  it "marks the notification as delivered if delivered successfully to all devices" do
48
- response.stub(:body => JSON.dump({ "failure" => 0 }))
49
- response.stub(:to_hash => {"x-notificationstatus" => ["Received"]})
55
+ response.stub(body: JSON.dump("failure" => 0))
56
+ response.stub(to_hash: { "x-notificationstatus" => ["Received"] })
50
57
  batch.should_receive(:mark_delivered).with(notification)
51
58
  perform
52
59
  end
53
60
 
54
61
  it "retries the notification when the queue is full" do
55
- response.stub(:body => JSON.dump({ "failure" => 0 }))
56
- response.stub(:to_hash => { "x-notificationstatus" => ["QueueFull"] })
57
- batch.should_receive(:mark_retryable).with(notification, Time.now + (60*10))
62
+ response.stub(body: JSON.dump("failure" => 0))
63
+ response.stub(to_hash: { "x-notificationstatus" => ["QueueFull"] })
64
+ batch.should_receive(:mark_retryable).with(notification, Time.now + (60 * 10))
58
65
  perform
59
66
  end
60
67
 
61
68
  it "marks the notification as failed if the notification is suppressed" do
62
- response.stub(:body => JSON.dump({ "faliure" => 0 }))
63
- response.stub(:to_hash => { "x-notificationstatus" => ["Suppressed"] })
64
- delivery.should_receive(:mark_failed).with(200, "Notification was received but suppressed by the service.")
65
- perform rescue Rpush::DeliveryError
69
+ response.stub(body: JSON.dump("faliure" => 0))
70
+ response.stub(to_hash: { "x-notificationstatus" => ["Suppressed"] })
71
+ error = Rpush::DeliveryError.new(200, notification.id, 'Notification was received but suppressed by the service.')
72
+ delivery.should_receive(:mark_failed).with(error)
73
+ perform_with_rescue
66
74
  end
67
75
  end
68
76
 
69
77
  describe "an 400 response" do
70
- before { response.stub(:code => 400) }
78
+ before { response.stub(code: 400) }
71
79
  it "marks notifications as failed" do
72
- delivery.should_receive(:mark_failed).with(400,
73
- "Bad XML or malformed notification URI.")
74
- perform rescue Rpush::DeliveryError
80
+ error = Rpush::DeliveryError.new(400, notification.id, 'Bad XML or malformed notification URI.')
81
+ delivery.should_receive(:mark_failed).with(error)
82
+ perform_with_rescue
75
83
  end
76
84
  end
77
85
 
78
86
  describe "an 401 response" do
79
- before { response.stub(:code => 401) }
87
+ before { response.stub(code: 401) }
80
88
  it "marks notifications as failed" do
81
- delivery.should_receive(:mark_failed).with(401,
82
- "Unauthorized to send a notification to this app.")
83
- perform rescue Rpush::DeliveryError
89
+ error = Rpush::DeliveryError.new(401, notification.id, 'Unauthorized to send a notification to this app.')
90
+ delivery.should_receive(:mark_failed).with(error)
91
+ perform_with_rescue
84
92
  end
85
93
  end
86
94
 
87
95
  describe "an 404 response" do
88
- before { response.stub(:code => 404) }
96
+ before { response.stub(code: 404) }
89
97
  it "marks notifications as failed" do
90
- delivery.should_receive(:mark_failed).with(404, "Not Found")
91
- perform rescue Rpush::DeliveryError
98
+ error = Rpush::DeliveryError.new(404, notification.id, 'Not Found')
99
+ delivery.should_receive(:mark_failed).with(error)
100
+ perform_with_rescue
92
101
  end
93
102
  end
94
103
 
95
104
  describe "an 405 response" do
96
- before { response.stub(:code => 405) }
105
+ before { response.stub(code: 405) }
97
106
  it "marks notifications as failed" do
98
- delivery.should_receive(:mark_failed).with(405, "Method Not Allowed")
99
- perform rescue Rpush::DeliveryError
107
+ error = Rpush::DeliveryError.new(405, notification.id, 'Method Not Allowed')
108
+ delivery.should_receive(:mark_failed).with(error)
109
+ perform_with_rescue
100
110
  end
101
111
  end
102
112
 
103
113
  describe "an 406 response" do
104
- before { response.stub(:code => 406) }
114
+ before { response.stub(code: 406) }
105
115
 
106
116
  it "retries the notification" do
107
- batch.should_receive(:mark_retryable).with(notification, Time.now + (60*60))
117
+ batch.should_receive(:mark_retryable).with(notification, Time.now + (60 * 60))
108
118
  perform
109
119
  end
110
120
 
111
121
  it "logs a warning that the notification will be retried" do
112
122
  notification.retries = 1
113
123
  notification.deliver_after = now + 2
114
- logger.should_receive(:warn).with("[MyApp] Per-day throttling limit reached. Notification 1 will be retried after 2012-10-14 00:00:02 (retry 1).")
124
+ logger.should_receive(:warn).with("[MyApp] Per-day throttling limit reached. Notification #{notification.id} will be retried after 2012-10-14 00:00:02 (retry 1).")
115
125
  perform
116
126
  end
117
127
  end
118
128
 
119
129
  describe "an 412 response" do
120
- before { response.stub(:code => 412) }
130
+ before { response.stub(code: 412) }
121
131
 
122
132
  it "retries the notification" do
123
- batch.should_receive(:mark_retryable).with(notification, Time.now + (60*60))
133
+ batch.should_receive(:mark_retryable).with(notification, Time.now + (60 * 60))
124
134
  perform
125
135
  end
126
136
 
127
137
  it "logs a warning that the notification will be retried" do
128
138
  notification.retries = 1
129
139
  notification.deliver_after = now + 2
130
- logger.should_receive(:warn).with("[MyApp] Device unreachable. Notification 1 will be retried after 2012-10-14 00:00:02 (retry 1).")
140
+ logger.should_receive(:warn).with("[MyApp] Device unreachable. Notification #{notification.id} will be retried after 2012-10-14 00:00:02 (retry 1).")
131
141
  perform
132
142
  end
133
143
  end
134
144
 
135
145
  describe "an 503 response" do
136
- before { response.stub(:code => 503) }
146
+ before { response.stub(code: 503) }
137
147
 
138
148
  it "retries the notification exponentially" do
139
149
  delivery.should_receive(:mark_retryable_exponential).with(notification)
@@ -149,11 +159,12 @@ describe Rpush::Daemon::Wpns::Delivery do
149
159
  end
150
160
 
151
161
  describe 'an un-handled response' do
152
- before { response.stub(:code => 418) }
162
+ before { response.stub(code: 418) }
153
163
 
154
164
  it 'marks the notification as failed' do
155
- delivery.should_receive(:mark_failed).with(418, "I'm a Teapot")
156
- perform rescue Rpush::DeliveryError
165
+ error = Rpush::DeliveryError.new(418, notification.id, "I'm a Teapot")
166
+ delivery.should_receive(:mark_failed).with(error)
167
+ perform_with_rescue
157
168
  end
158
169
  end
159
170
  end
@@ -6,75 +6,66 @@ describe Rpush::Daemon, "when starting" do
6
6
 
7
7
  let(:certificate) { double }
8
8
  let(:password) { double }
9
- let(:config) { double(:pid_file => nil, :foreground => true,
10
- :embedded => false, :push => false, :store => :active_record,
11
- :logger => nil) }
12
- let(:logger) { double(:logger, :info => nil, :error => nil, :warn => nil) }
9
+ let(:logger) { double(:logger, info: nil, error: nil, warn: nil) }
13
10
 
14
11
  before do
15
- Rpush.stub(:config => config, :logger => logger)
12
+ Rpush.stub(logger: logger)
16
13
  Rpush::Daemon::Feeder.stub(:start)
17
- Rpush::Daemon::AppRunner.stub(:sync => nil, :stop => nil)
18
- Rpush::Daemon.stub(:daemonize => nil, :exit => nil, :puts => nil)
14
+ Rpush::Daemon::AppRunner.stub(sync: nil, stop: nil)
15
+ Rpush::Daemon.stub(exit: nil, puts: nil)
16
+ Rpush::Daemon::SignalHandler.stub(start: nil, stop: nil, handle_shutdown_signal: nil)
17
+ Process.stub(:daemon)
19
18
  File.stub(:open)
20
- Rails.stub(:root).and_return("/rails_root")
21
19
  end
22
20
 
23
21
  unless Rpush.jruby?
24
22
  it "forks into a daemon if the foreground option is false" do
25
- config.stub(:foreground => false)
23
+ Rpush.config.foreground = false
26
24
  Rpush::Daemon.initialize_store
27
- Rpush::Daemon.store.stub(:after_daemonize => nil)
28
- Rpush::Daemon.should_receive(:daemonize)
29
- Rpush::Daemon.start
30
- end
31
-
32
- it 'notifies the store after forking' do
33
- config.stub(:foreground => false)
34
- Rpush::Daemon.initialize_store
35
- Rpush::Daemon.store.should_receive(:after_daemonize)
25
+ Process.should_receive(:daemon)
36
26
  Rpush::Daemon.start
37
27
  end
38
28
 
39
29
  it "does not fork into a daemon if the foreground option is true" do
40
- config.stub(:foreground => true)
41
- Rpush::Daemon.should_not_receive(:daemonize)
30
+ Rpush.config.foreground = true
31
+ Process.should_not_receive(:daemon)
42
32
  Rpush::Daemon.start
43
33
  end
44
34
 
45
35
  it "does not fork into a daemon if the push option is true" do
46
- config.stub(:push => true)
47
- Rpush::Daemon.should_not_receive(:daemonize)
36
+ Rpush.config.push = true
37
+ Process.should_not_receive(:daemon)
48
38
  Rpush::Daemon.start
49
39
  end
50
40
 
51
41
  it "does not fork into a daemon if the embedded option is true" do
52
- config.stub(:embedded => true)
53
- Rpush::Daemon.should_not_receive(:daemonize)
42
+ Rpush.config.embedded = true
43
+ Process.should_not_receive(:daemon)
54
44
  Rpush::Daemon.start
55
45
  end
56
46
  end
57
47
 
58
- it 'sets up setup signal traps' do
59
- Rpush::Daemon.should_receive(:setup_signal_traps)
48
+ it 'releases the store connection' do
49
+ Rpush::Daemon.store = double
50
+ Rpush::Daemon.store.should_receive(:release_connection)
60
51
  Rpush::Daemon.start
61
52
  end
62
53
 
63
- it 'does not setup signal traps when embedded' do
64
- config.stub(:embedded => true)
65
- Rpush::Daemon.should_not_receive(:setup_signal_traps)
54
+ it 'sets up setup signal traps' do
55
+ Rpush::Daemon::SignalHandler.should_receive(:start)
66
56
  Rpush::Daemon.start
67
57
  end
68
58
 
69
59
  it 'instantiates the store' do
70
- config.stub(:store => :active_record)
60
+ Rpush.config.client = :active_record
71
61
  Rpush::Daemon.start
72
62
  Rpush::Daemon.store.should be_kind_of(Rpush::Daemon::Store::ActiveRecord)
73
63
  end
74
64
 
75
65
  it 'logs an error if the store cannot be loaded' do
76
- config.stub(:store => :foo_bar)
66
+ Rpush.config.client = :foo_bar
77
67
  Rpush.logger.should_receive(:error).with(kind_of(LoadError))
68
+ Rpush::Daemon.stub(:exit) { Rpush::Daemon.store = double.as_null_object }
78
69
  Rpush::Daemon.start
79
70
  end
80
71
 
@@ -84,7 +75,7 @@ describe Rpush::Daemon, "when starting" do
84
75
  end
85
76
 
86
77
  it "logs an error if the PID file could not be written" do
87
- config.stub(:pid_file => '/rails_root/rpush.pid')
78
+ Rpush.config.pid_file = '/rails_root/rpush.pid'
88
79
  File.stub(:open).and_raise(Errno::ENOENT)
89
80
  logger.should_receive(:error).with("Failed to write PID to '/rails_root/rpush.pid': #<Errno::ENOENT: No such file or directory>")
90
81
  Rpush::Daemon.start
@@ -99,61 +90,35 @@ describe Rpush::Daemon, "when starting" do
99
90
  Rpush::Daemon::AppRunner.should_receive(:sync)
100
91
  Rpush::Daemon.start
101
92
  end
102
- end
103
-
104
- describe Rpush::Daemon, "when being shutdown" do
105
- let(:config) { double(:pid_file => '/rails_root/rpush.pid') }
106
- let(:logger) { double(:info => nil, :error => nil, :warn => nil) }
107
-
108
- before do
109
- Rpush.stub(:config => config)
110
- Rpush::Daemon.stub(:puts => nil)
111
- Rpush::Daemon::Feeder.stub(:stop)
112
- Rpush::Daemon::AppRunner.stub(:stop)
113
- end
114
93
 
115
- # These tests do not work on JRuby.
116
- unless Rpush.jruby?
117
- it "shuts down when signaled signaled SIGINT" do
118
- Rpush::Daemon.setup_signal_traps
119
- Rpush::Daemon.should_receive(:shutdown)
120
- Process.kill("SIGINT", Process.pid)
121
- sleep 0.01
94
+ describe "shutdown" do
95
+ it "stops the feeder" do
96
+ Rpush::Daemon::Feeder.should_receive(:stop)
97
+ Rpush::Daemon.shutdown
122
98
  end
123
99
 
124
- it "shuts down when signaled signaled SIGTERM" do
125
- Rpush::Daemon.setup_signal_traps
126
- Rpush::Daemon.should_receive(:shutdown)
127
- Process.kill("SIGTERM", Process.pid)
128
- sleep 0.01
100
+ it "stops the app runners" do
101
+ Rpush::Daemon::AppRunner.should_receive(:stop)
102
+ Rpush::Daemon.shutdown
129
103
  end
130
- end
131
-
132
- it "stops the feeder" do
133
- Rpush::Daemon::Feeder.should_receive(:stop)
134
- Rpush::Daemon.shutdown
135
- end
136
-
137
- it "stops the app runners" do
138
- Rpush::Daemon::AppRunner.should_receive(:stop)
139
- Rpush::Daemon.shutdown
140
- end
141
104
 
142
- it "removes the PID file if one was written" do
143
- File.stub(:exists?).and_return(true)
144
- File.should_receive(:delete).with("/rails_root/rpush.pid")
145
- Rpush::Daemon.shutdown
146
- end
105
+ it "removes the PID file if one was written" do
106
+ Rpush.config.pid_file = "/rails_root/rpush.pid"
107
+ File.stub(:exist?).and_return(true)
108
+ File.should_receive(:delete).with("/rails_root/rpush.pid")
109
+ Rpush::Daemon.shutdown
110
+ end
147
111
 
148
- it "does not attempt to remove the PID file if it does not exist" do
149
- File.stub(:exists?).and_return(false)
150
- File.should_not_receive(:delete)
151
- Rpush::Daemon.shutdown
152
- end
112
+ it "does not attempt to remove the PID file if it does not exist" do
113
+ File.stub(:exists?).and_return(false)
114
+ File.should_not_receive(:delete)
115
+ Rpush::Daemon.shutdown
116
+ end
153
117
 
154
- it "does not attempt to remove the PID file if one was not written" do
155
- config.stub(:pid_file).and_return(nil)
156
- File.should_not_receive(:delete)
157
- Rpush::Daemon.shutdown
118
+ it "does not attempt to remove the PID file if one was not written" do
119
+ Rpush.config.pid_file = nil
120
+ File.should_not_receive(:delete)
121
+ Rpush::Daemon.shutdown
122
+ end
158
123
  end
159
124
  end
@@ -2,10 +2,12 @@ require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush, 'embed' do
4
4
  before do
5
- Rpush::Daemon.stub(:start)
5
+ Rpush::Daemon.stub(start: nil, shutdown: nil)
6
6
  Kernel.stub(:at_exit)
7
7
  end
8
8
 
9
+ after { Rpush.shutdown }
10
+
9
11
  it 'sets the embedded config option to true' do
10
12
  Rpush.embed
11
13
  Rpush.config.embedded.should be_true
@@ -18,7 +20,7 @@ describe Rpush, 'embed' do
18
20
 
19
21
  it 'overrides the default config options with those given as a hash' do
20
22
  Rpush.config.push_poll = 4
21
- expect { Rpush.embed(:push_poll => 2) }.to change(Rpush.config, :push_poll).to(2)
23
+ expect { Rpush.embed(push_poll: 2) }.to change(Rpush.config, :push_poll).to(2)
22
24
  end
23
25
  end
24
26
 
@@ -1,74 +1,59 @@
1
1
  require "unit_spec_helper"
2
2
 
3
3
  module Rails
4
- def self.logger
5
- @logger
6
- end
7
-
8
- def self.logger=(logger)
9
- @logger = logger
10
- end
11
- end
12
-
13
- module HoptoadNotifier
14
- def self.notify(e)
15
- end
4
+ attr_accessor :logger
16
5
  end
17
6
 
18
7
  describe Rpush::Logger do
19
8
  let(:log) { double(:sync= => true) }
20
9
 
21
10
  before do
22
- Rails.stub(:root).and_return("/rails_root")
23
-
24
- @logger_class = if defined?(ActiveSupport::BufferedLogger)
25
- ActiveSupport::BufferedLogger
26
- else
27
- ActiveSupport::Logger
28
- end
29
-
30
- @logger = double(@logger_class.name, :info => nil, :error => nil, :level => 0, :auto_flushing => 1, :auto_flushing= => nil)
11
+ @logger_class = defined?(ActiveSupport::BufferedLogger) ? ActiveSupport::BufferedLogger : ActiveSupport::Logger
12
+ @logger = double(@logger_class.name, info: nil, error: nil, level: 0, auto_flushing: 1, :auto_flushing= => nil)
31
13
  @logger_class.stub(:new).and_return(@logger)
32
14
  Rails.logger = @logger
33
- File.stub(:open => log)
34
- FileUtils.stub(:mkdir_p => nil)
15
+ File.stub(open: log)
16
+ FileUtils.stub(mkdir_p: nil)
35
17
  STDERR.stub(:puts)
18
+ Rpush.config.foreground = true
19
+ Rpush.config.log_dir = Rails.root
36
20
  end
37
21
 
38
22
  it "disables logging if the log file cannot be opened" do
39
23
  File.stub(:open).and_raise(Errno::ENOENT)
40
24
  STDERR.should_receive(:puts).with(/No such file or directory/)
41
25
  STDERR.should_receive(:puts).with(/Logging disabled/)
42
- Rpush::Logger.new(:foreground => true)
26
+ Rpush::Logger.new
43
27
  end
44
28
 
45
29
  it 'creates the log directory' do
46
- FileUtils.should_receive(:mkdir_p).with('/rails_root/log')
47
- Rpush::Logger.new(:foreground => true)
30
+ FileUtils.should_receive(:mkdir_p).with('/tmp/rails_root/log')
31
+ Rpush::Logger.new
48
32
  end
49
33
 
50
34
  it "should open the a log file in the Rails log directory" do
51
- File.should_receive(:open).with('/rails_root/log/rpush.log', 'a')
52
- Rpush::Logger.new(:foreground => true)
35
+ File.should_receive(:open).with('/tmp/rails_root/log/rpush.log', 'a')
36
+ Rpush::Logger.new
53
37
  end
54
38
 
55
39
  it 'sets sync mode on the log descriptor' do
56
40
  log.should_receive(:sync=).with(true)
57
- Rpush::Logger.new(:foreground => true)
41
+ Rpush::Logger.new
58
42
  end
59
43
 
60
44
  it 'uses the user-defined logger' do
61
45
  my_logger = double
62
46
  Rpush.config.logger = my_logger
63
- logger = Rpush::Logger.new({})
47
+ logger = Rpush::Logger.new
64
48
  my_logger.should_receive(:info)
49
+ Rpush.config.foreground = false
65
50
  logger.info('test')
66
51
  end
67
52
 
68
53
  it 'uses ActiveSupport::BufferedLogger if a user-defined logger is not set' do
69
54
  if ActiveSupport.const_defined?('BufferedLogger')
70
55
  ActiveSupport::BufferedLogger.should_receive(:new).with(log, Rails.logger.level)
71
- Rpush::Logger.new(:foreground => true)
56
+ Rpush::Logger.new
72
57
  end
73
58
  end
74
59
 
@@ -76,51 +61,56 @@ describe Rpush::Logger do
76
61
  stub_const('ActiveSupport::Logger', double)
77
62
  ActiveSupport.stub(:const_defined? => false)
78
63
  ActiveSupport::Logger.should_receive(:new).with(log, Rails.logger.level)
79
- Rpush::Logger.new(:foreground => true)
64
+ Rpush::Logger.new
80
65
  end
81
66
 
82
67
  it "should print out the msg if running in the foreground" do
83
- logger = Rpush::Logger.new(:foreground => true)
68
+ logger = Rpush::Logger.new
84
69
  STDOUT.should_receive(:puts).with(/hi mom/)
85
70
  logger.info("hi mom")
86
71
  end
87
72
 
88
73
  it "should not print out the msg if not running in the foreground" do
89
- logger = Rpush::Logger.new(:foreground => false)
74
+ Rpush.config.foreground = false
75
+ logger = Rpush::Logger.new
90
76
  STDOUT.should_not_receive(:puts).with(/hi mom/)
91
77
  logger.info("hi mom")
92
78
  end
93
79
 
94
80
  it "should prefix log lines with the current time" do
81
+ Rpush.config.foreground = false
95
82
  now = Time.now
96
83
  Time.stub(:now).and_return(now)
97
- logger = Rpush::Logger.new(:foreground => false)
84
+ logger = Rpush::Logger.new
98
85
  @logger.should_receive(:info).with(/#{Regexp.escape("[#{now.to_s(:db)}]")}/)
99
86
  logger.info("blah")
100
87
  end
101
88
 
102
89
  it "should prefix error logs with the ERROR label" do
103
- logger = Rpush::Logger.new(:foreground => false)
90
+ Rpush.config.foreground = false
91
+ logger = Rpush::Logger.new
104
92
  @logger.should_receive(:error).with(/#{Regexp.escape("[ERROR]")}/)
105
93
  logger.error("eeek")
106
94
  end
107
95
 
108
96
  it "should prefix warn logs with the WARNING label" do
109
- logger = Rpush::Logger.new(:foreground => false)
97
+ Rpush.config.foreground = false
98
+ logger = Rpush::Logger.new
110
99
  @logger.should_receive(:warn).with(/#{Regexp.escape("[WARNING]")}/)
111
100
  logger.warn("eeek")
112
101
  end
113
102
 
114
103
  it "should handle an Exception instance" do
104
+ Rpush.config.foreground = false
115
105
  e = RuntimeError.new("hi mom")
116
- e.stub(:backtrace => [])
117
- logger = Rpush::Logger.new(:foreground => false)
106
+ e.stub(backtrace: [])
107
+ logger = Rpush::Logger.new
118
108
  @logger.should_receive(:error).with(/RuntimeError, hi mom/)
119
109
  logger.error(e)
120
110
  end
121
111
 
122
112
  it 'defaults auto_flushing to true if the Rails logger does not respond to auto_flushing' do
123
- rails_logger = double(:info => nil, :error => nil, :level => 0)
113
+ rails_logger = double(info: nil, error: nil, level: 0)
124
114
  Rails.logger = rails_logger
125
115
  @logger.auto_flushing.should be_true
126
116
  end