Skip to content

Instantly share code, notes, and snippets.

@aduane
Created May 23, 2017 22:23
Show Gist options
  • Save aduane/f3625297039634400adc859a2634143e to your computer and use it in GitHub Desktop.
Save aduane/f3625297039634400adc859a2634143e to your computer and use it in GitHub Desktop.
rspec spec/ --require ./progress_stream_formatter.rb --format ProgressStreamFormatter
require "net/http"
require "uri"
require 'json'
class ProgressStreamFormatter
RSpec::Core::Formatters.register self, :dump_summary, :close, :example_passed, :example_failed, :example_pending, :start
def initialize output
@output = output
@api_key = ENV['PROGRESS_STREAM_API_KEY']
@threads = []
end
def start notification
uri = URI.parse("https://pacific-fortress-60861.herokuapp.com/tasks")
name = "rspec test run #{Time.new.strftime('%F %r')}"
if [ENV['PARALLEL_TEST_GROUPS'], ENV['TEST_ENV_NUMBER']].all?
number = ENV['TEST_ENV_NUMBER']
number = '1' if number == ''
total = ENV['PARALLEL_TEST_GROUPS']
name << " Process #{number}/#{total}"
end
response = Net::HTTP.post_form(uri, {"label" => name,
"api_key" => @api_key,
"numerator" => 0,
"denominator" => notification.count,
"unit" => "tests"
})
parsed = JSON.parse response.body
@task_id = parsed["task_id"]
end
def example_passed notification # ExampleNotification
put_update
end
def example_failed notification # FailedExampleNotification
put_update
end
def example_pending notification # ExampleNotification
put_update
end
def dump_summary notification # SummaryNotification
end
def close notification # NullNotification
@threads.each{|t| t.join}
end
private
def put_update
@threads << Thread.new do
headers = {"Content-Type" => "application/json"}
body = {"increment_by" => 1, "api_key" => @api_key}.to_json
uri = URI.parse("http://pacific-fortress-60861.herokuapp.com/tasks/#{@task_id}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new(uri.request_uri)
headers.keys.each do |key|
request[key] = headers[key]
end
request.body = body
http.request(request)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment