I have a controller which includes the Rails ActionController::Live
module. I am displaying the contents of a logfile, which is being read using FileTail
gem, and using SSE
from ActionController::Live
like so:
class LogsController < ApplicationController
include ActionController::Live
def live
response.headers['Content-Type'] = 'text/event-stream'
sse = SSE.new(response.stream, event: 'queries')
File.open(logfile_location) do |log|
log.extend(File::Tail)
log.interval = 1
log.backward(10)
log.tail {|line| sse.write line}
end
rescue => e
Rails.logger.info "Error Message:: #{e.message}"
ensure
sse.close
end
end
I want to test the live
action using Rspec
. This is what I currently have:
before { get :live }
it { expect(response.headers['Content-Type']).to eq("text/event-stream") }
after {response.stream.close unless response.stream.closed? }
if I don't have the line with after
in place, the spec passes but it just keeps on listening and the connection is never closed, hence the specs never finish and you have to kill them manually.
If I have the after
line, it passes sometimes, but most of the times it throws an exception, and the specs fail.
fatal:
No live threads left. Deadlock?
Is there a way I can make this work? Maybe there's a specific way this has to be tested which am unable to find anywhere.