influxdb-client 1.7.0.pre.1015 → 1.8.0.pre.1240

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,41 +30,41 @@ class ClientTest < Minitest::Test
30
30
  end
31
31
 
32
32
  def test_client_new
33
- refute_nil InfluxDB2::Client.new('http://localhost:9999', 'my-token')
33
+ refute_nil InfluxDB2::Client.new('http://localhost:8086', 'my-token')
34
34
  end
35
35
 
36
36
  def test_client_hash
37
- client1 = InfluxDB2::Client.new('http://localhost:9999', 'my-token')
38
- client2 = InfluxDB2::Client.new('http://localhost:9999', 'my-token-diff')
37
+ client1 = InfluxDB2::Client.new('http://localhost:8086', 'my-token')
38
+ client2 = InfluxDB2::Client.new('http://localhost:8086', 'my-token-diff')
39
39
 
40
40
  refute_equal client1.hash, client2.hash
41
41
  assert_equal client1.hash, client1.hash
42
42
  end
43
43
 
44
44
  def test_client_eq
45
- client1 = InfluxDB2::Client.new('http://localhost:9999', 'my-token')
46
- client2 = InfluxDB2::Client.new('http://localhost:9999', 'my-token-diff')
45
+ client1 = InfluxDB2::Client.new('http://localhost:8086', 'my-token')
46
+ client2 = InfluxDB2::Client.new('http://localhost:8086', 'my-token-diff')
47
47
 
48
48
  refute_equal client1, client2
49
49
  assert_equal client1, client1
50
50
  end
51
51
 
52
52
  def test_client_options
53
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token')
53
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token')
54
54
 
55
- assert_equal 'http://localhost:9999', client.options[:url]
55
+ assert_equal 'http://localhost:8086', client.options[:url]
56
56
  assert_equal 'my-token', client.options[:token]
57
57
  end
58
58
 
59
59
  def test_close
60
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token')
60
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token')
61
61
 
62
62
  assert_equal true, client.close!
63
63
  assert_equal true, client.close!
64
64
  end
65
65
 
66
66
  def test_get_write_api
67
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token')
67
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token')
68
68
 
69
69
  write_api = client.create_write_api
70
70
 
@@ -73,7 +73,7 @@ class ClientTest < Minitest::Test
73
73
  end
74
74
 
75
75
  def test_health
76
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token', use_ssl: false)
76
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token', use_ssl: false)
77
77
 
78
78
  health = client.health
79
79
  assert_equal 'ready for queries and writes', health.message
@@ -24,7 +24,7 @@ class DeleteApiIntegrationTest < MiniTest::Test
24
24
  def setup
25
25
  WebMock.allow_net_connect!
26
26
 
27
- @client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
27
+ @client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
28
28
  bucket: 'my-bucket',
29
29
  org: 'my-org',
30
30
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -56,6 +56,8 @@ class DeleteApiIntegrationTest < MiniTest::Test
56
56
  end
57
57
 
58
58
  def test_delete
59
+ # TODO: https://github.com/influxdata/influxdb/issues/19545
60
+ skip
59
61
  @client.create_delete_api.delete(Time.utc(2015, 10, 16, 8, 20, 15), Time.utc(2020, 10, 16, 8, 20, 15),
60
62
  predicate: 'location="europe"')
61
63
 
@@ -63,6 +65,8 @@ class DeleteApiIntegrationTest < MiniTest::Test
63
65
  end
64
66
 
65
67
  def test_delete_without_predicate
68
+ # TODO: https://github.com/influxdata/influxdb/issues/19545
69
+ skip
66
70
  @client.create_delete_api.delete(Time.utc(2016, 10, 15, 7, 20, 15), Time.utc(2018, 10, 14, 8, 20, 15))
67
71
 
68
72
  assert_equal 2, _query_count
@@ -26,9 +26,9 @@ class DeleteApiTest < MiniTest::Test
26
26
  end
27
27
 
28
28
  def test_delete
