-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathretry_requests_spec.rb
104 lines (90 loc) · 3.15 KB
/
retry_requests_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require "spec_helper"
require "json"
describe InfluxDB::Client do
let(:client) do
described_class.new(
"database",
**{
host: "influxdb.test",
port: 9999,
username: "username",
password: "password",
time_precision: "s"
}.merge(args)
)
end
let(:args) { {} }
let(:database) { client.config.database }
describe "retrying requests" do
let(:series) { "cpu" }
let(:data) do
{ tags: { region: 'us', host: 'server_1' },
values: { temp: 88, value: 54 } }
end
let(:body) do
InfluxDB::PointValue.new(data.merge(series: series)).dump
end
subject { client.write_point(series, data) }
before do
allow(client).to receive(:log)
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: database },
headers: { "Content-Type" => "application/octet-stream" },
body: Zlib.gzip(body, level: 1)
).to_raise(Timeout::Error)
end
it "raises when stopped" do
client.stop!
expect(client).not_to receive(:sleep)
expect { subject }.to raise_error(InfluxDB::ConnectionError) do |e|
expect(e.cause).to be_an_instance_of(Timeout::Error)
end
end
context "when retry is 0" do
let(:args) { { retry: 0 } }
it "raise error directly" do
expect(client).not_to receive(:sleep)
expect { subject }.to raise_error(InfluxDB::ConnectionError) do |e|
expect(e.cause).to be_an_instance_of(Timeout::Error)
end
end
end
context "when retry is 'n'" do
let(:args) { { retry: 3 } }
it "raise error after 'n' attemps" do
expect(client).to receive(:sleep).exactly(3).times
expect { subject }.to raise_error(InfluxDB::ConnectionError) do |e|
expect(e.cause).to be_an_instance_of(Timeout::Error)
end
end
end
context "when retry is -1" do
let(:args) { { retry: -1 } }
before do
stub_request(:post, "http://influxdb.test:9999/write")
.with(
query: { u: "username", p: "password", precision: 's', db: database },
headers: { "Content-Type" => "application/octet-stream" },
body: Zlib.gzip(body, level: 1)
)
.to_raise(Timeout::Error).then
.to_raise(Timeout::Error).then
.to_raise(Timeout::Error).then
.to_raise(Timeout::Error).then
.to_return(status: 204)
end
it "keep trying until get the connection" do
expect(client).to receive(:sleep).exactly(4).times
expect { subject }.to_not raise_error
end
end
it "raise an exception if the server didn't return 200" do
stub_request(:post, "http://influxdb.test:9999/write").with(
query: { u: "username", p: "password", precision: 's', db: database },
headers: { "Content-Type" => "application/octet-stream" },
body: Zlib.gzip(body, level: 1)
).to_return(status: 401)
expect { client.write_point(series, data) }.to raise_error(InfluxDB::AuthenticationError)
end
end
end