-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
98 lines (77 loc) · 2.14 KB
/
test.rb
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
require 'fileutils'
require 'fluent-logger'
def start_fluentd(verbose=false)
conf = <<-CONF
<source>
@type forward
</source>
<match test>
@type file
path /tmp/fluent-shutdown-test/output/${tag}
append true
<buffer tag>
@type memory
</buffer>
</match>
CONF
FileUtils.remove_dir '/tmp/fluent-shutdown-test' if File.exist?('/tmp/fluent-shutdown-test')
FileUtils.mkdir_p '/tmp/fluent-shutdown-test'
File.open('/tmp/fluent-shutdown-test/fluent.conf', 'wb') do |file|
file.puts conf
end
pid = fork do
puts "start fluentd"
if verbose
system("fluentd -c /tmp/fluent-shutdown-test/fluent.conf -d /tmp/fluent-shutdown-test/pid -o /dev/stdout")
else
system("fluentd -c /tmp/fluent-shutdown-test/fluent.conf -d /tmp/fluent-shutdown-test/pid -o /dev/null")
end
exit
end
sleep 3
pid
end
def shutdown_fluentd(fork_pid)
puts "graceful shutdown fluentd"
pid = File.open('/tmp/fluent-shutdown-test/pid', 'r').read.chomp
system "kill #{pid}"
sleep 5
end
def post_events(cnt)
puts "post #{cnt} events"
log = Fluent::Logger::FluentLogger.new(nil, :host => 'localhost', :port => 24224, :buffer_limit => 256*1024*1024)
cnt.times do |i|
log.post("test", {"test" => i})
end
# flush
log.close
end
def veryfy_output(cnt)
log_count = File.open("/tmp/fluent-shutdown-test/output/test.log").readlines.count
log_count == cnt
end
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class TC_FluentdShutdownTest < Test::Unit::TestCase
def test_10_events
fork_pid = start_fluentd true
# post 10 events
post_events 10
# send graceful shutdown immediately
shutdown_fluentd(fork_pid)
# check out_file size
result = veryfy_output 10
assert(result, 'post event count != output event size')
end
def test_100000_events
fork_pid = start_fluentd true
# post 100000 events
post_events 100000
# send graceful shutdown immediately
shutdown_fluentd(fork_pid)
# check out_file size
result = veryfy_output 100000
assert(result, 'post event count != output event size')
end
end
Test::Unit::UI::Console::TestRunner.run(TC_FluentdShutdownTest)