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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
| | # Copyright (C) 2013-2020 all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# frozen_string_literal: true
require './test/helper'
require 'stringio'
require 'dtas/buffer'
class TestBuffer < Testcase
@@max_size = File.read("/proc/sys/fs/pipe-max-size").to_i
@@max_size = nil if @@max_size == 0
def teardown
@to_close.each { |io| io.close unless io.closed? }
end
def setup
@to_close = []
end
def pipe
ret = IO.pipe
ret.each do |x|
def x.ready_write_optimized?
false
end
end
@to_close.concat(ret)
ret
end
def tmperr
olderr = $stderr
$stderr = newerr = StringIO.new
yield
newerr
ensure
$stderr = olderr
end
def new_buffer
buf = DTAS::Buffer.new
@to_close << buf.to_io
@to_close << buf.wr
buf
end
def test_set_buffer_size
buf = new_buffer
buf.buffer_size = @@max_size
assert_equal @@max_size, buf.buffer_size
end if defined?(DTAS::Pipe::F_GETPIPE_SZ)
def test_buffer_size
buf = new_buffer
assert_operator buf.buffer_size, :>, 128
buf.buffer_size = @@max_size
assert_equal @@max_size, buf.buffer_size
end if defined?(DTAS::Pipe::F_GETPIPE_SZ)
def test_broadcast_1
buf = new_buffer
r, w = IO.pipe
buf.wr.write "HIHI"
assert_equal [w], buf.broadcast([w])
assert_equal 4, buf.bytes_xfer
tmp = [w]
r.close
buf.wr.write "HIHI"
newerr = tmperr { assert_nil buf.broadcast(tmp) }
assert_equal [], tmp
assert_match(%r{dropping}, newerr.string)
end
def test_broadcast_tee
buf = new_buffer
return unless buf.respond_to?(:__broadcast_tee)
blocked = []
a = pipe
b = pipe
buf.wr.write "HELLO"
assert_equal 4, buf.__broadcast_tee(blocked, [a[1], b[1]], 4)
assert_empty blocked
assert_equal "HELL", a[0].read(4)
assert_equal "HELL", b[0].read(4)
assert_equal 5, buf.__broadcast_tee(blocked, [a[1], b[1]], 5)
assert_empty blocked
assert_equal "HELLO", a[0].read(5)
assert_equal "HELLO", b[0].read(5)
max = '*' * pipe_size(a[0])
assert_equal max.size, a[1].write(max)
assert_equal a[0].nread, pipe_size(a[0])
a[1].nonblock = true
assert_equal 5, buf.__broadcast_tee(blocked, [a[1], b[1]], 5)
assert_equal [a[1]], blocked
a[1].nonblock = false
b[0].read(b[0].nread)
b[1].write(max)
end
def test_broadcast
a = pipe
b = pipe
buf = new_buffer
buf.wr.write "HELLO"
assert_equal :wait_readable, buf.broadcast([a[1], b[1]])
assert_equal 5, buf.bytes_xfer
assert_equal "HELLO", a[0].read(5)
assert_equal "HELLO", b[0].read(5)
return unless defined?(DTAS::Pipe::F_GETPIPE_SZ)
b[1].nonblock = true
b[1].write('*' * pipe_size(b[1]))
buf.wr.write "BYE"
assert_equal :wait_readable, buf.broadcast([a[1], b[1]])
assert_equal 8, buf.bytes_xfer
buf.wr.write "DROP"
b[0].close
tmp = [a[1], b[1]]
newerr = tmperr { assert_equal :wait_readable, buf.broadcast(tmp) }
assert_equal 12, buf.bytes_xfer
assert_equal [a[1]], tmp
assert_match(%r{dropping}, newerr.string)
end
def test_broadcast_total_fail
a = pipe
b = pipe
buf = new_buffer
buf.wr.write "HELLO"
a[0].close
b[0].close
tmp = [a[1], b[1]]
newerr = tmperr { assert_nil buf.broadcast(tmp) }
assert_equal [], tmp
assert_match(%r{dropping}, newerr.string)
end
def test_broadcast_mostly_fail
a = pipe
b = pipe
c = pipe
buf = new_buffer
buf.wr.write "HELLO"
b[0].close
c[0].close
tmp = [a[1], b[1], c[1]]
newerr = tmperr { assert_equal :wait_readable, buf.broadcast(tmp) }
assert_equal 5, buf.bytes_xfer
assert_equal [a[1]], tmp
assert_match(%r{dropping}, newerr.string)
end
def test_broadcast_all_full
a = pipe
b = pipe
buf = new_buffer
a[1].write('*' * pipe_size(a[1]))
b[1].write('*' * pipe_size(b[1]))
a[1].nonblock = true
b[1].nonblock = true
tmp = [a[1], b[1]]
buf.wr.write "HELLO"
assert_equal tmp, buf.broadcast(tmp)
assert_equal [a[1], b[1]], tmp
end if defined?(DTAS::Pipe::F_GETPIPE_SZ)
def test_serialize
buf = new_buffer
hash = buf.to_hsh
assert_empty hash
buf.buffer_size = 4096
hash = buf.to_hsh
assert_equal %w(buffer_size), hash.keys
assert_kind_of Integer, hash["buffer_size"]
assert_operator hash["buffer_size"], :>, 0
end
def test_close
buf = DTAS::Buffer.new
buf.wr.write "HI"
assert_equal 2, buf.inflight
buf.close
assert_equal 0, buf.inflight
assert_nil buf.close!
end
def test_load_nil
buf = DTAS::Buffer.load(nil)
buf.close!
end
def test_load_empty
buf = DTAS::Buffer.load({})
buf.close!
end
def test_load_size
buf = DTAS::Buffer.load({"buffer_size" => 4096})
assert_equal 4096, buf.buffer_size
buf.close!
end
def pipe_size(io)
io.fcntl(DTAS::Pipe::F_GETPIPE_SZ)
end
end
|