29
- stub_request(:any, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org')
29
+ stub_request(:any, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org')
30
30
  .to_return(status: 204)
31
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
31
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
32
32
  bucket: 'my-bucket',
33
33
  org: 'my-org',
34
34
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -40,13 +40,13 @@ class DeleteApiTest < MiniTest::Test
40
40
  body = '{"start":"2019-10-15T08:20:15+00:00","stop":"2019-11-15T08:20:15+00:00","predicate":"key1=\"value1\" ' \
41
41
  'AND key2=\"value\""}'
42
42
 
43
- assert_requested(:post, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
43
+ assert_requested(:post, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
44
44
  end
45
45
 
46
46
  def test_delete_time_as_date_time
47
- stub_request(:any, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org')
47
+ stub_request(:any, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org')
48
48
  .to_return(status: 204)
49
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
49
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
50
50
  bucket: 'my-bucket',
51
51
  org: 'my-org',
52
52
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -59,13 +59,13 @@ class DeleteApiTest < MiniTest::Test
59
59
  body = '{"start":"2019-02-03T04:05:06+07:00","stop":"2019-03-03T04:05:06+07:00","predicate":"key1=\"value1\" ' \
60
60
  'AND key2=\"value\""}'
61
61
 
62
- assert_requested(:post, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
62
+ assert_requested(:post, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
63
63
  end
64
64
 
65
65
  def test_delete_time_as_string
66
- stub_request(:any, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org')
66
+ stub_request(:any, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org')
67
67
  .to_return(status: 204)
68
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
68
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
69
69
  bucket: 'my-bucket',
70
70
  org: 'my-org',
71
71
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -77,13 +77,13 @@ class DeleteApiTest < MiniTest::Test
77
77
  body = '{"start":"2019-02-03T04:05:06+07:00","stop":"2019-04-03T04:05:06+07:00","predicate":"key1=\"value1\" ' \
78
78
  'AND key2=\"value\""}'
79
79
 
80
- assert_requested(:post, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
80
+ assert_requested(:post, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
81
81
  end
82
82
 
83
83
  def test_without_predicate
84
- stub_request(:any, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org')
84
+ stub_request(:any, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org')
85
85
  .to_return(status: 204)
86
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
86
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
87
87
  bucket: 'my-bucket',
88
88
  org: 'my-org',
89
89
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -94,13 +94,13 @@ class DeleteApiTest < MiniTest::Test
94
94
 
95
95
  body = '{"start":"2019-02-03T04:05:06+07:00","stop":"2019-04-03T04:05:06+07:00"}'
96
96
 
97
- assert_requested(:post, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
97
+ assert_requested(:post, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org', times: 1, body: body)
98
98
  end
99
99
 
100
100
  def test_user_agent_header
101
- stub_request(:any, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org')
101
+ stub_request(:any, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org')
102
102
  .to_return(status: 204)
103
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
103
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
104
104
  bucket: 'my-bucket',
105
105
  org: 'my-org',
106
106
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -115,7 +115,7 @@ class DeleteApiTest < MiniTest::Test
115
115
  'User-Agent' => "influxdb-client-ruby/#{InfluxDB2::VERSION}",
116
116
  'Content-Type' => 'application/json'
117
117
  }
118
- assert_requested(:post, 'http://localhost:9999/api/v2/delete?bucket=my-bucket&org=my-org',
118
+ assert_requested(:post, 'http://localhost:8086/api/v2/delete?bucket=my-bucket&org=my-org',
119
119
  times: 1, body: body, headers: headers)
120
120
  end
121
121
  end
@@ -24,7 +24,7 @@ class QueryApiIntegrationTest < MiniTest::Test
24
24
  def setup
25
25
  WebMock.allow_net_connect!
26
26
 
27
- @client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
27
+ @client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
28
28
  bucket: 'my-bucket',
29
29
  org: 'my-org',
30
30
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -24,7 +24,7 @@ class QueryApiStreamTest < MiniTest::Test
24
24
  def setup
25
25
  WebMock.allow_net_connect!
26
26
 
27
- @client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
27
+ @client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
28
28
  bucket: 'my-bucket',
29
29
  org: 'my-org',
30
30
  precision: InfluxDB2::WritePrecision::NANOSECOND,
@@ -35,9 +35,9 @@ class QueryApiTest < MiniTest::Test
35
35
  ',,0,1970-01-01T00:00:20Z,1970-01-01T00:00:30Z,1970-01-01T00:00:20Z,22,free,mem,B,west'
36
36
 
37
37
  def test_query_raw
38
- stub_request(:post, 'http://localhost:9999/api/v2/query?org=my-org')
38
+ stub_request(:post, 'http://localhost:8086/api/v2/query?org=my-org')
39
39
  .to_return(body: SUCCESS_DATA)
40
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
40
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
41
41
  bucket: 'my-bucket',
42
42
  org: 'my-org',
43
43
  use_ssl: false)
@@ -50,10 +50,10 @@ class QueryApiTest < MiniTest::Test
50
50
  end
51
51
 
52
52
  def test_query
53
- stub_request(:post, 'http://localhost:9999/api/v2/query?org=my-org')
53
+ stub_request(:post, 'http://localhost:8086/api/v2/query?org=my-org')
54
54
  .to_return(body: SUCCESS_DATA)
55
55
 
56
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
56
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
57
57
  bucket: 'my-bucket',
58
58
  org: 'my-org',
59
59
  use_ssl: false)
@@ -74,10 +74,10 @@ class QueryApiTest < MiniTest::Test
74
74
  end
75
75
 
76
76
  def test_headers
77
- stub_request(:post, 'http://localhost:9999/api/v2/query?org=my-org')
77
+ stub_request(:post, 'http://localhost:8086/api/v2/query?org=my-org')
78
78
  .to_return(body: SUCCESS_DATA)
79
79
 
80
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
80
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
81
81
  bucket: 'my-bucket',
82
82
  org: 'my-org',
83
83
  use_ssl: false)
@@ -90,7 +90,7 @@ class QueryApiTest < MiniTest::Test
90
90
  'User-Agent' => "influxdb-client-ruby/#{InfluxDB2::VERSION}",
91
91
  'Content-Type' => 'application/json'
92
92
  }
93
- assert_requested(:post, 'http://localhost:9999/api/v2/query?org=my-org',
93
+ assert_requested(:post, 'http://localhost:8086/api/v2/query?org=my-org',
94
94
  times: 1, headers: headers)
95
95
  end
96
96
  end
@@ -26,7 +26,7 @@ class WriteApiBatchingTest < MiniTest::Test
26
26
 
27
27
  @write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
28
28
  batch_size: 2, flush_interval: 5_000, retry_interval: 2_000)
29
- @client = InfluxDB2::Client.new('http://localhost:9999',
29
+ @client = InfluxDB2::Client.new('http://localhost:8086',
30
30
  'my-token',
31
31
  bucket: 'my-bucket',
32
32
  org: 'my-org',
@@ -68,7 +68,7 @@ class WriteApiBatchingTest < MiniTest::Test
68
68
  end
69
69
 
70
70
  def test_batch_size
71
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
71
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
72
72
  .to_return(status: 204)
73
73
 
74
74
  @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=1.0 1')
@@ -83,20 +83,20 @@ class WriteApiBatchingTest < MiniTest::Test
83
83
  request2 = "h2o_feet,location=coyote_creek level\\ water_level=3.0 3\n" \
84
84
  'h2o_feet,location=coyote_creek level\\ water_level=4.0 4'
85
85
 
86
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
86
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
87
87
  times: 1, body: request1)
88
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
88
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
89
89
  times: 1, body: request2)
90
90
  end
91
91
 
92
92
  def test_batch_size_group_by
93
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
93
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
94
94
  .to_return(status: 204)
95
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=s')
95
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=s')
96
96
  .to_return(status: 204)
97
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns')
97
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns')
98
98
  .to_return(status: 204)
99
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket2&org=my-org-a&precision=ns')
99
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket2&org=my-org-a&precision=ns')
100
100
  .to_return(status: 204)
101
101
 
102
102
  bucket = 'my-bucket'
@@ -113,21 +113,21 @@ class WriteApiBatchingTest < MiniTest::Test
113
113
 
114
114
  sleep(1)
115
115
 
116
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
116
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
117
117
  times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=1.0 1')
118
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=s',
118
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=s',
119
119
  times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2')
120
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns',
120
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns',
121
121
  times: 1, body: "h2o_feet,location=coyote_creek level\\ water_level=3.0 3\n" \
122
122
  'h2o_feet,location=coyote_creek level\\ water_level=4.0 4')
123
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket2&org=my-org-a&precision=ns',
123
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket2&org=my-org-a&precision=ns',
124
124
  times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=5.0 5')
125
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns',
125
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org-a&precision=ns',
126
126
  times: 1, body: 'h2o_feet,location=coyote_creek level\\ water_level=6.0 6')
127
127
  end
128
128
 
129
129
  def test_flush_interval
130
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
130
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
131
131
  .to_return(status: 204)
132
132
 
133
133
  request1 = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1\n" \
@@ -139,31 +139,31 @@ class WriteApiBatchingTest < MiniTest::Test
139
139
 
140
140
  sleep(1)
141
141
 
142
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
142
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
143
143
  times: 1, body: request1)
144
144
 
145
145
  @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
146
146
 
147
147
  sleep(2)
148
148
 
149
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
149
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
150
150
  times: 0, body: request2)
151
151
 
152
152
  sleep(3)
153
153
 
154
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
154
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
155
155
  times: 1, body: request2)
156
156
  end
157
157
 
158
158
  def test_flush_all_by_close_client
159
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
159
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
160
160
  .to_return(status: 204)
161
161
 
162
162
  @client.close!
163
163
 
164
164
  @write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
165
165
  batch_size: 10, flush_interval: 5_000)
166
- @client = InfluxDB2::Client.new('http://localhost:9999',
166
+ @client = InfluxDB2::Client.new('http://localhost:8086',
167
167
  'my-token',
168
168
  bucket: 'my-bucket',
169
169
  org: 'my-org',
@@ -176,22 +176,81 @@ class WriteApiBatchingTest < MiniTest::Test
176
176
  @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=2.0 2')
177
177
  @write_client.write(data: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
178
178
 
179
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
179
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
180
180
  times: 0, body: 'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
181
181
 
182
182
  @client.close!
183
183
 
184
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
184
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
185
185
  times: 1, body: "h2o_feet,location=coyote_creek level\\ water_level=1.0 1\n" \
186
186
  "h2o_feet,location=coyote_creek level\\ water_level=2.0 2\n" \
187
187
  'h2o_feet,location=coyote_creek level\\ water_level=3.0 3')
188
188
  end
189
189
 
190
+ def test_jitter_interval
191
+ @client.close!
192
+
193
+ @client = InfluxDB2::Client.new('http://localhost:8086',
194
+ 'my-token',
195
+ bucket: 'my-bucket',
196
+ org: 'my-org',
197
+ precision: InfluxDB2::WritePrecision::NANOSECOND,
198
+ use_ssl: false)
199
+
200
+ @write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
201
+ batch_size: 2, flush_interval: 5_000, jitter_interval: 2_000)
202
+ @write_client = @client.create_write_api(write_options: @write_options)
203
+
204
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
205
+ .to_return(status: 204)
206
+
207
+ request = "h2o_feet,location=coyote_creek water_level=1.0 1\n" \
208
+ 'h2o_feet,location=coyote_creek water_level=2.0 2'
209
+
210
+ @write_client.write(data: ['h2o_feet,location=coyote_creek water_level=1.0 1',
211
+ 'h2o_feet,location=coyote_creek water_level=2.0 2'])
212
+
213
+ sleep(0.05)
214
+
215
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
216
+ times: 0, body: request)
217
+
218
+ sleep(2)
219
+
220
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
221
+ times: 1, body: request)
222
+ end
223
+ end
224
+
225
+ class WriteApiRetryStrategyTest < MiniTest::Test
226
+ def setup
227
+ WebMock.disable_net_connect!
228
+
229
+ @write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
230
+ batch_size: 2, flush_interval: 5_000, retry_interval: 2_000)
231
+ @client = InfluxDB2::Client.new('http://localhost:8086',
232
+ 'my-token',
233
+ bucket: 'my-bucket',
234
+ org: 'my-org',
235
+ precision: InfluxDB2::WritePrecision::NANOSECOND,
236
+ use_ssl: false)
237
+
238
+ @write_client = @client.create_write_api(write_options: @write_options)
239
+ end
240
+
241
+ def teardown
242
+ @client.close!
243
+
244
+ assert_equal true, @write_client.closed
245
+
246
+ WebMock.reset!
247
+ end
248
+
190
249
  def test_retry_interval_by_config
191
250
  error_body = '{"code":"temporarily unavailable","message":"Token is temporarily over quota. '\
192
251
  'The Retry-After header describes when to try the write again."}'
193
252
 
194
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
253
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
195
254
  .to_return(status: 429, headers: { 'X-Platform-Error-Code' => 'temporarily unavailable' }, body: error_body).then
196
255
  .to_return(status: 204)
197
256
 
@@ -203,17 +262,17 @@ class WriteApiBatchingTest < MiniTest::Test
203
262
 
204
263
  sleep(0.5)
205
264
 
206
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
265
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
207
266
  times: 1, body: request)
208
267
 
209
268
  sleep(1)
210
269
 
211
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
270
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
212
271
  times: 1, body: request)
213
272
 
214
- sleep(1)
273
+ sleep(5)
215
274
 
216
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
275
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
217
276
  times: 2, body: request)
218
277
  end
219
278
 
@@ -221,7 +280,7 @@ class WriteApiBatchingTest < MiniTest::Test
221
280
  error_body = '{"code":"temporarily unavailable","message":"Server is temporarily unavailable to accept writes. '\
222
281
  'The Retry-After header describes when to try the write again."}'
223
282
 
224
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
283
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
225
284
  .to_return(status: 503, headers: { 'X-Platform-Error-Code' => 'temporarily unavailable', 'Retry-After' => '3' },
226
285
  body: error_body).then
227
286
  .to_return(status: 204)
@@ -237,56 +296,253 @@ class WriteApiBatchingTest < MiniTest::Test
237
296
 
238
297
  sleep(0.5)
239
298
 
240
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
299
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
241
300
  times: 1, body: request)
242
301
 
243
302
  sleep(1)
244
303
 
245
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
304
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
246
305
  times: 1, body: request)
247
306
 
248
307
  sleep(1)
249
308
 
250
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
309
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
251
310
  times: 1, body: request)
