From a598d0756756ddb46b884f45999545298426d4ce Mon Sep 17 00:00:00 2001 From: Jeffrey Phillips Freeman <the@jeffreyfreeman.me> Date: Mon, 7 Aug 2023 14:34:08 -0400 Subject: [PATCH] Prevented infinite loop on reading of data --- lib/apex/igate_tcp.rb | 4 ++-- spec/apex/igate_tcp_spec.rb | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/apex/igate_tcp.rb b/lib/apex/igate_tcp.rb index 3ccaeab..3217872 100644 --- a/lib/apex/igate_tcp.rb +++ b/lib/apex/igate_tcp.rb @@ -132,8 +132,8 @@ module Apex unless read_line.nil? @data_buffer << read_line end - ## TODO: Remove line below, only temporarly used to test - #read_more = false if @data_buffer.include? "\r\n" + ## Lets not read in more than one frame at a time so we dont get stuck in a loop + read_more = false if @data_buffer.include? "\r\n" rescue IO::WaitReadable read_more = false end diff --git a/spec/apex/igate_tcp_spec.rb b/spec/apex/igate_tcp_spec.rb index 2477cf6..342b673 100644 --- a/spec/apex/igate_tcp_spec.rb +++ b/spec/apex/igate_tcp_spec.rb @@ -13,7 +13,7 @@ ENCODED_FRAME_IGATE = "W2GMD-1>OMG,WIDE1-1,WIDE2-2:test_encode_frame" describe Apex::IGateTcp do let(:aprsis_socket) { double("aprsis_socket") } let(:igate_tcp) {Apex::IGateTcp.new("WI2ARD")} - + describe ".connect" do context "When attempting to connect to an igate" do it "The correct login message is sent" do @@ -22,17 +22,17 @@ describe Apex::IGateTcp do end end end - # describe ".read" do - # context "Given a valid frame on the igate" do - # it "decodes a frame from the stream" do - # allow(aprsis_socket).to receive(:puts) - # igate_tcp.connect(aprsis_socket_override: aprsis_socket) - # - # expect(aprsis_socket).to receive(:read_nonblock).and_return(ENCODED_FRAME_IGATE + "\r\n").and_raise(IO::WaitReadable) - # expect(igate_tcp.read()).to eql(DECODED_FRAME_IGATE) - # end - # end - # end + describe ".read" do + context "Given a valid frame on the igate" do + it "decodes a frame from the stream" do + allow(aprsis_socket).to receive(:puts) + igate_tcp.connect(aprsis_socket_override: aprsis_socket) + + expect(aprsis_socket).to receive(:read_nonblock).and_return(ENCODED_FRAME_IGATE + "\r\n") + expect(igate_tcp.read()).to eql(DECODED_FRAME_IGATE) + end + end + end describe ".write" do context "Given a a valid Frame" do it "correctly encodes and sends the frame" do -- GitLab