rpush 1.0.0-java → 2.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -6,75 +6,67 @@ 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::Synchronizer.stub(sync: nil)
15
+ Rpush::Daemon::AppRunner.stub(stop: nil)
16
+ Rpush::Daemon.stub(exit: nil, puts: nil)
17
+ Rpush::Daemon::SignalHandler.stub(start: nil, stop: nil, handle_shutdown_signal: nil)
18
+ Process.stub(:daemon)
19
19
  File.stub(:open)
20
- Rails.stub(:root).and_return("/rails_root")
21
20
  end
22
21
 
23
22
  unless Rpush.jruby?
24
23
  it "forks into a daemon if the foreground option is false" do
25
- config.stub(:foreground => false)
24
+ Rpush.config.foreground = false
26
25
  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)
26
+ Process.should_receive(:daemon)
36
27
  Rpush::Daemon.start
37
28
  end
38
29
 
39
30
  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)
31
+ Rpush.config.foreground = true
32
+ Process.should_not_receive(:daemon)
42
33
  Rpush::Daemon.start
43
34
  end
44
35
 
45
36
  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)
37
+ Rpush.config.push = true
38
+ Process.should_not_receive(:daemon)
48
39
  Rpush::Daemon.start
49
40
  end
50
41
 
51
42
  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)
43
+ Rpush.config.embedded = true
44
+ Process.should_not_receive(:daemon)
54
45
  Rpush::Daemon.start
55
46
  end
56
47
  end
57
48
 
58
- it 'sets up setup signal traps' do
59
- Rpush::Daemon.should_receive(:setup_signal_traps)
49
+ it 'releases the store connection' do
50
+ Rpush::Daemon.store = double
51
+ Rpush::Daemon.store.should_receive(:release_connection)
60
52
  Rpush::Daemon.start
61
53
  end
62
54
 
63
- it 'does not setup signal traps when embedded' do
64
- config.stub(:embedded => true)
65
- Rpush::Daemon.should_not_receive(:setup_signal_traps)
55
+ it 'sets up setup signal traps' do
56
+ Rpush::Daemon::SignalHandler.should_receive(:start)
66
57
  Rpush::Daemon.start
67
58
  end
68
59
 
69
60
  it 'instantiates the store' do
70
- config.stub(:store => :active_record)
61
+ Rpush.config.client = :active_record
71
62
  Rpush::Daemon.start
72
63
  Rpush::Daemon.store.should be_kind_of(Rpush::Daemon::Store::ActiveRecord)
73
64
  end
74
65
 
75
66
  it 'logs an error if the store cannot be loaded' do
76
- config.stub(:store => :foo_bar)
67
+ Rpush.config.client = :foo_bar
77
68
  Rpush.logger.should_receive(:error).with(kind_of(LoadError))
69
+ Rpush::Daemon.stub(:exit) { Rpush::Daemon.store = double.as_null_object }
78
70
  Rpush::Daemon.start
79
71
  end
80
72
 
@@ -84,7 +76,7 @@ describe Rpush::Daemon, "when starting" do
84
76
  end
85
77
 
86
78
  it "logs an error if the PID file could not be written" do
87
- config.stub(:pid_file => '/rails_root/rpush.pid')
79
+ Rpush.config.pid_file = '/rails_root/rpush.pid'
88
80
  File.stub(:open).and_raise(Errno::ENOENT)
89
81
  logger.should_receive(:error).with("Failed to write PID to '/rails_root/rpush.pid': #<Errno::ENOENT: No such file or directory>")
90
82
  Rpush::Daemon.start
@@ -96,64 +88,38 @@ describe Rpush::Daemon, "when starting" do
96
88
  end
97
89
 
98
90
  it "syncs apps" do
99
- Rpush::Daemon::AppRunner.should_receive(:sync)
91
+ Rpush::Daemon::Synchronizer.should_receive(:sync)
100
92
  Rpush::Daemon.start
101
93
  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
94
 
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
95
+ describe "shutdown" do
96
+ it "stops the feeder" do
97
+ Rpush::Daemon::Feeder.should_receive(:stop)
98
+ Rpush::Daemon.shutdown
122
99
  end
123
100
 
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
101
+ it "stops the app runners" do
102
+ Rpush::Daemon::AppRunner.should_receive(:stop)
103
+ Rpush::Daemon.shutdown
129
104
  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
