ar_mailer_aws 0.0.4 → 0.1.0

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.
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'command line options parsing' do
4
- it 'return defaults if no options specified' do
5
- options = ArMailerAWS.parse_options([])
6
- options.batch_size.should == 100
7
- options.delay.should == 180
8
- options.quota.should == 10_000
9
- options.rate.should == 5
10
- options.max_age.should == 3600 * 24 * 7
11
- end
12
-
13
- it 'batch_size' do
14
- ArMailerAWS.parse_options(%w(-b 10)).batch_size.should == 10
15
- end
16
-
17
- it 'delay' do
18
- ArMailerAWS.parse_options(%w(-d 90)).delay.should == 90
19
- end
20
-
21
- it 'quota' do
22
- ArMailerAWS.parse_options(%w(-q 100)).quota.should == 100
23
- end
24
-
25
- it 'rate' do
26
- ArMailerAWS.parse_options(%w(-r 7)).rate.should == 7
27
- end
28
-
29
- it 'max_age' do
30
- ArMailerAWS.parse_options(%w(-m 300)).max_age.should == 300
31
- end
32
-
33
- it 'verbose' do
34
- ArMailerAWS.parse_options(%w(-v)).verbose.should be_true
35
- end
36
-
37
- it 'pid_dir' do
38
- ArMailerAWS.parse_options(%w(-p tmp/pids)).pid_dir.should == 'tmp/pids'
39
- end
40
-
41
- it 'app_name' do
42
- ArMailerAWS.parse_options(%w(--app-name my_daemon)).app_name.should == 'my_daemon'
43
- end
44
- end
@@ -1,158 +0,0 @@
1
- require 'spec_helper'
2
-
3
- def create_email(options={})
4
- BatchEmail.create!({from: 'from@example.com', to: 'to@example.com', mail: 'email content'}.update(options))
5
- end
6
-
7
- describe ArMailerAWS::Sender do
8
-
9
- it 'convert Hash options to OpenStruct' do
10
- sender = ArMailerAWS::Sender.new({})
11
- sender.options.class.name.should == 'OpenStruct'
12
- end
13
-
14
- it 'get default emails model' do
15
- ArMailerAWS::Sender.new.model.name.should == 'BatchEmail'
16
- end
17
-
18
- it 'supply ses options to AWS::SimpleEmailService initializer' do
19
- ArMailerAWS.ses_options = {a: 1}
20
- AWS::SimpleEmailService.should_receive(:new).with({a: 1})
21
- ArMailerAWS::Sender.new
22
- end
23
-
24
- context 'sending' do
25
- before do
26
- BatchEmail.delete_all
27
- end
28
-
29
- describe '#find_emails' do
30
- it 'batch_size emails' do
31
- 5.times { create_email }
32
- @sender = ArMailerAWS::Sender.new(batch_size: 3)
33
- @sender.find_emails.should have(3).emails
34
- end
35
-
36
- it 'ignore emails last_send_attempt_at < 300 seconds ago' do
37
- 2.times { create_email }
38
- 2.times { create_email(last_send_attempt_at: Time.now - 100) }
39
- @sender = ArMailerAWS::Sender.new(batch_size: 3)
40
- @sender.find_emails.should have(2).emails
41
- end
42
- end
43
-
44
- describe '#cleanup' do
45
- it 'do nothing if max_age == 0' do
46
- @sender = ArMailerAWS::Sender.new(max_age: 0)
47
- @sender.model.should_not_receive(:destroy_all)
48
- @sender.cleanup
49
- end
50
-
51
- it 'remove emails with last_send_attempt_at and create_at greater then max_age' do
52
- 2.times { create_email }
53
- 2.times { create_email(last_send_attempt_at: Time.now, created_at: Time.now - 4000) }
54
-
55
- @sender = ArMailerAWS::Sender.new(max_age: 3600)
56
- expect {
57
- @sender.cleanup
58
- }.to change { @sender.model.count }.from(4).to(2)
59
- end
60
- end
61
-
62
- describe '#send_emails' do
63
- before do
64
- @sender = ArMailerAWS::Sender.new(quota: 100)
65
- @sender.ses.stub(:send_raw_email)
66
- @sender.ses.stub(:quotas).and_return({sent_last_24_hours: 0})
67
- end
68
-
69
- context 'success' do
70
- it 'send email via ses' do
71
- 2.times { create_email }
72
- @sender.ses.should_receive(:send_raw_email).twice
73
- @sender.send_emails(@sender.model.all)
74
- end
75
-
76
- it 'remove sent emails' do
77
- 2.times { create_email }
78
- expect {
79
- @sender.send_emails(@sender.model.all)
80
- }.to change { @sender.model.count }.from(2).to(0)
81
- end
82
- end
83
-
84
- context 'error' do
85
- it 'call error_proc' do
86
- email = create_email
87
- exception = StandardError.new
88
- ArMailerAWS.error_proc = proc {}
89
- ArMailerAWS.error_proc.should_receive(:call).with(email, exception)
90
- @sender.ses.should_receive(:send_raw_email).and_raise(exception)
91
- @sender.send_emails([email])
92
- end
93
-
94
- it 'update last_send_attempt_at column' do
95
- email = create_email
96
- exception = StandardError.new
97
- @sender.ses.should_receive(:send_raw_email).and_raise(exception)
98
- @sender.send_emails([email])
99
- email.reload.last_send_attempt_at.should_not be_nil
100
- end
101
- end
102
-
103
- context 'rate' do
104
- it 'call not more the rate times per second' do
105
- 5.times { create_email }
106
- @sender.options.rate = 2
107
- @sender.ses.should_receive(:send_raw_email).twice
108
- begin
109
- Timeout::timeout(1) do
110
- @sender.send_emails(@sender.model.all)
111
- end
112
- rescue Timeout::Error
113
- end
114
- end
115
- end
116
-
117
- context 'quota' do
118
- it 'not exceed quota' do
119
- 10.times { create_email }
120
- @sender.options.quota = 5
121
- expect {
122
- @sender.send_emails(@sender.model.all)
123
- }.to change { @sender.model.count }.by(-5)
124
- end
125
-
126
- it 'consider sent_last_24_hours from ses' do
127
- 10.times { create_email }
128
- @sender.ses.stub(:quotas).and_return({sent_last_24_hours: 10})
129
- @sender.options.quota = 15
130
- expect {
131
- @sender.send_emails(@sender.model.all)
132
- }.to change { @sender.model.count }.by(-5)
133
- end
134
- end
135
- end
136
-
137
- describe '#send_batch' do
138
- before do
139
- @sender = ArMailerAWS::Sender.new
140
- @sender.ses.stub(:send_raw_email)
141
- end
142
-
143
- it 'no pending emails' do
144
- @sender.should_receive(:cleanup)
145
- @sender.should_receive(:find_emails).and_return([])
146
- @sender.should_not_receive(:send_emails)
147
- @sender.send_batch
148
- end
149
-
150
- it 'no pending emails' do
151
- @sender.should_receive(:find_emails).and_return([create_email])
152
- @sender.should_receive(:send_emails)
153
- @sender.send_batch
154
- end
155
- end
156
-
157
- end
158
- end