<commit_before>require 'test_helper'
require 'fileutils'
require 'net/http'
require 'posix/spawn'
require 'support/hello_server'
require 'turnout_proxy'

class TestTurnoutProxy < TurnoutProxyTestCase
  include POSIX::Spawn
  include TurnoutProxy

  TEST_LOCK_FILE = PROJECT_ROOT.join("lock")

  def setup
    @@servers ||= begin
      HelloServer.new(8083, "S1")
      HelloServer.new(8084, "S2")
    end
    FileUtils.rm_rf TEST_LOCK_FILE
  end

  def teardown
    FileUtils.rm_rf TEST_LOCK_FILE
  end

  def with_running_proxy
    router = PROJECT_ROOT.join("bin/turnout_proxy")
    pid = spawn("ruby -Ilib #{router} --default-port=8083 --alternate-port=8084 --lock-file=#{TEST_LOCK_FILE}")
    sleep 0.5
    yield
    sleep 0.5
  ensure
    Process.kill(:SIGQUIT, pid)
  end

  test "proxies to default server when lock does not exist" do
    called = false
    with_running_proxy do
      called = Net::HTTP.get(URI("http://127.0.0.1:5678")) == "S1"
    end
    assert called, "Expected to proxy to server on 8083"
  end

  test "proxies to alternate server when lock file does exist" do
    FileUtils.touch TEST_LOCK_FILE
    called = false
    with_running_proxy do
      called = Net::HTTP.get(URI("http://127.0.0.1:5678")) == "S2"
    end
    assert called, "Expected to proxy to server on 8084"
  end

end
<commit_msg>Test for faulty connection<commit_after>require 'test_helper'
require 'fileutils'
require 'net/http'
require 'posix/spawn'
require 'support/hello_server'
require 'turnout_proxy'

class TestTurnoutProxy < TurnoutProxyTestCase
  include POSIX::Spawn
  include TurnoutProxy

  TEST_LOCK_FILE = PROJECT_ROOT.join("lock")

  def setup
    @@servers ||= begin
      HelloServer.new(8083, "S1")
      HelloServer.new(8084, "S2")
    end
    FileUtils.rm_rf TEST_LOCK_FILE
  end

  def teardown
    FileUtils.rm_rf TEST_LOCK_FILE
  end

  def with_running_proxy(default_port = 8083, alternate_port = 8084)
    router = PROJECT_ROOT.join("bin/turnout_proxy")
    pid = spawn("ruby -Ilib #{router} --default-port=#{default_port} --alternate-port=#{alternate_port} --lock-file=#{TEST_LOCK_FILE}")
    sleep 0.5
    yield pid
    sleep 0.5
  ensure
    Process.kill(:SIGQUIT, pid)
  end

  test "proxies to default server when lock does not exist" do
    called = false
    with_running_proxy do
      called = Net::HTTP.get(URI("http://127.0.0.1:5678")) == "S1"
    end
    assert called, "Expected to proxy to server on 8083"
  end

  test "proxies to alternate server when lock file does exist" do
    FileUtils.touch TEST_LOCK_FILE
    called = false
    with_running_proxy do
      called = Net::HTTP.get(URI("http://127.0.0.1:5678")) == "S2"
    end
    assert called, "Expected to proxy to server on 8084"
  end

  test "doesn't fail if destination server isn't responding" do
    @pid = nil
    with_running_proxy(8085) do |pid|
      @pid = pid
      Net::HTTP.get(URI("http://127.0.0.1:5678")) rescue nil
    end
    Process.kill 0, @pid
  end

end

{'fraction_non_alphanumeric': 0.10759919300605246, 'fraction_numerical': 0.04001344989912576, 'mean_word_length': 3.375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4639624', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1018, 'n_words': 269}