File: test_net_http_persistent_timed_stack_multi.rb

package info (click to toggle)
ruby-net-http-persistent 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: ruby: 1,703; makefile: 4
file content (151 lines) | stat: -rw-r--r-- 2,770 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'minitest/autorun'
require 'net/http/persistent'

class TestNetHttpPersistentTimedStackMulti < Minitest::Test

  class Connection
    attr_reader :host

    def initialize(host)
      @host = host
    end
  end

  def setup
    @stack = Net::HTTP::Persistent::TimedStackMulti.new { Object.new }
  end

  def test_empty_eh
    stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }

    refute_empty stack

    popped = stack.pop

    assert_empty stack

    stack.push connection_args: popped

    refute_empty stack
  end

  def test_length
    stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }

    assert_equal 1, stack.length

    popped = stack.pop

    assert_equal 0, stack.length

    stack.push connection_args: popped

    assert_equal 1, stack.length
  end

  def test_pop
    object = Object.new
    @stack.push object

    popped = @stack.pop

    assert_same object, popped
  end

  def test_pop_empty
    e = assert_raises Timeout::Error do
      @stack.pop timeout: 0
    end

    assert_match 'Waited 0 sec', e.message
  end

  def test_pop_full
    stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }

    popped = stack.pop

    refute_nil popped
    assert_empty stack
  end

  def test_pop_wait
    thread = Thread.start do
      @stack.pop
    end

    Thread.pass while thread.status == 'run'

    object = Object.new

    @stack.push object

    assert_same object, thread.value
  end

  def test_pop_shutdown
    @stack.shutdown { }

    assert_raises ConnectionPool::PoolShuttingDownError do
      @stack.pop
    end
  end

  def test_push
    stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { Object.new }

    conn = stack.pop

    stack.push connection_args: conn

    refute_empty stack
  end

  def test_push_shutdown
    called = []

    @stack.shutdown do |object|
      called << object
    end

    @stack.push connection_args: Object.new

    refute_empty called
    assert_empty @stack
  end

  def test_shutdown
    @stack.push connection_args: Object.new

    called = []

    @stack.shutdown do |object|
      called << object
    end

    refute_empty called
    assert_empty @stack
  end

  def test_pop_recycle
    stack = Net::HTTP::Persistent::TimedStackMulti.new(2) { |host| Connection.new(host) }

    a_conn = stack.pop connection_args: 'a.example'
    stack.push a_conn, connection_args: 'a.example'

    b_conn = stack.pop connection_args: 'b.example'
    stack.push b_conn, connection_args: 'b.example'

    c_conn = stack.pop connection_args: 'c.example'

    assert_equal 'c.example', c_conn.host

    stack.push c_conn, connection_args: 'c.example'

    recreated = stack.pop connection_args: 'a.example'

    refute_same a_conn, recreated
  end

end