Skip to content

Commit 45622ce

Browse files
guyboertjejordansissel
authored andcommitted
refactor some operations to enable better testing
Fixes #23
1 parent b78d6b9 commit 45622ce

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/logstash/inputs/log4j.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,29 @@ def run(output_queue)
153153
if server?
154154
while !stop?
155155
Thread.start(@server_socket.accept) do |s|
156-
s.instance_eval { class << self; include ::LogStash::Util::SocketPeer end }
156+
add_socket_mixin(s)
157157
@logger.debug? && @logger.debug("Accepted connection", :client => s.peer,
158158
:server => "#{@host}:#{@port}")
159159
handle_socket(s, output_queue)
160160
end # Thread.start
161161
end # loop
162162
else
163163
while !stop?
164-
client_socket = TCPSocket.new(@host, @port)
165-
client_socket.instance_eval { class << self; include ::LogStash::Util::SocketPeer end }
164+
client_socket = build_client_socket
166165
@logger.debug? && @logger.debug("Opened connection", :client => "#{client_socket.peer}")
167166
handle_socket(client_socket, output_queue)
168167
end # loop
169168
end
170169
rescue IOError
171170
end # def run
171+
172+
def build_client_socket
173+
s = TCPSocket.new(@host, @port)
174+
add_socket_mixin(s)
175+
s
176+
end
177+
178+
def add_socket_mixin(socket)
179+
socket.instance_eval { class << self; include ::LogStash::Util::SocketPeer end }
180+
end
172181
end # class LogStash::Inputs::Log4j

spec/inputs/log4j_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
allow(socket).to receive(:peer).and_return("localhost")
2828
allow(socket).to receive(:close).and_return(true)
2929
allow(ois).to receive(:readObject).and_return({})
30-
allow(TCPSocket).to receive(:new).and_return(socket)
30+
allow(subject).to receive(:build_client_socket).and_return(socket)
3131
expect(subject).to receive(:socket_to_inputstream).with(socket).and_return(ois)
3232
expect(subject).to receive(:create_event).and_return(LogStash::Event.new).at_least(:once)
3333
end

0 commit comments

Comments
 (0)