105
 
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
106
+ it "removes the PID file if one was written" do
107
+ Rpush.config.pid_file = "/rails_root/rpush.pid"
108
+ File.stub(:exist?).and_return(true)
109
+ File.should_receive(:delete).with("/rails_root/rpush.pid")
110
+ Rpush::Daemon.shutdown
111
+ end
147
112
 
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
113
+ it "does not attempt to remove the PID file if it does not exist" do
114
+ File.stub(:exists?).and_return(false)
115
+ File.should_not_receive(:delete)
116
+ Rpush::Daemon.shutdown
117
+ end
153
118
 
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
119
+ it "does not attempt to remove the PID file if one was not written" do
120
+ Rpush.config.pid_file = nil
121
+ File.should_not_receive(:delete)
122
+ Rpush::Daemon.shutdown
123
+ end
158
124
  end
159
125
  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
 
@@ -34,8 +36,8 @@ end
34
36
  describe Rpush, 'sync' do
35
37
  before { Rpush.config.embedded = true }
36
38
 
37
- it 'syncs the AppRunner' do
38
- Rpush::Daemon::AppRunner.should_receive(:sync)
39
+ it 'syncs' do
40
+ Rpush::Daemon::Synchronizer.should_receive(:sync)
39
41
  Rpush.sync
40
42
  end
41
43
  end
@@ -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,58 @@ 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
- it "should not print out the msg if not running in the foreground" do
89
- logger = Rpush::Logger.new(:foreground => false)
90
- STDOUT.should_not_receive(:puts).with(/hi mom/)
91
- logger.info("hi mom")
73
+ unless Rpush.jruby? # These tests do not work on JRuby.
74
+ it "should not print out the msg if not running in the foreground" do
75
+ Rpush.config.foreground = false
76
+ logger = Rpush::Logger.new
77
+ STDOUT.should_not_receive(:puts).with(/hi mom/)
78
+ logger.info("hi mom")
79
+ end
92
80
  end
93
81
 
94
82
  it "should prefix log lines with the current time" do
83
+ Rpush.config.foreground = false
95
84
  now = Time.now
96
85
  Time.stub(:now).and_return(now)
