Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Adding mock server and test case to verify http timeout #649

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'minitest/autorun'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why there's a separate testcases folder? Don't think these tests would be picked up with the automated test runs, the test command follows a pattern that picks up the files in spec folder with filename ending with _spec.rb. Anything else, doesn't run.

require 'minitest/pride'
require "socket"
require 'bundler/setup'
Bundler.require :default

Thread.report_on_exception = false

class UnknownTimeoutError < StandardError; end

server = TCPServer.new("127.0.0.1", 4567)
Minitest.after_run { server.close }

socket = UDPSocket.new
socket.bind("127.0.0.1", 4568)
Minitest.after_run { socket.close }

class Minitest::Test
def assert_timeout(exception = UnknownTimeoutError, timeout: 1)
started_at = monotonic_time
ex = assert_raises(exception) { yield }
# test exact class
assert_equal exception, ex.class

time = monotonic_time - started_at
timeout = timeout..timeout + 0.5 unless timeout.is_a?(Range)
assert_includes timeout, time
end

def assert_threaded_timeout(exception = UnknownTimeoutError, timeout: 1, &block)
assert_timeout(exception, timeout: timeout) do
with_threads(&block)
end
end

def with_threads
threads = []
results = []
2.times do
threads << Thread.new { results << yield }
end
threads.each(&:join)
results
end

def connect_host
"10.255.255.1"
end

def connect_url
"http://#{connect_host}"
end

def read_host
"127.0.0.1"
end

def read_port
4567
end

def udp_port
4568
end

def read_host_and_port
"#{read_host}:#{read_port}"
end

def read_url
"http://#{read_host_and_port}"
end

def monotonic_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def ruby3?
RUBY_VERSION.to_i >= 3
end
end
18 changes: 18 additions & 0 deletions tests/twilio_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative "test_helper"


class TwilioTest < Minitest::Test
def test_connect
http_client = Twilio::HTTP::Client.new("http", connect_host, 80, timeout: 1)
assert_timeout(Twilio::REST::TwilioError) do
Twilio::REST::Client.new("sid", "token", nil, nil, http_client).api.account.calls.list
end
end

def test_read
http_client = Twilio::HTTP::Client.new("http", read_host, read_port, timeout: 1)
assert_timeout(Twilio::REST::TwilioError) do
Twilio::REST::Client.new("sid", "token", nil, nil, http_client).api.account.calls.list
end
end
end