252
311
 
253
312
  sleep(1)
254
313
 
255
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
314
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
256
315
  times: 2, body: request)
257
316
  end
258
317
 
259
- def test_jitter_interval
260
- @client.close!
318
+ def test_max_retries
319
+ error_body = '{"code":"temporarily unavailable","message":"Server is temporarily unavailable to accept writes. '\
320
+ 'The Retry-After header describes when to try the write again."}'
261
321
 
262
- @client = InfluxDB2::Client.new('http://localhost:9999',
263
- 'my-token',
264
- bucket: 'my-bucket',
265
- org: 'my-org',
266
- precision: InfluxDB2::WritePrecision::NANOSECOND,
267
- use_ssl: false)
322
+ headers = { 'X-Platform-Error-Code' => 'temporarily unavailable' }
268
323
 
269
- @write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
270
- batch_size: 2, flush_interval: 5_000, jitter_interval: 2_000)
271
- @write_client = @client.create_write_api(write_options: @write_options)
324
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
325
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
326
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
327
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
328
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
329
+ .to_return(status: 429, headers: headers, body: error_body) # not called
330
+
331
+ point = InfluxDB2::Point.new(name: 'h2o')
332
+ .add_tag('location', 'europe')
333
+ .add_field('level', 2.0)
334
+
335
+ request = 'h2o,location=europe level=2.0'
336
+
337
+ write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
338
+ batch_size: 1, retry_interval: 2_000, max_retries: 3,
339
+ max_retry_delay: 5_000, exponential_base: 2)
340
+
341
+ error = assert_raises InfluxDB2::InfluxError do
342
+ @client.create_write_api(write_options: write_options).write(data: point)
343
+
344
+ sleep(0.5)
345
+
346
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
347
+ times: 1, body: request)
348
+
349
+ sleep(2)
350
+
351
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
352
+ times: 2, body: request)
353
+
354
+ sleep(4)
355
+
356
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
357
+ times: 3, body: request)
272
358
 
