|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require_relative "test_helper" |
| 4 | +require "thread" |
2 | 5 |
|
3 | | -class ThreadQueueTest < StdlibTest |
4 | | - target Thread::Queue |
| 6 | +class Thread__Queue__InstanceTest < Test::Unit::TestCase |
| 7 | + include TestHelper |
5 | 8 |
|
6 | | - def test_lshift |
7 | | - q = Thread::Queue.new |
8 | | - q << :a |
9 | | - end |
| 9 | + testing "::Thread::Queue[::Integer]" |
10 | 10 |
|
11 | | - def test_clear |
12 | | - q = Thread::Queue.new |
13 | | - q.clear |
14 | | - end |
| 11 | + def test_push |
| 12 | + queue = Thread::Queue.new |
15 | 13 |
|
16 | | - def test_close |
17 | | - q = Thread::Queue.new |
18 | | - q.close |
| 14 | + assert_send_type( |
| 15 | + "(::Integer) -> void", |
| 16 | + queue, :push, 1 |
| 17 | + ) |
19 | 18 | end |
20 | 19 |
|
21 | | - def test_closed? |
22 | | - q = Thread::Queue.new |
23 | | - q.closed? |
24 | | - q.close |
25 | | - q.closed? |
| 20 | + def test_length |
| 21 | + queue = Thread::Queue.new |
| 22 | + |
| 23 | + assert_send_type( |
| 24 | + "() -> ::Integer", |
| 25 | + queue, :length |
| 26 | + ) |
26 | 27 | end |
27 | 28 |
|
28 | | - def test_deq |
29 | | - q = Thread::Queue.new |
30 | | - 3.times { q << :a } |
| 29 | + def test_num_waiting |
| 30 | + queue = Thread::Queue.new |
31 | 31 |
|
32 | | - q.deq |
33 | | - q.deq(true) |
34 | | - q.deq(false) |
| 32 | + assert_send_type( |
| 33 | + "() -> ::Integer", |
| 34 | + queue, :num_waiting |
| 35 | + ) |
35 | 36 | end |
36 | 37 |
|
37 | 38 | def test_empty? |
38 | | - q = Thread::Queue.new |
39 | | - q.empty? |
40 | | - q << :a |
41 | | - q.empty? |
| 39 | + queue = Thread::Queue.new |
| 40 | + |
| 41 | + assert_send_type( |
| 42 | + "() -> bool", |
| 43 | + queue, :empty? |
| 44 | + ) |
42 | 45 | end |
43 | 46 |
|
44 | | - def test_enq |
45 | | - q = Thread::Queue.new |
46 | | - q.enq(:a) |
| 47 | + def test_closed? |
| 48 | + queue = Thread::Queue.new |
| 49 | + |
| 50 | + assert_send_type( |
| 51 | + "() -> bool", |
| 52 | + queue, :closed? |
| 53 | + ) |
47 | 54 | end |
48 | 55 |
|
49 | | - def test_length |
50 | | - q = Thread::Queue.new |
51 | | - q.length |
| 56 | + def test_close |
| 57 | + queue = Thread::Queue.new |
| 58 | + |
| 59 | + assert_send_type( |
| 60 | + "() -> void", |
| 61 | + queue, :close |
| 62 | + ) |
52 | 63 | end |
53 | 64 |
|
54 | | - def test_num_waiting |
55 | | - q = Thread::Queue.new |
56 | | - q.num_waiting |
| 65 | + def test_clear |
| 66 | + queue = Thread::Queue.new |
| 67 | + |
| 68 | + assert_send_type( |
| 69 | + "() -> void", |
| 70 | + queue, :clear |
| 71 | + ) |
57 | 72 | end |
58 | 73 |
|
59 | 74 | def test_pop |
60 | | - q = Thread::Queue.new |
61 | | - 3.times { q << :a } |
62 | | - q.pop |
63 | | - q.pop(true) |
64 | | - q.pop(false) |
65 | | - q.pop(timeout: 0.2) |
66 | | - q.pop(timeout: ToF.new(0.1).__with_object_methods(:==)) |
| 75 | + queue = Thread::Queue.new |
| 76 | + |
| 77 | + queue.push(1) |
| 78 | + |
| 79 | + # Get an Integer from the queue |
| 80 | + assert_send_type( |
| 81 | + "() -> ::Integer", |
| 82 | + queue, :pop |
| 83 | + ) |
| 84 | + |
| 85 | + queue.push(2) |
| 86 | + |
| 87 | + # Get an Integer from the queue with timeout |
| 88 | + assert_send_type( |
| 89 | + "(timeout: ::Float) -> ::Integer", |
| 90 | + queue, :pop, timeout: 0.1 |
| 91 | + ) |
| 92 | + |
| 93 | + queue.push(3) |
| 94 | + |
| 95 | + # Get an Integer from the queue with `nil` timeout |
| 96 | + assert_send_type( |
| 97 | + "(timeout: nil) -> ::Integer", |
| 98 | + queue, :pop, timeout: nil |
| 99 | + ) |
| 100 | + |
| 101 | + # Returns `nil` when the queue is empty and with timeout |
| 102 | + assert_send_type( |
| 103 | + "(timeout: ::Float) -> nil", |
| 104 | + queue, :pop, timeout: 0.0 |
| 105 | + ) |
| 106 | + |
| 107 | + queue.push(4) |
| 108 | + |
| 109 | + # With nonblocking is true, but queue is not empty |
| 110 | + assert_send_type( |
| 111 | + "(bool) -> ::Integer", |
| 112 | + queue, :pop, true |
| 113 | + ) |
| 114 | + |
| 115 | + # Raises an exception with nonblocking but queue is empty |
| 116 | + begin |
| 117 | + assert_send_type( |
| 118 | + "(bool) -> bot", |
| 119 | + queue, :pop, true |
| 120 | + ) |
| 121 | + rescue ThreadError |
| 122 | + end |
| 123 | + |
| 124 | + queue.close() |
| 125 | + |
| 126 | + # Returns `nil` if the queue is closed |
| 127 | + assert_send_type( |
| 128 | + "() -> nil", |
| 129 | + queue, :pop |
| 130 | + ) |
67 | 131 | end |
| 132 | +end |
68 | 133 |
|
69 | | - def test_push |
70 | | - q = Thread::Queue.new |
71 | | - q.push(:a) |
| 134 | + |
| 135 | +class Thread__SizedQueue__InstanceTest < Test::Unit::TestCase |
| 136 | + include TestHelper |
| 137 | + |
| 138 | + testing "::Thread::SizedQueue[::Integer]" |
| 139 | + |
| 140 | + def test_max |
| 141 | + queue = Thread::SizedQueue.new(10) |
| 142 | + |
| 143 | + assert_send_type( |
| 144 | + "() -> ::Integer", |
| 145 | + queue, :max |
| 146 | + ) |
72 | 147 | end |
73 | 148 |
|
74 | | - def test_shift |
75 | | - q = Thread::Queue.new |
76 | | - 3.times { q << :a } |
77 | | - q.shift |
78 | | - q.shift(true) |
79 | | - q.shift(false) |
| 149 | + def test_max= |
| 150 | + queue = Thread::SizedQueue.new(10) |
| 151 | + |
| 152 | + assert_send_type( |
| 153 | + "(::Integer) -> ::Integer", |
| 154 | + queue, :max=, 20 |
| 155 | + ) |
80 | 156 | end |
81 | 157 |
|
82 | | - def test_size |
83 | | - q = Thread::Queue.new |
84 | | - q.size |
| 158 | + def test_push |
| 159 | + queue = Thread::SizedQueue.new(1) |
| 160 | + |
| 161 | + assert_send_type( |
| 162 | + "(::Integer) -> void", |
| 163 | + queue, :push, 1 |
| 164 | + ) |
| 165 | + |
| 166 | + queue.pop |
| 167 | + |
| 168 | + # Nonblocking mode: queue is not full |
| 169 | + assert_send_type( |
| 170 | + "(::Integer, bool) -> void", |
| 171 | + queue, :push, 2, true |
| 172 | + ) |
| 173 | + |
| 174 | + # Nonblocking mode: queue is full |
| 175 | + begin |
| 176 | + assert_send_type( |
| 177 | + "(::Integer, bool) -> bot", |
| 178 | + queue, :push, 3, true |
| 179 | + ) |
| 180 | + rescue ThreadError |
| 181 | + end |
| 182 | + |
| 183 | + queue.pop |
| 184 | + |
| 185 | + # With timeout: queue is not full |
| 186 | + assert_send_type( |
| 187 | + "(::Integer, timeout: Float) -> ::Thread::SizedQueue[::Integer]", |
| 188 | + queue, :push, 3, timeout: 0.3 |
| 189 | + ) |
| 190 | + |
| 191 | + # With timeout: queue is full |
| 192 | + assert_send_type( |
| 193 | + "(::Integer, timeout: Float) -> nil", |
| 194 | + queue, :push, 4, timeout: 0.0 |
| 195 | + ) |
85 | 196 | end |
86 | 197 | end |
0 commit comments