axtro-ar_mailer 2.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,553 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class ActionMailer::ARSendmail
4
+ attr_accessor :slept
5
+ def sleep(secs)
6
+ @slept ||= []
7
+ @slept << secs
8
+ end
9
+ end
10
+
11
+ class TestARSendmail < MiniTest::Unit::TestCase
12
+
13
+ def setup
14
+ ActionMailer::Base.reset
15
+ Email.reset
16
+ Net::SMTP.reset
17
+
18
+ @sm = ActionMailer::ARSendmail.new
19
+ @sm.verbose = true
20
+
21
+ Net::SMTP.clear_on_start
22
+
23
+ @include_c_e = ! $".grep(/config\/environment.rb/).empty?
24
+ $" << 'config/environment.rb' unless @include_c_e
25
+ end
26
+
27
+ def teardown
28
+ $".delete 'config/environment.rb' unless @include_c_e
29
+ end
30
+
31
+ def test_class_mailq
32
+ Email.create :from => nobody, :to => 'recip@h1.example.com',
33
+ :mail => 'body0'
34
+ Email.create :from => nobody, :to => 'recip@h1.example.com',
35
+ :mail => 'body1'
36
+ last = Email.create :from => nobody, :to => 'recip@h2.example.com',
37
+ :mail => 'body2'
38
+ last_attempt_time = Time.parse('Thu Aug 10 2006 11:40:05')
39
+ last.last_send_attempt = last_attempt_time.to_i
40
+
41
+ out, err = capture_io do
42
+ ActionMailer::ARSendmail.mailq
43
+ end
44
+
45
+ expected = <<-EOF
46
+ -Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
47
+ 1 5 Thu Aug 10 11:19:49 nobody@example.com
48
+ recip@h1.example.com
49
+
50
+ 2 5 Thu Aug 10 11:19:50 nobody@example.com
51
+ recip@h1.example.com
52
+
53
+ 3 5 Thu Aug 10 11:19:51 nobody@example.com
54
+ Last send attempt: Thu Aug 10 11:40:05 %s 2006
55
+ recip@h2.example.com
56
+
57
+ -- 0 Kbytes in 3 Requests.
58
+ EOF
59
+
60
+ expected = expected % last_attempt_time.strftime('%z')
61
+ assert_equal expected, out
62
+ end
63
+
64
+ def test_class_mailq_empty
65
+ out, err = capture_io do
66
+ ActionMailer::ARSendmail.mailq
67
+ end
68
+
69
+ assert_equal "Mail queue is empty\n", out
70
+ end
71
+
72
+ def test_class_new
73
+ @sm = ActionMailer::ARSendmail.new
74
+
75
+ assert_equal 60, @sm.delay
76
+ assert_equal nil, @sm.once
77
+ assert_equal nil, @sm.verbose
78
+ assert_equal nil, @sm.batch_size
79
+
80
+ @sm = ActionMailer::ARSendmail.new :Delay => 75, :Verbose => true,
81
+ :Once => true, :BatchSize => 1000
82
+
83
+ assert_equal 75, @sm.delay
84
+ assert_equal true, @sm.once
85
+ assert_equal true, @sm.verbose
86
+ assert_equal 1000, @sm.batch_size
87
+ end
88
+
89
+ def test_class_parse_args_batch_size
90
+ options = ActionMailer::ARSendmail.process_args %w[-b 500]
91
+
92
+ assert_equal 500, options[:BatchSize]
93
+
94
+ options = ActionMailer::ARSendmail.process_args %w[--batch-size 500]
95
+
96
+ assert_equal 500, options[:BatchSize]
97
+ end
98
+
99
+ def test_class_parse_args_chdir
100
+ argv = %w[-c /tmp]
101
+
102
+ options = ActionMailer::ARSendmail.process_args argv
103
+
104
+ assert_equal '/tmp', options[:Chdir]
105
+
106
+ argv = %w[--chdir /tmp]
107
+
108
+ options = ActionMailer::ARSendmail.process_args argv
109
+
110
+ assert_equal '/tmp', options[:Chdir]
111
+
112
+ argv = %w[-c /nonexistent]
113
+
114
+ out, err = capture_io do
115
+ assert_raises SystemExit do
116
+ ActionMailer::ARSendmail.process_args argv
117
+ end
118
+ end
119
+ end
120
+
121
+ def test_class_parse_args_daemon
122
+ argv = %w[-d]
123
+
124
+ options = ActionMailer::ARSendmail.process_args argv
125
+
126
+ assert_equal true, options[:Daemon]
127
+
128
+ argv = %w[--daemon]
129
+
130
+ options = ActionMailer::ARSendmail.process_args argv
131
+
132
+ assert_equal true, options[:Daemon]
133
+ end
134
+
135
+ def test_class_parse_args_pidfile
136
+ argv = %w[-p ./log/ar_sendmail.pid]
137
+
138
+ options = ActionMailer::ARSendmail.process_args argv
139
+
140
+ assert_equal './log/ar_sendmail.pid', options[:Pidfile]
141
+
142
+ argv = %w[--pidfile ./log/ar_sendmail.pid]
143
+
144
+ options = ActionMailer::ARSendmail.process_args argv
145
+
146
+ assert_equal './log/ar_sendmail.pid', options[:Pidfile]
147
+ end
148
+
149
+ def test_class_parse_args_delay
150
+ argv = %w[--delay 75]
151
+
152
+ options = ActionMailer::ARSendmail.process_args argv
153
+
154
+ assert_equal 75, options[:Delay]
155
+ end
156
+
157
+ def test_class_parse_args_environment
158
+ assert_equal nil, ENV['RAILS_ENV']
159
+
160
+ argv = %w[-e production]
161
+
162
+ options = ActionMailer::ARSendmail.process_args argv
163
+
164
+ assert_equal 'production', options[:RailsEnv]
165
+
166
+ assert_equal 'production', ENV['RAILS_ENV']
167
+
168
+ argv = %w[--environment production]
169
+
170
+ options = ActionMailer::ARSendmail.process_args argv
171
+
172
+ assert_equal 'production', options[:RailsEnv]
173
+ end
174
+
175
+ def test_class_parse_args_mailq
176
+ options = ActionMailer::ARSendmail.process_args []
177
+ refute_includes options, :MailQ
178
+
179
+ argv = %w[--mailq]
180
+
181
+ options = ActionMailer::ARSendmail.process_args argv
182
+
183
+ assert_equal true, options[:MailQ]
184
+ end
185
+
186
+ def test_class_parse_args_max_age
187
+ options = ActionMailer::ARSendmail.process_args []
188
+ assert_equal 86400 * 7, options[:MaxAge]
189
+
190
+ argv = %w[--max-age 86400]
191
+
192
+ options = ActionMailer::ARSendmail.process_args argv
193
+
194
+ assert_equal 86400, options[:MaxAge]
195
+ end
196
+
197
+ def test_class_parse_args_no_config_environment
198
+ $".delete 'config/environment.rb'
199
+
200
+ out, err = capture_io do
201
+ assert_raises SystemExit do
202
+ ActionMailer::ARSendmail.process_args []
203
+ end
204
+ end
205
+
206
+ ensure
207
+ $" << 'config/environment.rb' if @include_c_e
208
+ end
209
+
210
+ def test_class_parse_args_once
211
+ argv = %w[-o]
212
+
213
+ options = ActionMailer::ARSendmail.process_args argv
214
+
215
+ assert_equal true, options[:Once]
216
+
217
+ argv = %w[--once]
218
+
219
+ options = ActionMailer::ARSendmail.process_args argv
220
+
221
+ assert_equal true, options[:Once]
222
+ end
223
+
224
+ def test_class_usage
225
+ out, err = capture_io do
226
+ assert_raises SystemExit do
227
+ ActionMailer::ARSendmail.usage 'opts'
228
+ end
229
+ end
230
+
231
+ assert_equal '', out
232
+ assert_equal "opts\n", err
233
+
234
+ out, err = capture_io do
235
+ assert_raises SystemExit do
236
+ ActionMailer::ARSendmail.usage 'opts', 'hi'
237
+ end
238
+ end
239
+
240
+ assert_equal '', out
241
+ assert_equal "hi\n\nopts\n", err
242
+ end
243
+
244
+ def test_cleanup
245
+ e1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
246
+ e1.created_on = Time.now
247
+ e2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
248
+ e3 = Email.create :mail => 'body', :to => 'to', :from => 'from'
249
+ e3.last_send_attempt = Time.now
250
+
251
+ out, err = capture_io do
252
+ @sm.cleanup
253
+ end
254
+
255
+ assert_equal '', out
256
+ assert_equal "expired 1 emails from the queue\n", err
257
+ assert_equal 2, Email.records.length
258
+
259
+ assert_equal [e1, e2], Email.records
260
+ end
261
+
262
+ def test_cleanup_disabled
263
+ e1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
264
+ e1.created_on = Time.now
265
+ e2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
266
+
267
+ @sm.max_age = 0
268
+
269
+ out, err = capture_io do
270
+ @sm.cleanup
271
+ end
272
+
273
+ assert_equal '', out
274
+ assert_equal 2, Email.records.length
275
+ end
276
+
277
+ def test_deliver
278
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
279
+
280
+ out, err = capture_io do
281
+ @sm.deliver [email]
282
+ end
283
+
284
+ assert_equal 1, Net::SMTP.deliveries.length
285
+ assert_equal ['body', 'from', 'to'], Net::SMTP.deliveries.first
286
+ assert_equal 0, Email.records.length
287
+ assert_equal 0, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
288
+
289
+ assert_equal '', out
290
+ assert_equal "sent email 00000000001 from from to to: \"queued\"\n", err
291
+ end
292
+
293
+ def test_deliver_not_called_when_no_emails
294
+ sm = ActionMailer::ARSendmail.new({:Once => true})
295
+ sm.expects(:deliver).never
296
+ sm.run
297
+ end
298
+
299
+ def test_deliver_auth_error
300
+ Net::SMTP.on_start do
301
+ e = Net::SMTPAuthenticationError.new 'try again'
302
+ e.set_backtrace %w[one two three]
303
+ raise e
304
+ end
305
+
306
+ now = Time.now.to_i
307
+
308
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
309
+
310
+ out, err = capture_io do
311
+ @sm.deliver [email]
312
+ end
313
+
314
+ assert_equal 0, Net::SMTP.deliveries.length
315
+ assert_equal 1, Email.records.length
316
+ assert_equal 0, Email.records.first.last_send_attempt
317
+ assert_equal 0, Net::SMTP.reset_called
318
+ assert_equal 1, @sm.failed_auth_count
319
+ assert_equal [60], @sm.slept
320
+
321
+ assert_equal '', out
322
+ assert_equal "authentication error, retrying: try again\n", err
323
+ end
324
+
325
+ def test_deliver_auth_error_recover
326
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
327
+ @sm.failed_auth_count = 1
328
+
329
+ out, err = capture_io do @sm.deliver [email] end
330
+
331
+ assert_equal 0, @sm.failed_auth_count
332
+ assert_equal 1, Net::SMTP.deliveries.length
333
+ end
334
+
335
+ def test_deliver_auth_error_twice
336
+ Net::SMTP.on_start do
337
+ e = Net::SMTPAuthenticationError.new 'try again'
338
+ e.set_backtrace %w[one two three]
339
+ raise e
340
+ end
341
+
342
+ @sm.failed_auth_count = 1
343
+
344
+ out, err = capture_io do
345
+ assert_raises Net::SMTPAuthenticationError do
346
+ @sm.deliver []
347
+ end
348
+ end
349
+
350
+ assert_equal 2, @sm.failed_auth_count
351
+ assert_equal "authentication error, giving up: try again\n", err
352
+ end
353
+
354
+ def test_deliver_4xx_error
355
+ Net::SMTP.on_send_message do
356
+ e = Net::SMTPSyntaxError.new 'try again'
357
+ e.set_backtrace %w[one two three]
358
+ raise e
359
+ end
360
+
361
+ now = Time.now.to_i
362
+
363
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
364
+
365
+ out, err = capture_io do
366
+ @sm.deliver [email]
367
+ end
368
+
369
+ assert_equal 0, Net::SMTP.deliveries.length
370
+ assert_equal 1, Email.records.length
371
+ assert_operator now, :<=, Email.records.first.last_send_attempt
372
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
373
+
374
+ assert_equal '', out
375
+ assert_equal "error sending email 1: \"try again\"(Net::SMTPSyntaxError):\n\tone\n\ttwo\n\tthree\n", err
376
+ end
377
+
378
+ def test_deliver_5xx_error
379
+ Net::SMTP.on_send_message do
380
+ e = Net::SMTPFatalError.new 'unknown recipient'
381
+ e.set_backtrace %w[one two three]
382
+ raise e
383
+ end
384
+
385
+ now = Time.now.to_i
386
+
387
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
388
+
389
+ out, err = capture_io do
390
+ @sm.deliver [email]
391
+ end
392
+
393
+ assert_equal 0, Net::SMTP.deliveries.length
394
+ assert_equal 0, Email.records.length
395
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
396
+
397
+ assert_equal '', out
398
+ assert_equal "5xx error sending email 1, removing from queue: \"unknown recipient\"(Net::SMTPFatalError):\n\tone\n\ttwo\n\tthree\n", err
399
+ end
400
+
401
+ def test_deliver_errno_epipe
402
+ Net::SMTP.on_send_message do
403
+ raise Errno::EPIPE
404
+ end
405
+
406
+ now = Time.now.to_i
407
+
408
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
409
+
410
+ out, err = capture_io do
411
+ @sm.deliver [email]
412
+ end
413
+
414
+ assert_equal 0, Net::SMTP.deliveries.length
415
+ assert_equal 1, Email.records.length
416
+ assert_operator now, :>=, Email.records.first.last_send_attempt
417
+ assert_equal 0, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
418
+
419
+ assert_equal '', out
420
+ assert_equal '', err
421
+ end
422
+
423
+ def test_deliver_server_busy
424
+ Net::SMTP.on_send_message do
425
+ e = Net::SMTPServerBusy.new 'try again'
426
+ e.set_backtrace %w[one two three]
427
+ raise e
428
+ end
429
+
430
+ now = Time.now.to_i
431
+
432
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
433
+
434
+ out, err = capture_io do
435
+ @sm.deliver [email]
436
+ end
437
+
438
+ assert_equal 0, Net::SMTP.deliveries.length
439
+ assert_equal 1, Email.records.length
440
+ assert_operator now, :>=, Email.records.first.last_send_attempt
441
+ assert_equal 0, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
442
+ assert_equal [60], @sm.slept
443
+
444
+ assert_equal '', out
445
+ assert_equal "server too busy, sleeping 60 seconds\n", err
446
+ end
447
+
448
+ def test_deliver_syntax_error
449
+ Net::SMTP.on_send_message do
450
+ Net::SMTP.on_send_message # clear
451
+ e = Net::SMTPSyntaxError.new 'blah blah blah'
452
+ e.set_backtrace %w[one two three]
453
+ raise e
454
+ end
455
+
456
+ now = Time.now.to_i
457
+
458
+ email1 = Email.create :mail => 'body', :to => 'to', :from => 'from'
459
+ email2 = Email.create :mail => 'body', :to => 'to', :from => 'from'
460
+
461
+ out, err = capture_io do
462
+ @sm.deliver [email1, email2]
463
+ end
464
+
465
+ assert_equal 1, Net::SMTP.deliveries.length, 'delivery count'
466
+ assert_equal 1, Email.records.length
467
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on SyntaxError'
468
+ assert_operator now, :<=, Email.records.first.last_send_attempt
469
+
470
+ assert_equal '', out
471
+ assert_equal "error sending email 1: \"blah blah blah\"(Net::SMTPSyntaxError):\n\tone\n\ttwo\n\tthree\nsent email 00000000002 from from to to: \"queued\"\n", err
472
+ end
473
+
474
+ def test_deliver_timeout
475
+ Net::SMTP.on_send_message do
476
+ e = Timeout::Error.new 'timed out'
477
+ e.set_backtrace %w[one two three]
478
+ raise e
479
+ end
480
+
481
+ now = Time.now.to_i
482
+
483
+ email = Email.create :mail => 'body', :to => 'to', :from => 'from'
484
+
485
+ out, err = capture_io do
486
+ @sm.deliver [email]
487
+ end
488
+
489
+ assert_equal 0, Net::SMTP.deliveries.length
490
+ assert_equal 1, Email.records.length
491
+ assert_operator now, :>=, Email.records.first.last_send_attempt
492
+ assert_equal 1, Net::SMTP.reset_called, 'Reset connection on Timeout'
493
+
494
+ assert_equal '', out
495
+ assert_equal "error sending email 1: \"timed out\"(Timeout::Error):\n\tone\n\ttwo\n\tthree\n", err
496
+ end
497
+
498
+ def test_do_exit
499
+ out, err = capture_io do
500
+ assert_raises SystemExit do
501
+ @sm.do_exit
502
+ end
503
+ end
504
+
505
+ assert_equal '', out
506
+ assert_equal "caught signal, shutting down\n", err
507
+ end
508
+
509
+ def test_log
510
+ out, err = capture_io do
511
+ @sm.log 'hi'
512
+ end
513
+
514
+ assert_equal "hi\n", err
515
+ end
516
+
517
+ def test_find_emails
518
+ email_data = [
519
+ { :mail => 'body0', :to => 'recip@h1.example.com', :from => nobody },
520
+ { :mail => 'body1', :to => 'recip@h1.example.com', :from => nobody },
521
+ { :mail => 'body2', :to => 'recip@h2.example.com', :from => nobody },
522
+ ]
523
+
524
+ emails = email_data.map do |email_data| Email.create email_data end
525
+
526
+ tried = Email.create :mail => 'body3', :to => 'recip@h3.example.com',
527
+ :from => nobody
528
+
529
+ tried.last_send_attempt = Time.now.to_i - 258
530
+
531
+ found_emails = []
532
+
533
+ out, err = capture_io do
534
+ found_emails = @sm.find_emails
535
+ end
536
+
537
+ assert_equal emails, found_emails
538
+
539
+ assert_equal '', out
540
+ assert_equal "found 3 emails to send\n", err
541
+ end
542
+
543
+ def test_smtp_settings
544
+ ActionMailer::Base.server_settings[:address] = 'localhost'
545
+
546
+ assert_equal 'localhost', @sm.smtp_settings[:address]
547
+ end
548
+
549
+ def nobody
550
+ 'nobody@example.com'
551
+ end
552
+
553
+ end
@@ -0,0 +1,9 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_support'
4
+ require 'test/resources/action_mailer'
5
+ require 'minitest/autorun'
6
+ require 'mocha'
7
+
8
+ require 'action_mailer/ar_mailer'
9
+ require 'action_mailer/ar_sendmail'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: axtro-ar_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Eric Hodel
8
+ - Adam Meehan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-10-26 00:00:00 +01:00
14
+ default_executable: ar_sendmail
15
+ dependencies: []
16
+
17
+ description: Even delivering email to the local machine may take too long when you have to send hundreds of messages. ar_mailer allows you to store messages into the database for later delivery by a separate process, ar_sendmail.
18
+ email: adam.meehan@gmail.com
19
+ executables:
20
+ - ar_sendmail
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - History.txt
25
+ - LICENSE.txt
26
+ - README.rdoc
27
+ files:
28
+ - History.txt
29
+ - LICENSE.txt
30
+ - README.rdoc
31
+ - Rakefile
32
+ - bin/ar_sendmail
33
+ - generators/ar_mailer/ar_mailer_generator.rb
34
+ - generators/ar_mailer/templates/migration.rb
35
+ - generators/ar_mailer/templates/model.rb
36
+ - lib/action_mailer/ar_mailer.rb
37
+ - lib/action_mailer/ar_sendmail.rb
38
+ - lib/smtp_tls.rb
39
+ - share/bsd/ar_sendmail
40
+ - share/linux/ar_sendmail
41
+ - share/linux/ar_sendmail.conf
42
+ - test/resources/action_mailer.rb
43
+ - test/test_armailer.rb
44
+ - test/test_arsendmail.rb
45
+ - test/test_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/adzap/ar_mailer
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --main
53
+ - README.rdoc
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project: seattlerb
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: A two-phase delivery agent for ActionMailer
75
+ test_files:
76
+ - test/test_armailer.rb
77
+ - test/test_arsendmail.rb