Sha256: 59bf9421936cbac25c583d6c16d9a9515c7bd9f12778821a79960e20c9e6fa0a

Contents?: true

Size: 1.92 KB

Versions: 39

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Stripe
  class StripeMock
    include Singleton

    PATH_SPEC = "#{::File.dirname(__FILE__)}/openapi/spec3.json"
    PATH_FIXTURES = "#{::File.dirname(__FILE__)}/openapi/fixtures3.json"

    @pid = nil
    @port = -1

    # Starts stripe-mock, if necessary. Returns the port on which stripe-mock is listening.
    def self.start
      unless ::File.exist?(PATH_SPEC)
        port = ENV["STRIPE_MOCK_PORT"] || 12_111
        puts("No custom spec file found, assuming stripe-mock is already running on port #{port}")
        return port
      end

      unless @pid.nil?
        puts("stripe-mock already running on port #{@port}")
        return @port
      end

      puts("Starting stripe-mock...")

      @stdout, @child_stdout = ::IO.pipe
      @stderr, @child_stderr = ::IO.pipe

      @pid = ::Process.spawn(
        %w[stripe-mock stripe-mock],
        "-http-port",
        "0", # have stripe-mock select a port
        "-spec",
        PATH_SPEC,
        "-fixtures",
        PATH_FIXTURES,
        out: @child_stdout,
        err: @child_stderr
      )

      [@child_stdout, @child_stderr].each(&:close)

      # Look for port in "Listening for HTTP on port: 50602"
      buffer = ""
      loop do
        buffer += @stdout.readpartial(4096)
        if (matches = buffer.match(/ port: (\d+)/))
          @port = matches[1]
          break
        end
        sleep(0.1)
      end

      status = (::Process.wait2(@pid, ::Process::WNOHANG) || []).last
      if status.nil?
        puts("Started stripe-mock; PID = #{@pid}, port = #{@port}")
      else
        abort("stripe-mock terminated early: #{status}")
      end

      @port
    end

    # Stops stripe-mock, if necessary.
    def self.stop
      return if @pid.nil?

      puts("Stopping stripe-mock...")
      ::Process.kill(:SIGTERM, @pid)
      ::Process.waitpid2(@pid)
      @pid = nil
      @port = -1
      puts("Stopped stripe-mock")
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
stripe-5.31.0 test/stripe_mock.rb
stripe-5.30.0 test/stripe_mock.rb
stripe-5.29.1 test/stripe_mock.rb
stripe-5.29.0 test/stripe_mock.rb
stripe-5.28.0 test/stripe_mock.rb
stripe-5.27.0 test/stripe_mock.rb
stripe-5.26.0 test/stripe_mock.rb
stripe-5.25.0 test/stripe_mock.rb
stripe-5.24.0 test/stripe_mock.rb
stripe-5.23.1 test/stripe_mock.rb
stripe-5.23.0 test/stripe_mock.rb
stripe-5.22.0 test/stripe_mock.rb
stripe-5.21.0 test/stripe_mock.rb
stripe-5.20.0 test/stripe_mock.rb
stripe-5.19.0 test/stripe_mock.rb
stripe-5.18.0 test/stripe_mock.rb
stripe-5.17.0 test/stripe_mock.rb
stripe-5.16.0 test/stripe_mock.rb
stripe-5.15.0 test/stripe_mock.rb
stripe-5.14.0 test/stripe_mock.rb