97
- logger = Rpush::Logger.new(:foreground => false)
86
+ logger = Rpush::Logger.new
98
87
  @logger.should_receive(:info).with(/#{Regexp.escape("[#{now.to_s(:db)}]")}/)
99
88
  logger.info("blah")
100
89
  end
101
90
 
102
91
  it "should prefix error logs with the ERROR label" do
103
- logger = Rpush::Logger.new(:foreground => false)
92
+ Rpush.config.foreground = false
93
+ logger = Rpush::Logger.new
104
94
  @logger.should_receive(:error).with(/#{Regexp.escape("[ERROR]")}/)
105
95
  logger.error("eeek")
106
96
  end
107
97
 
108
98
  it "should prefix warn logs with the WARNING label" do
109
- logger = Rpush::Logger.new(:foreground => false)
99
+ Rpush.config.foreground = false
100
+ logger = Rpush::Logger.new
110
101
  @logger.should_receive(:warn).with(/#{Regexp.escape("[WARNING]")}/)
111
102
  logger.warn("eeek")
112
103
  end
113
104
 
114
105
  it "should handle an Exception instance" do
106
+ Rpush.config.foreground = false
115
107
  e = RuntimeError.new("hi mom")
116
- e.stub(:backtrace => [])
117
- logger = Rpush::Logger.new(:foreground => false)
108
+ e.stub(backtrace: [])
109
+ logger = Rpush::Logger.new
118
110
  @logger.should_receive(:error).with(/RuntimeError, hi mom/)
119
111
  logger.error(e)
120
112
  end
121
113
 
122
114
  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)
115
+ rails_logger = double(info: nil, error: nil, level: 0)
124
116
  Rails.logger = rails_logger
125
117
  @logger.auto_flushing.should be_true
126
118
  end
@@ -5,101 +5,31 @@ shared_examples_for "an Notification subclass" do
5
5
  it "calls MultiJson.dump when multi_json responds to :dump" do
6
6
  notification = notification_class.new
7
7
  MultiJson.stub(:respond_to?).with(:dump).and_return(true)
8
- MultiJson.should_receive(:dump).with(any_args())
9
- notification.send(data_setter, { :pirates => 1 })
8
+ MultiJson.should_receive(:dump).with(any_args)
9
+ notification.data = { pirates: 1 }
10
10
  end
11
11
 
12
12
  it "calls MultiJson.encode when multi_json does not respond to :dump" do
13
13
  notification = notification_class.new
14
14
  MultiJson.stub(:respond_to?).with(:dump).and_return(false)
15
- MultiJson.should_receive(:encode).with(any_args())
16
- notification.send(data_setter, { :ninjas => 1 })
15
+ MultiJson.should_receive(:encode).with(any_args)
16
+ notification.data = { ninjas: 1 }
17
17
  end
18
18
 
19
19
  it "raises an ArgumentError if something other than a Hash is assigned" do
20
20
  expect do
21
- notification.send(data_setter, Array.new)
21
+ notification.data = Array.new
22
22
  end.to raise_error(ArgumentError, "must be a Hash")
23
23
  end
24
24
 
25
25
  it "encodes the given Hash as JSON" do
26
- notification.send(data_setter, { :hi => "mom" })
27
- notification.read_attribute(:data).should eq "{\"hi\":\"mom\"}"
26
+ notification.data = { hi: "mom" }
27
+ notification.read_attribute(:data).should eq("{\"hi\":\"mom\"}")
28
28
  end
29
29
 
30
30
  it "decodes the JSON when using the reader method" do
31
- notification.send(data_setter, { :hi => "mom" })
32
- notification.send(data_getter).should eq ({"hi" => "mom"})
33
- end
34
-
35
- describe 'scopes' do
36
- before do
37
- Timecop.freeze(Time.now)
38
-
39
- (@delivered_notification = notification_class.new(:app => app, :delivered => true, :failed => false)).save!(:validate => false)
40
- (@failed_notification = notification_class.new(:app => app, :delivered => false, :failed => true)).save!(:validate => false)
41
- (@new_notification = notification_class.new(:app => app, :delivered => false, :failed => false)).save!(:validate => false)
42
- end
43
-
44
- after do
45
- Timecop.return
46
- end
47
-
48
- describe '.completed' do
49
- it 'should return notifications that have been delivered or failed' do
50
- completed_notification_ids = Rpush::Notification.completed.map(&:id)
51
-
52
- completed_notification_ids.size.should eq 2
53
- completed_notification_ids.should include(@delivered_notification.id, @failed_notification.id)
54
- completed_notification_ids.should_not include(@new_notification.id)
55
- end
56
- end
57
-
58
- describe '.created_before' do
59
- it 'should return notifications that were created before the specified date' do
60
- @delivered_notification.created_at = Time.now - 30.days - 1.second
61
- @delivered_notification.save!(:validate => false)
62
-
63
- notification_ids = Rpush::Notification.created_before(Time.now - 30.days).map(&:id)
64
-
65
- notification_ids.size.should eq 1
66
- notification_ids.should include(@delivered_notification.id)
67
- end
68
- end
69
-
70
- describe '.completed_and_older_than' do
71
- before do
72
- @delivered_notification.created_at = Time.now - 30.days - 1.second
73
- @delivered_notification.save!(:validate => false)
74
-
75
- @failed_notification.created_at = Time.now - 20.days - 1.second
76
- @failed_notification.save!(:validate => false)
77
-
78
- @new_notification.created_at = Time.now - 30.days - 1.second
79
- @new_notification.save!(:validate => false)
80
- end
81
-
82
- it 'should only include completed notifications' do
83
- notification_ids = Rpush::Notification.completed_and_older_than(Time.now - 30.days).map(&:id)
84
-
85
- notification_ids.size.should eq 1
86
- notification_ids.should include(@delivered_notification.id)
87
- end
88
-
89
- it 'should not include completed notifications if not older than specified date' do
90
- notification_ids = Rpush::Notification.completed_and_older_than(Time.now - 30.days).map(&:id)
91
-
92
- notification_ids.size.should eq 1
93
- notification_ids.should_not include(@failed_notification.id)
94
- end
95
-
96
- it 'should return notifications that are completed and created before the specified date' do
97
- notification_ids = Rpush::Notification.completed_and_older_than(Time.now - 20.days).map(&:id)
98
-
99
- notification_ids.size.should eq 2
100
- notification_ids.should include(@delivered_notification.id, @failed_notification.id)
101
- end
102
- end
31
+ notification.data = { hi: "mom" }
32
+ notification.data.should eq("hi" => "mom")
103
33
  end
104
34
  end
105
35
  end
@@ -2,8 +2,9 @@ require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush, 'push' do
4
4
  before do
5
- Rpush::Daemon::AppRunner.stub(:sync => nil, :wait => nil)
6
- Rpush::Daemon::Feeder.stub(:start => nil)
5
+ Rpush::Daemon::Synchronizer.stub(sync: nil)
6
+ Rpush::Daemon::AppRunner.stub(wait: nil)
7
+ Rpush::Daemon::Feeder.stub(start: nil)
7
8
  end
8
9
 
9
10
  it 'sets the push config option to true' do
@@ -16,8 +17,8 @@ describe Rpush, 'push' do
16
17
  Rpush.push
17
18
  end
18
19
 
19
- it 'syncs the app runner' do
20
- Rpush::Daemon::AppRunner.should_receive(:sync)
20
+ it 'syncs' do
21
+ Rpush::Daemon::Synchronizer.should_receive(:sync)
21
22
  Rpush.push
22
23
  end
23
24
 
@@ -26,11 +27,6 @@ describe Rpush, 'push' do
26
27
  Rpush.push
27
28
  end
28
29
 
29
- it 'waits on the app runner' do
30
- Rpush::Daemon::AppRunner.should_receive(:wait)
31
- Rpush.push
32
- end
33
-
34
30
  it 'stops on the app runner' do
35
31
  Rpush::Daemon::AppRunner.should_receive(:stop)
36
32
  Rpush.push
@@ -38,6 +34,6 @@ describe Rpush, 'push' do
38
34
 
39
35
  it 'overrides the default config options with those given as a hash' do
40
36
  Rpush.config.batch_size = 20
41
- expect { Rpush.push(:batch_size => 10) }.to change(Rpush.config, :batch_size).to(10)
37
+ expect { Rpush.push(batch_size: 10) }.to change(Rpush.config, :batch_size).to(10)
42
38
  end
43
39
  end
@@ -1,30 +1,30 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
- describe Rpush do
4
- it 'yields reflections for configuration' do
5
- did_yield = false
6
- Rpush.reflect { |on| did_yield = true }
7
- did_yield.should be_true
8
- end
3
+ describe Rpush do
4
+ it 'yields reflections for configuration' do
5
+ did_yield = false
6
+ Rpush.reflect { did_yield = true }
7
+ did_yield.should be_true
8
+ end
9
9
 
10
- it 'returns all reflections' do
11
- Rpush.reflections.should be_kind_of(Rpush::Reflections)
12
- end
13
- end
10
+ it 'returns all reflections' do
11
+ Rpush.reflections.should be_kind_of(Rpush::Reflections)
12
+ end
13
+ end
14
14
 
15
- describe Rpush::Reflections do
16
- it 'dispatches the given reflection' do
17
- did_yield = false
18
- Rpush.reflect do |on|
19
- on.error { did_yield = true }
20
- end
21
- Rpush.reflections.__dispatch(:error)
22
- did_yield.should be_true
23
- end
15
+ describe Rpush::Reflections do
16
+ it 'dispatches the given reflection' do
17
+ did_yield = false
18
+ Rpush.reflect do |on|
19
+ on.error { did_yield = true }
20
+ end
21
+ Rpush.reflections.__dispatch(:error)
22
+ did_yield.should be_true
23
+ end
24
24
 
25
- it 'raises an error when trying to dispatch and unknown reflection' do
26
- expect do
27
- Rpush.reflections.__dispatch(:unknown)
28
- end.to raise_error(Rpush::Reflections::NoSuchReflectionError)
29
- end
30
- end
25
+ it 'raises an error when trying to dispatch and unknown reflection' do
26
+ expect do
27
+ Rpush.reflections.__dispatch(:unknown)
28
+ end.to raise_error(Rpush::Reflections::NoSuchReflectionError)
29
+ end
30
+ end
@@ -2,8 +2,7 @@ require 'unit_spec_helper'
2
2
 
3
3
  describe Rpush do
4
4
  it "lazy initializes the logger" do
5
- Rpush.config.stub(:foreground => true)
6
- Rpush::Logger.should_receive(:new).with(:foreground => true)
5
+ Rpush::Logger.should_receive(:new)
7
6
  Rpush.logger
8
7
  end
9
8
  end