Skip to content

Commit 4580a2f

Browse files
committed
test: move stage_size race test into test_buffer.rb
Per review, fold the concurrency regression test into the existing buffer test file (as #4447 did) instead of a separate file. The test now reuses the file's DummyOutputPlugin/DummyMemoryChunk fixtures via an EmptyResumeDummyPlugin variant, since DummyPlugin#resume pre-seeds staged/queued chunks and the race test asserts exact byte accounting on an empty buffer. Full BufferTest: 74 tests, 436 assertions, green.
1 parent 729955b commit 4580a2f

2 files changed

Lines changed: 67 additions & 74 deletions

File tree

‎test/plugin/test_buffer.rb‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ def purge
4646
@purged = true
4747
end
4848
end
49+
# DummyPlugin#resume pre-seeds staged/queued chunks; tests that assert exact
50+
# byte accounting on an empty buffer need this empty-resume variant.
4951
class DummyPlugin < Fluent::Plugin::Buffer
5052
def create_metadata(timekey=nil, tag=nil, variables=nil)
5153
Fluent::Plugin::Buffer::Metadata.new(timekey, tag, variables)
5254
end
55+
5356
def create_chunk(metadata, data)
5457
c = FluentPluginBufferTest::DummyMemoryChunk.new(metadata)
5558
c.append(data)
@@ -84,6 +87,14 @@ def generate_chunk(metadata)
8487
end
8588
end
8689

90+
# DummyPlugin#resume pre-seeds staged/queued chunks; tests that assert exact
91+
# byte accounting on an empty buffer need this empty-resume variant.
92+
class EmptyResumeDummyPlugin < FluentPluginBufferTest::DummyPlugin
93+
def resume
94+
return {}, []
95+
end
96+
end
97+
8798
class BufferTest < Test::Unit::TestCase
8899
def create_buffer(hash)
89100
buffer_conf = config_element('buffer', '', hash, [])
@@ -1606,4 +1617,60 @@ def create_chunk_es(metadata, es)
16061617
refute stats['available_buffer_space_ratios'].to_f.nan?
16071618
end
16081619
end
1620+
1621+
sub_test_case 'stage_size accounting between concurrent write and enqueue' do
1622+
def create_empty_buffer(hash)
1623+
buffer_conf = config_element('buffer', '', hash, [])
1624+
owner = FluentPluginBufferTest::DummyOutputPlugin.new
1625+
owner.configure(config_element('ROOT', '', {}, [ buffer_conf ]))
1626+
p = EmptyResumeDummyPlugin.new
1627+
p.owner = owner
1628+
p.configure(buffer_conf)
1629+
p
1630+
end
1631+
1632+
test 'stage_size does not go negative while a write is pending' do
1633+
b = create_empty_buffer('total_limit_size' => 1024, 'chunk_limit_size' => 4096)
1634+
b.start
1635+
1636+
m = create_metadata
1637+
b.write({ m => ['a' * 400] })
1638+
chunk = b.stage[m]
1639+
assert_equal 400, b.stage_size
1640+
1641+
reached = Queue.new
1642+
resume = Queue.new
1643+
armed = false
1644+
1645+
chunk.define_singleton_method(:mon_exit) do
1646+
result = super()
1647+
if armed
1648+
armed = false
1649+
reached << true
1650+
resume.pop
1651+
end
1652+
result
1653+
end
1654+
1655+
armed = true
1656+
writer = Thread.new { b.write({ m => ['b' * 400] }) }
1657+
reached.pop
1658+
1659+
b.enqueue_chunk(m)
1660+
assert_equal 0, b.stage_size
1661+
1662+
resume << true
1663+
writer.join
1664+
1665+
assert_equal 0, b.stage.size
1666+
assert_equal 0, b.stage_size
1667+
assert_equal 800, b.queue_size
1668+
ensure
1669+
if writer&.alive?
1670+
resume << true
1671+
writer.join
1672+
end
1673+
b&.stop
1674+
end
1675+
end
16091676
end

‎test/plugin/test_stage_size_race.rb‎

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)