bookingsync-engine 5.0.0 → 5.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.
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe BookingSync::Engine::Retryable do
4
+ describe ".perform" do
5
+ let(:before_retry) do
6
+ Class.new do
7
+ attr_reader :called, :errors
8
+
9
+ def initialize
10
+ @called = 0
11
+ @errors = []
12
+ end
13
+
14
+ def call(error)
15
+ @called += 1
16
+ @errors << error
17
+ end
18
+ end.new
19
+ end
20
+
21
+ it "retries logic for given amount of times for given errors
22
+ and raises original error if the number is exceeded" do
23
+ times_executed = 0
24
+
25
+ expect {
26
+ BookingSync::Engine::Retryable.perform(times: 3, errors: [NotImplementedError]) do
27
+ times_executed += 1
28
+ raise NotImplementedError
29
+ end
30
+ }.to raise_error NotImplementedError
31
+
32
+ expect(times_executed).to eq 3
33
+ end
34
+
35
+ it "calls before_retry callback before every retry" do
36
+ expect {
37
+ BookingSync::Engine::Retryable.perform(times: 3, errors: [NotImplementedError], before_retry: before_retry) do
38
+ raise NotImplementedError
39
+ end
40
+ }.to raise_error NotImplementedError
41
+
42
+ expect(before_retry.called).to eq 2
43
+ expect(before_retry.errors.count).to eq 2
44
+ expect(before_retry.errors.uniq.first).to be_a NotImplementedError
45
+ end
46
+
47
+ it "does not retry for given amount of times if it succeeds before exceeding given number" do
48
+ times_executed = 0
49
+
50
+ BookingSync::Engine::Retryable.perform(times: 3, errors: [NotImplementedError]) do
51
+ times_executed += 1
52
+ raise NotImplementedError if times_executed < 2
53
+ end
54
+
55
+ expect(times_executed).to eq 2
56
+ end
57
+
58
+ it "does not retry if no error is raised" do
59
+ times_executed = 0
60
+
61
+ BookingSync::Engine::Retryable.perform(times: 3, errors: [NotImplementedError]) do
62
+ times_executed += 1
63
+ end
64
+
65
+ expect(times_executed).to eq 1
66
+ end
67
+
68
+ it "does not retry not whitelisted errors" do
69
+ times_executed = 0
70
+
71
+ expect {
72
+ BookingSync::Engine::Retryable.perform(times: 3, errors: [LocalJumpError]) do
73
+ times_executed += 1
74
+ raise NotImplementedError
75
+ end
76
+ }.to raise_error NotImplementedError
77
+
78
+ expect(times_executed).to eq 1
79
+ end
80
+ end
81
+ end
@@ -131,6 +131,40 @@ RSpec.describe Account, type: :model do
131
131
  expect(account.token).to be_a OAuth2::AccessToken
132
132
  expect(account.token.token).to eq "refreshed_token"
133
133
  end
134
+
135
+ context "with refresh failing with timeout" do
136
+ before do
137
+ call_count = 0
138
+ allow_any_instance_of(OAuth2::Client).to receive(:get_token).and_wrap_original do |m, *args|
139
+ call_count += 1
140
+ call_count < 3 ? raise(Faraday::TimeoutError) : m.call(*args)
141
+ end
142
+ end
143
+
144
+ context "and retry works" do
145
+ before do
146
+ BookingSyncEngine.setup do |setup|
147
+ setup.token_refresh_timeout_retry_count = 2
148
+ end
149
+ end
150
+
151
+ it "retries as passed in config" do
152
+ expect(account.token.token).to eq "refreshed_token"
153
+ end
154
+ end
155
+
156
+ context "and retry doesn't help" do
157
+ before do
158
+ BookingSyncEngine.setup do |setup|
159
+ setup.token_refresh_timeout_retry_count = 1
160
+ end
161
+ end
162
+
163
+ it "raises error" do
164
+ expect { account.token.token }.to raise_error(Faraday::TimeoutError)
165
+ end
166
+ end
167
+ end
134
168
  end
135
169
  end
136
170
 
@@ -157,6 +191,40 @@ RSpec.describe Account, type: :model do
157
191
  it "returns a client credential token setup without default params" do
158
192
  expect(account.application_token.token).to eq "the_access_token"
159
193
  end
194
+
195
+ context "with refresh failing with timeout" do
196
+ before do
197
+ call_count = 0
198
+ allow_any_instance_of(OAuth2::Client).to receive(:get_token).and_wrap_original do |m, *args|
199
+ call_count += 1
200
+ call_count < 3 ? raise(Faraday::TimeoutError) : m.call(*args)
201
+ end
202
+ end
203
+
204
+ context "and retry works" do
205
+ before do
206
+ BookingSyncEngine.setup do |setup|
207
+ setup.token_refresh_timeout_retry_count = 2
208
+ end
209
+ end
210
+
211
+ it "retries as passed in config" do
212
+ expect(account.application_token.token).to eq "the_access_token"
213
+ end
214
+ end
215
+
216
+ context "and retry doesn't help" do
217
+ before do
218
+ BookingSyncEngine.setup do |setup|
219
+ setup.token_refresh_timeout_retry_count = 1
220
+ end
221
+ end
222
+
223
+ it "raises error" do
224
+ expect { account.application_token.token }.to raise_error(Faraday::TimeoutError)
225
+ end
226
+ end
227
+ end
160
228
  end
161
229
 
162
230
  describe "#oauth_client" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookingsync-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Grosjean
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-01 00:00:00.000000000 Z
12
+ date: 2020-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -205,6 +205,7 @@ files:
205
205
  - lib/bookingsync/engine/models/application.rb
206
206
  - lib/bookingsync/engine/models/base_account.rb
207
207
  - lib/bookingsync/engine/models/multi_applications_account.rb
208
+ - lib/bookingsync/engine/retryable.rb
208
209
  - lib/bookingsync/engine/session_helpers.rb
209
210
  - lib/bookingsync/engine/version.rb
210
211
  - lib/generators/bookingsync/install/install_generator.rb
@@ -265,6 +266,7 @@ files:
265
266
  - spec/helpers/auth_helpers_spec.rb
266
267
  - spec/lib/bookingsync/engine/application_credentials_spec.rb
267
268
  - spec/lib/bookingsync/engine/credentials_resolver_spec.rb
269
+ - spec/lib/bookingsync/engine/retryable_spec.rb
268
270
  - spec/models/account_spec.rb
269
271
  - spec/models/application_spec.rb
270
272
  - spec/models/multi_application_account_spec.rb
@@ -352,6 +354,7 @@ test_files:
352
354
  - spec/support/omniauth.rb
353
355
  - spec/lib/bookingsync/engine/credentials_resolver_spec.rb
354
356
  - spec/lib/bookingsync/engine/application_credentials_spec.rb
357
+ - spec/lib/bookingsync/engine/retryable_spec.rb
355
358
  - spec/fixtures/accounts.yml
356
359
  - spec/routing/oauth_routing_spec.rb
357
360
  - spec/controllers/authenticated_controller_spec.rb