From 498f7316937cb062f41abef102b9f241798f9f76 Mon Sep 17 00:00:00 2001 From: DmitryTsepelev Date: Sun, 26 Feb 2023 12:40:45 +0300 Subject: [PATCH] Handle zero payload --- lib/io_monitor/publishers/base_publisher.rb | 6 +++--- spec/io_monitor/publishers/shared_examples.rb | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/io_monitor/publishers/base_publisher.rb b/lib/io_monitor/publishers/base_publisher.rb index 12585cb..2c85676 100644 --- a/lib/io_monitor/publishers/base_publisher.rb +++ b/lib/io_monitor/publishers/base_publisher.rb @@ -16,15 +16,15 @@ def process_action(payload) (payload.keys - [:response]).each do |source| ratio = ratio(payload[:response], payload[source]) - if ratio < IoMonitor.config.warn_threshold - publish(source, ratio) - end + publish(source, ratio) if ratio < IoMonitor.config.warn_threshold end end private def ratio(response_size, io_size) + return 0 if io_size.to_f.zero? + response_size.to_f / io_size.to_f end end diff --git a/spec/io_monitor/publishers/shared_examples.rb b/spec/io_monitor/publishers/shared_examples.rb index 4559910..cecceb8 100644 --- a/spec/io_monitor/publishers/shared_examples.rb +++ b/spec/io_monitor/publishers/shared_examples.rb @@ -52,6 +52,17 @@ publisher.process_action(payload) end + + context "when io_payload_size is zero" do + let(:io_payload_size) { 0 } + let(:ratio) { 0 } + + it "calls .publish method with source and ratio" do + expect(publisher).to receive(:publish).with(io_kind, ratio) + + publisher.process_action(payload) + end + end end end end