|
2 | 2 | require "timeout" |
3 | 3 |
|
4 | 4 | describe InfluxDB::Client do |
5 | | - let(:async_options) { true } |
| 5 | + let(:async_options) { { sleep_interval: 0.1 } } |
6 | 6 | let(:client) { described_class.new(async: async_options) } |
7 | 7 | let(:subject) { client } |
8 | 8 | let(:stub_url) { "http://localhost:8086/write?db=&p=root&precision=s&u=root" } |
|
18 | 18 | subject.write_point('a', values: { i: i }) |
19 | 19 | end |
20 | 20 |
|
21 | | - # The timout code is fragile, and heavily dependent on system load |
22 | | - # (and scheduler decisions). On the CI, the system is less |
23 | | - # responsive and needs a bit more time. |
24 | | - timeout_stretch = ENV["TRAVIS"] == "true" ? 10 : 3 |
| 21 | + sleep 1 until worker.threads.none? { |t| t[:influxdb].nil? } |
25 | 22 |
|
26 | | - Timeout.timeout(timeout_stretch * worker.sleep_interval) do |
27 | | - subject.stop! |
28 | | - end |
| 23 | + subject.stop! |
29 | 24 |
|
30 | 25 | worker.threads.each do |t| |
31 | 26 | expect(t.stop?).to be true |
|
41 | 36 | let(:precision) { 'test_precision' } |
42 | 37 | let(:retention_policy) { 'test_period' } |
43 | 38 | let(:database) { 'test_database' } |
| 39 | + let(:async_options) { { num_worker_threads: 1, sleep_interval: 0.1 } } |
44 | 40 |
|
45 | 41 | it "writes aggregate payload to the client" do |
46 | 42 | queue = Queue.new |
|
51 | 47 | subject.write_point(series, { values: { t: 60 } }, precision, retention_policy, database) |
52 | 48 | subject.write_point(series, { values: { t: 61 } }, precision, retention_policy, database) |
53 | 49 |
|
54 | | - Timeout.timeout(worker.sleep_interval) do |
55 | | - expect(queue.pop).to eq ["#{series} t=60i\n#{series} t=61i", precision, retention_policy, database] |
56 | | - end |
| 50 | + sleep 1 until worker.threads.none? { |t| t[:influxdb].nil? } |
| 51 | + |
| 52 | + subject.stop! |
| 53 | + |
| 54 | + expect(queue.pop).to eq ["#{series} t=60i\n#{series} t=61i", precision, retention_policy, database] |
57 | 55 | end |
58 | 56 |
|
59 | 57 | context 'when different precision, retention_policy and database are given' do |
|
72 | 70 | subject.write_point(series, { values: { t: 62 } }, precision, retention_policy2, database) |
73 | 71 | subject.write_point(series, { values: { t: 63 } }, precision, retention_policy, database2) |
74 | 72 |
|
75 | | - Timeout.timeout(worker.sleep_interval) do |
76 | | - expect(queue.pop).to eq ["#{series} t=60i", precision, retention_policy, database] |
77 | | - expect(queue.pop).to eq ["#{series} t=61i", precision2, retention_policy, database] |
78 | | - expect(queue.pop).to eq ["#{series} t=62i", precision, retention_policy2, database] |
79 | | - expect(queue.pop).to eq ["#{series} t=63i", precision, retention_policy, database2] |
80 | | - end |
| 73 | + sleep 1 until worker.threads.none? { |t| t[:influxdb].nil? } |
| 74 | + |
| 75 | + subject.stop! |
| 76 | + |
| 77 | + expect(queue.pop).to eq ["#{series} t=60i", precision, retention_policy, database] |
| 78 | + expect(queue.pop).to eq ["#{series} t=61i", precision2, retention_policy, database] |
| 79 | + expect(queue.pop).to eq ["#{series} t=62i", precision, retention_policy2, database] |
| 80 | + expect(queue.pop).to eq ["#{series} t=63i", precision, retention_policy, database2] |
81 | 81 | end |
82 | 82 | end |
83 | 83 | end |
84 | 84 | end |
85 | 85 |
|
86 | 86 | describe "async options" do |
87 | | - let(:async_options) do |
88 | | - { |
89 | | - max_post_points: 10, |
90 | | - max_queue_size: 100, |
91 | | - num_worker_threads: 1, |
92 | | - sleep_interval: 0.5, |
93 | | - block_on_full_queue: false |
94 | | - } |
95 | | - end |
96 | | - |
97 | 87 | subject { worker } |
98 | 88 | before { worker.stop! } |
99 | 89 |
|
100 | | - specify { expect(subject.max_post_points).to be 10 } |
101 | | - specify { expect(subject.max_queue_size).to be 100 } |
102 | | - specify { expect(subject.num_worker_threads).to be 1 } |
103 | | - specify { expect(subject.sleep_interval).to be_within(0.0001).of(0.5) } |
104 | | - specify { expect(subject.block_on_full_queue).to be false } |
105 | | - specify { expect(subject.queue).to be_kind_of(InfluxDB::MaxQueue) } |
| 90 | + context 'when all options are given' do |
| 91 | + let(:async_options) do |
| 92 | + { |
| 93 | + max_post_points: 10, |
| 94 | + max_queue_size: 100, |
| 95 | + num_worker_threads: 1, |
| 96 | + sleep_interval: 0.5, |
| 97 | + block_on_full_queue: false, |
| 98 | + shutdown_timeout: 0.6, |
| 99 | + } |
| 100 | + end |
| 101 | + |
| 102 | + it "uses the specified values" do |
| 103 | + expect(subject.max_post_points).to be 10 |
| 104 | + expect(subject.max_queue_size).to be 100 |
| 105 | + expect(subject.num_worker_threads).to be 1 |
| 106 | + expect(subject.sleep_interval).to be_within(0.0001).of(0.5) |
| 107 | + expect(subject.block_on_full_queue).to be false |
| 108 | + expect(subject.queue).to be_kind_of(InfluxDB::MaxQueue) |
| 109 | + expect(subject.shutdown_timeout).to be_within(0.0001).of(0.6) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context 'when only sleep_interval is given' do |
| 114 | + let(:async_options) { { sleep_interval: 0.2 } } |
| 115 | + |
| 116 | + it "uses a value for shutdown_timeout that is 2x sleep_interval" do |
| 117 | + expect(subject.sleep_interval).to be_within(0.0001).of(0.2) |
| 118 | + expect(subject.shutdown_timeout).to be_within(0.0001).of(0.4) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context 'when only shutdown_timeout is given' do |
| 123 | + let(:async_options) { { shutdown_timeout: 0.3 } } |
| 124 | + |
| 125 | + it "uses that value" do |
| 126 | + expect(subject.sleep_interval).to be_within(0.0001).of(5) |
| 127 | + expect(subject.shutdown_timeout).to be_within(0.0001).of(0.3) |
| 128 | + end |
| 129 | + end |
106 | 130 | end |
107 | 131 | end |
0 commit comments