CodeSampleX

サンプル

faraday-net_http 3.4.4: Handle the asymmetric exception wrapping and phase distribution between Faraday timeout options and Faraday::Adapter::NetHttp

検証済みサンプル — gem faraday-net_http 3.4.4: Handle the asymmetric exception wrapping and phase distribution between Faraday timeout options and…

sha256:d2ab361b5a26aaca1485c8c63f2f849804f7725bbab6574a58d895abd1d2a4e1

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
2
宣言された環境 ruby linux x64 ruby ruby rubygems

検証実行環境

環境 コントラクト ステージ 実行日
ruby 3 · linux debian/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-17
ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-18

ケース

HOW
ゴール
Handle the asymmetric exception wrapping and phase distribution between Faraday timeout options and Faraday::Adapter::NetHttp
パッケージ
シンボル
  • Faraday::Adapter::NetHttp#call
  • Faraday::Adapter::NetHttp#configure_request
  • Faraday::Adapter::NetHttp::NET_HTTP_EXCEPTIONS
環境
ruby
作成日
2026-08-17T02:41:37Z

コントラクト

  1. Net::OpenTimeout raises Faraday::ConnectionFailed rather than Faraday::TimeoutError because NET_HTTP_EXCEPTIONS intercepts open timeouts before Timeout::Error rescue handlers run.
  2. Net::ReadTimeout and Net::WriteTimeout raise Faraday::TimeoutError wrapping the underlying Net::ProtocolError timeout exception.
  3. Setting a general request timeout distributes the duration to open_timeout, read_timeout, and write_timeout on Net::HTTP individually instead of enforcing a single total deadline.
  4. Specific timeout options override their respective phase on Net::HTTP while leaving other phases with the general timeout fallback.
  5. The adapter configuration block executes after request options are applied and overrides Net::HTTP timeout properties.

ファイル

  • Gemfile
  • Gemfile.lock
  • NOTES.md
  • csx.json
  • test/contract.rb

ソースアーティファクトをダウンロード (tar.gz)

ソース

Gemfile
source "https://rubygems.org"

gem "faraday-net_http", "3.4.4"
gem "faraday", "2.14.3"
gem "minitest", "5.27.0"
Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    faraday (2.14.3)
      faraday-net_http (>= 2.0, < 3.5)
      json
      logger
    faraday-net_http (3.4.4)
      net-http (~> 0.5)
    json (2.21.2)
    logger (1.7.0)
    minitest (5.27.0)
    net-http (0.9.1)
      uri (>= 0.11.1)
    uri (1.1.1)

PLATFORMS
  ruby
  x64-mingw-ucrt

DEPENDENCIES
  faraday (= 2.14.3)
  faraday-net_http (= 3.4.4)
  minitest (= 5.27.0)

CHECKSUMS
  bundler (4.0.18) sha256=02d9a17429de1847b4e0c9f27a9ee4b20c0a74c0a641b4e77195d6019e3618ac
  faraday (2.14.3) sha256=1882247e6766615c8220b4392bf1d27f6ebb63d8e28267587cef1fb0bf37f278
  faraday-net_http (3.4.4) sha256=0e78af151747ed1b00f33e25973b4bc220d7f16c00c39676817c8b12331eb588
  json (2.21.2) sha256=1f1d3b7cf2b3ba1a69beca0bb6db13d5438b80bff3cd54cdaaa620b9b07c1c6a
  logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203
  minitest (5.27.0) sha256=2d3b17f8a36fe7801c1adcffdbc38233b938eb0b4966e97a6739055a45fa77d5
  net-http (0.9.1) sha256=25ba0b67c63e89df626ed8fac771d0ad24ad151a858af2cc8e6a716ca4336996
  uri (1.1.1) sha256=379fa58d27ffb1387eaada68c749d1426738bd0f654d812fcc07e7568f5c57c6

BUNDLED WITH
  4.0.18
NOTES.md
# Notes

