From 27fa046dc14fbb5e6e186c75be7eabffec19a01f Mon Sep 17 00:00:00 2001 From: AsabuHere Date: Tue, 25 Apr 2023 13:42:15 +0530 Subject: [PATCH] Adding mock server for ruby testing and test case to verify http timeout functionality --- tests/test_helper.rb | 81 ++++++++++++++++++++++++++++++++++++++++++++ tests/twilio_test.rb | 18 ++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/test_helper.rb create mode 100644 tests/twilio_test.rb diff --git a/tests/test_helper.rb b/tests/test_helper.rb new file mode 100644 index 000000000..bf15757cd --- /dev/null +++ b/tests/test_helper.rb @@ -0,0 +1,81 @@ +require 'minitest/autorun' +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 \ No newline at end of file diff --git a/tests/twilio_test.rb b/tests/twilio_test.rb new file mode 100644 index 000000000..6bb4ac710 --- /dev/null +++ b/tests/twilio_test.rb @@ -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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy