|
129 | 129 | connected_at: Time.utc(2024, 1, 1, 12, 0, 0)) |
130 | 130 | end |
131 | 131 |
|
| 132 | + let(:mock_nc) { double('NATS::Client', rtt: 0.005) } |
| 133 | + |
132 | 134 | let(:stream_info_data) do |
133 | 135 | double('StreamInfo', |
134 | 136 | config: double(subjects: ['test.*']), |
|
138 | 140 | before do |
139 | 141 | allow(JetstreamBridge::Connection).to receive(:instance).and_return(conn_instance) |
140 | 142 | allow(JetstreamBridge::Connection).to receive(:jetstream).and_return(jts) |
| 143 | + allow(JetstreamBridge::Connection).to receive(:nc).and_return(mock_nc) |
141 | 144 | allow(jts).to receive(:stream_info).and_return(stream_info_data) |
| 145 | + allow(JetstreamBridge::Logging).to receive(:warn) |
142 | 146 | end |
143 | 147 |
|
144 | 148 | it 'returns health status hash' do |
|
147 | 151 | expect(result).to have_key(:healthy) |
148 | 152 | expect(result).to have_key(:nats_connected) |
149 | 153 | expect(result).to have_key(:stream) |
| 154 | + expect(result).to have_key(:performance) |
150 | 155 | expect(result).to have_key(:config) |
151 | 156 | expect(result).to have_key(:version) |
152 | 157 | end |
|
196 | 201 | expect(jts).not_to receive(:stream_info) |
197 | 202 | described_class.health_check |
198 | 203 | end |
| 204 | + |
| 205 | + it 'does not measure RTT' do |
| 206 | + expect(mock_nc).not_to receive(:rtt) |
| 207 | + described_class.health_check |
| 208 | + end |
199 | 209 | end |
200 | 210 |
|
201 | 211 | context 'when stream does not exist' do |
|
215 | 225 | end |
216 | 226 | end |
217 | 227 |
|
| 228 | + it 'includes performance metrics' do |
| 229 | + result = described_class.health_check |
| 230 | + expect(result[:performance]).to be_a(Hash) |
| 231 | + expect(result[:performance][:nats_rtt_ms]).to be_a(Numeric) |
| 232 | + expect(result[:performance][:health_check_duration_ms]).to be_a(Numeric) |
| 233 | + expect(result[:performance][:health_check_duration_ms]).to be > 0 |
| 234 | + end |
| 235 | + |
| 236 | + it 'measures NATS RTT actively' do |
| 237 | + expect(mock_nc).to receive(:rtt) |
| 238 | + described_class.health_check |
| 239 | + end |
| 240 | + |
| 241 | + context 'when RTT measurement fails' do |
| 242 | + before do |
| 243 | + allow(mock_nc).to receive(:rtt).and_raise(StandardError, 'RTT failed') |
| 244 | + end |
| 245 | + |
| 246 | + it 'returns nil for RTT and logs warning' do |
| 247 | + result = described_class.health_check |
| 248 | + expect(result[:performance][:nats_rtt_ms]).to be_nil |
| 249 | + expect(JetstreamBridge::Logging).to have_received(:warn).with( |
| 250 | + /Failed to measure NATS RTT/, |
| 251 | + tag: 'JetstreamBridge' |
| 252 | + ) |
| 253 | + end |
| 254 | + |
| 255 | + it 'still reports as healthy if connection works' do |
| 256 | + result = described_class.health_check |
| 257 | + expect(result[:healthy]).to be true |
| 258 | + end |
| 259 | + end |
| 260 | + |
218 | 261 | context 'when health check raises error' do |
219 | 262 | before do |
220 | 263 | allow(JetstreamBridge::Connection).to receive(:instance).and_raise(StandardError, 'Connection error') |
|
498 | 541 | connected_at: nil) |
499 | 542 | end |
500 | 543 |
|
| 544 | + let(:mock_nc) { double('NATS::Client', rtt: 0.005) } |
| 545 | + |
501 | 546 | let(:stream_info_data) do |
502 | 547 | double('StreamInfo', |
503 | 548 | config: double(subjects: ['test.*']), |
|
507 | 552 | before do |
508 | 553 | allow(JetstreamBridge::Connection).to receive(:instance).and_return(conn_instance) |
509 | 554 | allow(JetstreamBridge::Connection).to receive(:jetstream).and_return(jts) |
| 555 | + allow(JetstreamBridge::Connection).to receive(:nc).and_return(mock_nc) |
510 | 556 | allow(jts).to receive(:stream_info).and_return(stream_info_data) |
511 | 557 | end |
512 | 558 |
|
|
0 commit comments