<commit_before># encoding: UTF-8

describe Stompede::Stomplet do
  integration_test!

  describe "generic client errors" do
    it "terminates the connection on parser errors" do
      send_message(client_io, "INVALID_COMMAND", "foo" => "Bar")
      client_io.should receive_error(StompParser::ParseError, "unexpected I in chunk (\" -->I<-- NVALID_COMMAND\")")
      app_monitor.wait_for_terminate
    end
  end

  describe "#on_open" do
    it "is called when a socket is opened" do
      latch.receive(:open)
      app.should be_alive
    end
  end

  describe "#on_close" do
    it "is called when a socket is closed" do
      client_io.close
      latch.receive(:close)
      app_monitor.wait_for_terminate
    end

    it "is called even when app throws an error", error: :open do
      latch.receive(:close)
      client_io.should receive_error(MooError, "MOOOO!")
    end

    it "closes socket even when on_close dies", error: [:open, :close] do
      latch.receive(:close)
      client_io.should receive_error(MooError, "MOOOO!")
    end
  end

  describe "#on_connect" do
    it "is called when a client sends a CONNECT frame" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")

      message = latch.receive(:connect).first
      message["foo"].should eq("Bar")
    end

    it "is called when a client sends a STOMP frame" do
      send_message(client_io, "STOMP", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")

      message = latch.receive(:connect).first
      message["foo"].should eq("Bar")
    end

    it "does not send a receipt, because the STOMP spec says so" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "receipt" => "1234")
      latch.receive(:connect)
      message = parse_message(client_io)
      message.command.should eq("CONNECTED")
      message["receipt-id"].should be_nil
      client_io.should be_an_empty_socket
    end

    it "replies with a CONNECTED frame when the handler succeeds" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")
      message = parse_message(client_io)
      message.command.should eq("CONNECTED")
      message["version"].should eq("1.2")
      message["server"].should eq("Stompede/#{Stompede::VERSION}")
      message["session"].should match(/\A[a-f0-9\-]{36}\z/)
    end

    it "replies with an ERROR frame when the handler fails", error: :connect do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")
      client_io.should receive_error(MooError, "MOOOO!")
      expect { app_monitor.wait_for_crash! }.to raise_error(MooError, "MOOOO!")
    end

    it "sends a client error when client does not support STOMP version #{Stompede::STOMP_VERSION}" do
      send_message(client_io, "CONNECT", "accept-version" => "1.0,1.1")
      client_io.should receive_error(Stompede::ClientError, "client must support STOMP version #{Stompede::STOMP_VERSION}", version: Stompede::STOMP_VERSION)
      app_monitor.wait_for_terminate
    end

    it "crashes when the client sends another frame before the CONNECT frame" do
      send_message(client_io, "SEND", destination: "foo")
      client_io.should receive_error(Stompede::ClientError, "client is not connected")
    end

    it "crashes when the client sends multiple CONNECT frames" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION)
      parse_message(client_io).command.should eq("CONNECTED")
      send_message(client_io, "STOMP", "accept-version" => Stompede::STOMP_VERSION)
      client_io.should receive_error(Stompede::ClientError, "must not send CONNECT or STOMP frame after connection is already open")
    end

    context "with a connect timeout configured" do
      let(:connector) { Stompede::Connector.new(app_klass, connect_timeout: 0.010) }

      it "replies with a CONNECTED frame when the handler replies promptly" do<commit_msg>Check that nil connect timeout works<commit_after># encoding: UTF-8

describe Stompede::Stomplet do
  integration_test!

  describe "generic client errors" do
    it "terminates the connection on parser errors" do
      send_message(client_io, "INVALID_COMMAND", "foo" => "Bar")
      client_io.should receive_error(StompParser::ParseError, "unexpected I in chunk (\" -->I<-- NVALID_COMMAND\")")
      app_monitor.wait_for_terminate
    end
  end

  describe "#on_open" do
    it "is called when a socket is opened" do
      latch.receive(:open)
      app.should be_alive
    end
  end

  describe "#on_close" do
    it "is called when a socket is closed" do
      client_io.close
      latch.receive(:close)
      app_monitor.wait_for_terminate
    end

    it "is called even when app throws an error", error: :open do
      latch.receive(:close)
      client_io.should receive_error(MooError, "MOOOO!")
    end

    it "closes socket even when on_close dies", error: [:open, :close] do
      latch.receive(:close)
      client_io.should receive_error(MooError, "MOOOO!")
    end
  end

  describe "#on_connect" do
    it "is called when a client sends a CONNECT frame" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")

      message = latch.receive(:connect).first
      message["foo"].should eq("Bar")
    end

    it "is called when a client sends a STOMP frame" do
      send_message(client_io, "STOMP", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")

      message = latch.receive(:connect).first
      message["foo"].should eq("Bar")
    end

    it "does not send a receipt, because the STOMP spec says so" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "receipt" => "1234")
      latch.receive(:connect)
      message = parse_message(client_io)
      message.command.should eq("CONNECTED")
      message["receipt-id"].should be_nil
      client_io.should be_an_empty_socket
    end

    it "replies with a CONNECTED frame when the handler succeeds" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")
      message = parse_message(client_io)
      message.command.should eq("CONNECTED")
      message["version"].should eq("1.2")
      message["server"].should eq("Stompede/#{Stompede::VERSION}")
      message["session"].should match(/\A[a-f0-9\-]{36}\z/)
    end

    it "replies with an ERROR frame when the handler fails", error: :connect do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")
      client_io.should receive_error(MooError, "MOOOO!")
      expect { app_monitor.wait_for_crash! }.to raise_error(MooError, "MOOOO!")
    end

    it "sends a client error when client does not support STOMP version #{Stompede::STOMP_VERSION}" do
      send_message(client_io, "CONNECT", "accept-version" => "1.0,1.1")
      client_io.should receive_error(Stompede::ClientError, "client must support STOMP version #{Stompede::STOMP_VERSION}", version: Stompede::STOMP_VERSION)
      app_monitor.wait_for_terminate
    end

    it "crashes when the client sends another frame before the CONNECT frame" do
      send_message(client_io, "SEND", destination: "foo")
      client_io.should receive_error(Stompede::ClientError, "client is not connected")
    end

    it "crashes when the client sends multiple CONNECT frames" do
      send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION)
      parse_message(client_io).command.should eq("CONNECTED")
      send_message(client_io, "STOMP", "accept-version" => Stompede::STOMP_VERSION)
      client_io.should receive_error(Stompede::ClientError, "must not send CONNECT or STOMP frame after connection is already open")
    end

    context "with a nil timeout" do
      let(:connector) { Stompede::Connector.new(app_klass, connect_timeout: nil) }

      it "does not time out (we can't really test this in any sensible way)" do
        send_message(client_io, "CONNECT", "accept-version" => Stompede::STOMP_VERSION, "foo" => "Bar")
        parse_message(client_io).command.should eq("CONNECTED")
      end
    end

    context "with a connect timeout configured" do
      let(:connector) { Stompede::Connector.new(app_klass, connect_timeout: 0.010) }

      it "replies with a CONNECTED frame when the handler replies promptly" do
{'fraction_non_alphanumeric': 0.1150746622422375, 'fraction_numerical': 0.004503436833372837, 'mean_word_length': 3.7651044607566346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3492992', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2706, 'n_words': 731}