### What search_known_solution answered
`search_known_solution` for `pkg:gem/faraday-net_http` returned three existing samples: `sha256:ff98862d1f5a38ed19d00c8029788fb31dc64d355f52329873c75e52a6539e69` (hostless absolute URI handling), `sha256:510a4d8ab52d47b81fe0d197ce5522fc1baeff6b3b0a52ff2d1dd16657923da9` (type and serialization boundaries), and `sha256:0389cb49f89e735138cf19ca73666e18758518dfadfefff7d6716c570f1ec588` (version boundaries for proxy SSL, chain certs, and empty body mutability). None of them covered the interaction between timeout configuration options and exception mapping in `Faraday::Adapter::NetHttp`.

### What a model would have written instead
A model would have written error handling assuming that `rescue Faraday::TimeoutError` catches all timeout failures resulting from configured Faraday timeouts (`timeout`, `open_timeout`, `read_timeout`, `write_timeout`), and that setting `request: { timeout: 5 }` enforces a single overall 5-second deadline across the entire request lifecycle.

### How the wrong version fails
Fails loudly by escaping error handlers when open timeouts occur (raising uncaught `Faraday::ConnectionFailed` where `Faraday::TimeoutError` was expected), and fails silently with elapsed times exceeding the configured timeout duration because each phase (`open_timeout`, `read_timeout`, `write_timeout`) receives the full timeout duration independently rather than sharing a cumulative budget.
csx.json
{"case":{"believed":"Faraday::Adapter::NetHttp wraps all request timeout failures as Faraday::TimeoutError regardless of whether the connection open or response read phase timed out.","caseId":"case:sha256:a2cf8d87e48436c66dc334f4d5ac04644c2d616b1d3e2ccd572284c4c8746754","contract":["Net::OpenTimeout raises Faraday::ConnectionFailed rather than Faraday::TimeoutError because NET_HTTP_EXCEPTIONS intercepts open timeouts before Timeout::Error rescue handlers run.","Net::ReadTimeout and Net::WriteTimeout raise Faraday::TimeoutError wrapping the underlying Net::ProtocolError timeout exception.","Setting a general request timeout distributes the duration to open_timeout, read_timeout, and write_timeout on Net::HTTP individually instead of enforcing a single total deadline.","Specific timeout options override their respective phase on Net::HTTP while leaving other phases with the general timeout fallback.","The adapter configuration block executes after request options are applied and overrides Net::HTTP timeout properties."],"goal":"Handle the asymmetric exception wrapping and phase distribution between Faraday timeout options and Faraday::Adapter::NetHttp","kind":"HOW","packages":["pkg:gem/faraday-net_http@3.4.4"],"schemaVersion":1,"symbols":["Faraday::Adapter::NetHttp#call","Faraday::Adapter::NetHttp#configure_request","Faraday::Adapter::NetHttp::NET_HTTP_EXCEPTIONS"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"rubygems","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/faraday-net_http@3.4.4"],"schemaVersion":1,"symbols":["Faraday::Adapter::NetHttp#call","Faraday::Adapter::NetHttp#configure_request","Faraday::Adapter::NetHttp::NET_HTTP_EXCEPTIONS"],"verifierAdapter":"rubygems@1"}
test/contract.rb
# frozen_string_literal: true

require "minitest/autorun"
require "faraday"
require "faraday/net_http"
require "socket"
require "uri"

class FaradayNetHttpTimeoutContractTest < Minitest::Test
  def setup
    @adapter = Faraday::Adapter::NetHttp.new
  end

  def test_open_timeout_raises_connection_failed_instead_of_timeout_error
    env = Faraday::Env.from(
      url: URI("http://example.org/test"),
      method: :get,
      request_headers: Faraday::Utils::Headers.new,
      request: Faraday::RequestOptions.new
    )

    @adapter.define_singleton_method(:perform_request) do |_, _|
      raise Net::OpenTimeout, "execution expired"
    end

    error = assert_raises(Faraday::ConnectionFailed) do
      @adapter.call(env)
    end

    refute error.is_a?(Faraday::TimeoutError), "OpenTimeout must raise Faraday::ConnectionFailed, not Faraday::TimeoutError"
    assert_equal Net::OpenTimeout, error.wrapped_exception.class
    assert Faraday::Adapter::NetHttp::NET_HTTP_EXCEPTIONS.include?(Net::OpenTimeout)
  end

  def test_read_and_write_timeouts_raise_timeout_error
    env = Faraday::Env.from(
      url: URI("http://example.org/test"),
      method: :get,
      request_headers: Faraday::Utils::Headers.new,
      request: Faraday::RequestOptions.new
    )

    adapter_read = Faraday::Adapter::NetHttp.new
    adapter_read.define_singleton_method(:perform_request) do |_, _|
      raise Net::ReadTimeout, "execution expired"
    end

    read_err = assert_raises(Faraday::TimeoutError) do
      adapter_read.call(env)
    end
    assert_equal Net::ReadTimeout, read_err.wrapped_exception.class

    adapter_write = Faraday::Adapter::NetHttp.new
    adapter_write.define_singleton_method(:perform_request) do |_, _|
      raise Net::WriteTimeout, "execution expired"
    end

    write_err = assert_raises(Faraday::TimeoutError) do
      adapter_write.call(env)
    end
    assert_equal Net::WriteTimeout, write_err.wrapped_exception.class
  end

  def test_timeout_option_fans_out_to_all_three_net_http_timeouts
    env = Faraday::Env.from(
      url: URI("http://example.org/test"),
      method: :get,
      request_headers: Faraday::Utils::Headers.new,
      request: Faraday::RequestOptions.from(timeout: 7)
    )

    http = @adapter.send(:build_connection, env)
    assert_equal 7, http.open_timeout
    assert_equal 7, http.read_timeout
    assert_equal 7, http.write_timeout
  end

  def test_specific_timeout_options_override_individual_phases
    env = Faraday::Env.from(
      url: URI("http://example.org/test"),
      method: :get,
      request_headers: Faraday::Utils::Headers.new,
      request: Faraday::RequestOptions.from(timeout: 10, open_timeout: 2, read_timeout: 4)
    )

    http = @adapter.send(:build_connection, env)
    assert_equal 2, http.open_timeout
    assert_equal 4, http.read_timeout
    assert_equal 10, http.write_timeout
  end

  def test_adapter_block_executes_after_request_options_and_overrides_timeouts
    adapter_with_block = Faraday::Adapter::NetHttp.new(nil) do |http|
      http.read_timeout = 30
      http.open_timeout = 3
    end

    env = Faraday::Env.from(
      url: URI("http://example.org/test"),
      method: :get,
      request_headers: Faraday::Utils::Headers.new,
      request: Faraday::RequestOptions.from(timeout: 10, read_timeout: 5)
    )

    http = adapter_with_block.send(:build_connection, env)
    assert_equal 3, http.open_timeout
    assert_equal 30, http.read_timeout
    assert_equal 10, http.write_timeout
  end

  def test_live_socket_read_timeout_bubbles_as_faraday_timeout_error
    server = TCPServer.new("127.0.0.1", 0)
    port = server.addr[1]

    thread = Thread.new do
      sock = server.accept rescue nil
      if sock
        sock.gets
        sleep 0.3
        sock.close rescue nil
      end
    end

    conn = Faraday.new("http://127.0.0.1:#{port}") do |f|
      f.adapter :net_http
    end

    err = assert_raises(Faraday::TimeoutError) do
      conn.get("/") do |req|
        req.options.read_timeout = 0.05
      end
    end

    assert_equal Net::ReadTimeout, err.wrapped_exception.class
  ensure
    server&.close rescue nil
    thread&.kill rescue nil
  end
end

オリジンシーダー

csx-seed