File: timeout_spec.rb

package info (click to toggle)
ruby-excon 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: ruby: 7,970; makefile: 5
file content (83 lines) | stat: -rw-r--r-- 2,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'spec_helper'

describe Excon::Connection do
  include_context('test server', :webrick, 'timeout.ru', before: :start, after: :stop)

  let(:read_timeout) { 60 }
  let(:conn) do
    Excon::Connection.new(host: '127.0.0.1',
                          hostname: '127.0.0.1',
                          nonblock: nonblock,
                          port: 9292,
                          scheme: 'http',
                          timeout: timeout,
                          read_timeout: read_timeout)
  end

  context "blocking connection" do
    let (:nonblock) { false }

    context 'when timeout is not set' do
      let(:timeout) { nil }
  
      it 'does not error' do
        expect(conn.request(:path => '/no-timeout').status).to eq(200)
      end
    end

    context 'when timeout is not triggered' do
      let(:timeout) { 1 }

      it 'does not error' do
        expect(conn.request(:path => '/no-timeout').status).to eq(200)        
      end
    end
  
    context 'when timeout is triggered' do
      let(:read_timeout) { 0.005 }
      let(:timeout) { 0.001 }
  
      it 'does not raise' do
        # raising a read timeout to keep tests fast
        expect { conn.request(:path => '/timeout') }.to raise_error(Excon::Error::Timeout, 'read timeout reached')
      end
    end
  end

  context "non-blocking connection" do
    let (:nonblock) { true }

    context 'when timeout is not set' do
      let(:timeout) { nil }
  
      it 'does not error' do
        expect(conn.request(:path => '/no-timeout').status).to eq(200)        
      end
    end

    context 'when timeout is not triggered' do
      let(:timeout) { 1 }
  
      it 'does not error' do
        expect(conn.request(:path => '/no-timeout').status).to eq(200)        
      end
    end
  
    context 'when timeout is triggered' do
      let(:timeout) { 0.001 }
  
      it 'returns a request Excon::Error::Timeout' do
        expect { conn.request(:path => '/timeout') }.to raise_error(Excon::Error::Timeout, 'request timeout reached')
      end
    end

    context 'when read timeout is triggered' do
      let(:read_timeout) { 0.001 }
      let(:timeout) { 5 }
  
      it 'returns a read Excon::Error::Timeout' do
        expect { conn.request(:path => '/timeout') }.to raise_error(Excon::Error::Timeout, 'read timeout reached')
      end
    end
  end
end