~ubuntu-branches/ubuntu/utopic/ruby-excon/utopic

« back to all changes in this revision

Viewing changes to tests/idempotent_tests.rb

  • Committer: Package Import Robot
  • Author(s): Laurent Bigonville
  • Date: 2012-03-07 16:36:21 UTC
  • Revision ID: package-import@ubuntu.com-20120307163621-bztj2b8860dxpzs7
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Shindo.tests('Excon request idempotencey') do
 
2
 
 
3
  before do
 
4
    @connection = Excon.new('http://127.0.0.1:9292', :mock => true)
 
5
  end
 
6
 
 
7
  after do
 
8
    # flush any existing stubs after each test
 
9
    Excon.stubs.clear
 
10
  end
 
11
 
 
12
  tests("Non-idempotent call with an erroring socket").raises(Excon::Errors::SocketError) do
 
13
    run_count = 0
 
14
    Excon.stub({:method => :get}) { |params|
 
15
      run_count += 1
 
16
      if run_count <= 3 # First 3 calls fail.
 
17
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
18
      else
 
19
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
20
      end
 
21
    }
 
22
 
 
23
    @connection.request(:method => :get, :path => '/some-path')
 
24
  end
 
25
 
 
26
  tests("Idempotent request with socket erroring first 3 times").returns(200) do
 
27
    run_count = 0
 
28
    Excon.stub({:method => :get}) { |params|
 
29
      run_count += 1
 
30
      if run_count <= 3 # First 3 calls fail.
 
31
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
32
      else
 
33
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
34
      end
 
35
    }
 
36
 
 
37
    response = @connection.request(:method => :get, :idempotent => true, :path => '/some-path')
 
38
    response.status
 
39
  end
 
40
 
 
41
  tests("Idempotent request with socket erroring first 5 times").raises(Excon::Errors::SocketError) do
 
42
    run_count = 0
 
43
    Excon.stub({:method => :get}) { |params|
 
44
      run_count += 1
 
45
      if run_count <= 5 # First 5 calls fail.
 
46
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
47
      else
 
48
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
49
      end
 
50
    }
 
51
 
 
52
    response = @connection.request(:method => :get, :idempotent => true, :path => '/some-path')
 
53
    response.status
 
54
  end
 
55
 
 
56
  tests("Lowered retry limit with socket erroring first time").returns(200) do
 
57
    run_count = 0
 
58
    Excon.stub({:method => :get}) { |params|
 
59
      run_count += 1
 
60
      if run_count <= 1 # First call fails.
 
61
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
62
      else
 
63
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
64
      end
 
65
    }
 
66
 
 
67
    response = @connection.request(:method => :get, :idempotent => true, :path => '/some-path', :retry_limit => 2)
 
68
    response.status
 
69
  end
 
70
 
 
71
  tests("Lowered retry limit with socket erroring first 3 times").raises(Excon::Errors::SocketError) do
 
72
    run_count = 0
 
73
    Excon.stub({:method => :get}) { |params|
 
74
      run_count += 1
 
75
      if run_count <= 3 # First 3 calls fail.
 
76
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
77
      else
 
78
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
79
      end
 
80
    }
 
81
 
 
82
    response = @connection.request(:method => :get, :idempotent => true, :path => '/some-path', :retry_limit => 2)
 
83
    response.status
 
84
  end
 
85
 
 
86
  tests("Raised retry limit with socket erroring first 5 times").returns(200) do
 
87
    run_count = 0
 
88
    Excon.stub({:method => :get}) { |params|
 
89
      run_count += 1
 
90
      if run_count <= 5 # First 5 calls fail.
 
91
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
92
      else
 
93
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
94
      end
 
95
    }
 
96
 
 
97
    response = @connection.request(:method => :get, :idempotent => true, :path => '/some-path', :retry_limit => 8)
 
98
    response.status
 
99
  end
 
100
 
 
101
  tests("Raised retry limit with socket erroring first 9 times").raises(Excon::Errors::SocketError) do
 
102
    run_count = 0
 
103
    Excon.stub({:method => :get}) { |params|
 
104
      run_count += 1
 
105
      if run_count <= 9 # First 9 calls fail.
 
106
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
107
      else
 
108
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
109
      end
 
110
    }
 
111
 
 
112
    response = @connection.request(:method => :get, :idempotent => true, :path => '/some-path', :retry_limit => 8)
 
113
    response.status
 
114
  end
 
115
 
 
116
  tests("Retry limit in constructor with socket erroring first 5 times").returns(200) do
 
117
    run_count = 0
 
118
    Excon.stub({:method => :get}) { |params|
 
119
      run_count += 1
 
120
      if run_count <= 5 # First 5 calls fail.
 
121
        raise Excon::Errors::SocketError.new(Exception.new "Mock Error")
 
122
      else
 
123
        {:body => params[:body], :headers => params[:headers], :status => 200}
 
124
      end
 
125
    }
 
126
 
 
127
    response = @connection.request(:method => :get, :idempotent => true, :path => '/some-path', :retry_limit => 6)
 
128
    response.status
 
129
  end
 
130
 
 
131
end