273
- stub_request(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
359
+ sleep(5)
360
+
361
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
362
+ times: 4, body: request)
363
+
364
+ sleep(5)
365
+
366
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
367
+ times: 4, body: request)
368
+
369
+ sleep(5)
370
+ end
371
+
372
+ assert_equal('429', error.code)
373
+
374
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
375
+ times: 4, body: request)
376
+ end
377
+
378
+ def test_influx_exception
379
+ error_body = '{"code":"invalid","message":"unable to parse '\
380
+ '\'h2o_feet, location=coyote_creek water_level=1.0 1\': missing tag key"}'
381
+
382
+ stub_request(:any, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
383
+ .to_return(status: 400, headers: { 'X-Platform-Error-Code' => 'invalid' }, body: error_body)
384
+
385
+ write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
386
+ batch_size: 1, retry_interval: 2_000, max_retries: 3,
387
+ max_retry_delay: 5_000, exponential_base: 2)
388
+
389
+ error = assert_raises InfluxDB2::InfluxError do
390
+ @client.create_write_api(write_options: write_options).write(data: 'h2o,location=west value=33i 15')
391
+
392
+ sleep(1)
393
+ end
394
+
395
+ assert_equal '400', error.code
396
+ assert_equal 'invalid', error.reference
397
+ assert_equal "unable to parse 'h2o_feet, location=coyote_creek water_level=1.0 1': missing tag key", error.message
398
+ end
399
+
400
+ def test_max_retries_by_header
401
+ error_body = '{"code":"temporarily unavailable","message":"Server is temporarily unavailable to accept writes. '\
402
+ 'The Retry-After header describes when to try the write again."}'
403
+
404
+ headers = { 'X-Platform-Error-Code' => 'temporarily unavailable', 'Retry-After' => '3' }
405
+
406
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
407
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
408
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
409
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
410
+ .to_return(status: 429, headers: headers, body: error_body).then # retry
411
+ .to_return(status: 429, headers: headers, body: error_body) # not called
412
+
413
+ point = InfluxDB2::Point.new(name: 'h2o')
414
+ .add_tag('location', 'europe')
415
+ .add_field('level', 2.0)
416
+
417
+ request = 'h2o,location=europe level=2.0'
418
+
419
+ write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
420
+ batch_size: 1, retry_interval: 2_000, max_retries: 3,
421
+ max_retry_delay: 5_000, exponential_base: 2)
422
+
423
+ error = assert_raises InfluxDB2::InfluxError do
424
+ @client.create_write_api(write_options: write_options).write(data: point)
425
+
426
+ sleep(0.5)
427
+
428
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
429
+ times: 1, body: request)
430
+
431
+ sleep(3)
432
+
433
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
434
+ times: 2, body: request)
435
+
436
+ sleep(3)
437
+
438
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
439
+ times: 3, body: request)
440
+
441
+ sleep(3)
442
+
443
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
444
+ times: 4, body: request)
445
+
446
+ sleep(3)
447
+ end
448
+
449
+ assert_equal('429', error.code)
450
+
451
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
452
+ times: 4, body: request)
453
+ end
454
+
455
+ def test_connection_error
456
+ error_message = 'Failed to open TCP connection to localhost:8086' \
457
+ '(Connection refused - connect(2) for "localhost" port 8086)'
458
+
459
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
460
+ .to_raise(Errno::ECONNREFUSED.new(error_message))
461
+ .to_raise(Errno::ECONNREFUSED.new(error_message))
462
+ .to_raise(Errno::ECONNREFUSED.new(error_message))
463
+ .to_raise(Errno::ECONNREFUSED.new(error_message))
464
+ .to_raise(Errno::ECONNREFUSED.new(error_message))
465
+
466
+ point = InfluxDB2::Point.new(name: 'h2o')
467
+ .add_tag('location', 'europe')
468
+ .add_field('level', 2.0)
469
+
470
+ request = 'h2o,location=europe level=2.0'
471
+
472
+ write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
473
+ batch_size: 1, retry_interval: 2_000, max_retries: 3,
474
+ max_retry_delay: 5_000, exponential_base: 2)
475
+
476
+ error = assert_raises InfluxDB2::InfluxError do
477
+ @client.create_write_api(write_options: write_options).write(data: point)
478
+
479
+ sleep(0.5)
480
+
481
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
482
+ times: 1, body: request)
483
+
484
+ sleep(2)
485
+
486
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
487
+ times: 2, body: request)
488
+
489
+ sleep(4)
490
+
491
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
492
+ times: 3, body: request)
493
+
494
+ sleep(5)
495
+
496
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
497
+ times: 4, body: request)
498
+
499
+ sleep(5)
500
+
501
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
502
+ times: 4, body: request)
503
+
504
+ sleep(5)
505
+ end
506
+
507
+ assert_equal('Connection refused - ' + error_message, error.message)
508
+ end
509
+
510
+ def test_write_connection_error
511
+ stub_request(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns')
512
+ .to_raise(Errno::ECONNREFUSED.new(''))
513
+ .to_raise(Errno::ECONNREFUSED.new(''))
274
514
  .to_return(status: 204)
275
515
 
276
- request = "h2o_feet,location=coyote_creek water_level=1.0 1\n" \
277
- 'h2o_feet,location=coyote_creek water_level=2.0 2'
516
+ point = InfluxDB2::Point.new(name: 'h2o')
517
+ .add_tag('location', 'europe')
518
+ .add_field('level', 2.0)
278
519
 
279
- @write_client.write(data: ['h2o_feet,location=coyote_creek water_level=1.0 1',
280
- 'h2o_feet,location=coyote_creek water_level=2.0 2'])
520
+ request = 'h2o,location=europe level=2.0'
281
521
 
282
- sleep(0.05)
522
+ write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
523
+ batch_size: 1, retry_interval: 2_000, max_retries: 3,
524
+ max_retry_delay: 5_000, exponential_base: 2)
283
525
 
284
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
285
- times: 0, body: request)
526
+ @client.create_write_api(write_options: write_options).write(data: point)
286
527
 
287
- sleep(2)
528
+ sleep(0.5)
288
529
 
289
- assert_requested(:post, 'http://localhost:9999/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
530
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
290
531
  times: 1, body: request)
532
+
533
+ sleep(2)
534
+
535
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
536
+ times: 2, body: request)
537
+
538
+ sleep(4)
539
+
540
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
541
+ times: 3, body: request)
542
+
543
+ sleep(5)
544
+
545
+ assert_requested(:post, 'http://localhost:8086/api/v2/write?bucket=my-bucket&org=my-org&precision=ns',
546
+ times: 3, body: request)
291
547
  end
292
548
  end