Skip to content

Commit c179334

Browse files
authored
Merge pull request #144 from rophy/fix-state-file
Fix crash when specifying a state_file in Ruby > 3.2.0
2 parents f116180 + 490e612 commit c179334

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

lib/fluent/plugin/in_sql.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def initialize(path)
282282
require 'yaml'
283283

284284
@path = path
285-
if File.exists?(@path)
285+
if File.exist?(@path)
286286
@data = YAML.load_file(@path)
287287
if @data == false || @data == []
288288
# this happens if an users created an empty file accidentally
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require "helper"
2+
require "fluent/test/driver/input"
3+
4+
class SqlInputStateFileTest < Test::Unit::TestCase
5+
def setup
6+
Fluent::Test.setup
7+
end
8+
9+
def teardown
10+
end
11+
12+
CONFIG = %[
13+
adapter postgresql
14+
host localhost
15+
port 5432
16+
database fluentd_test
17+
18+
username fluentd
19+
password fluentd
20+
21+
state_file /tmp/sql_state
22+
23+
schema_search_path public
24+
25+
tag_prefix db
26+
27+
<table>
28+
table messages
29+
tag logs
30+
update_column updated_at
31+
time_column updated_at
32+
</table>
33+
]
34+
35+
def create_driver(conf = CONFIG)
36+
Fluent::Test::Driver::Input.new(Fluent::Plugin::SQLInput).configure(conf)
37+
end
38+
39+
def test_configure
40+
d = create_driver
41+
expected = {
42+
host: "localhost",
43+
port: 5432,
44+
adapter: "postgresql",
45+
database: "fluentd_test",
46+
username: "fluentd",
47+
password: "fluentd",
48+
schema_search_path: "public",
49+
tag_prefix: "db"
50+
}
51+
actual = {
52+
host: d.instance.host,
53+
port: d.instance.port,
54+
adapter: d.instance.adapter,
55+
database: d.instance.database,
56+
username: d.instance.username,
57+
password: d.instance.password,
58+
schema_search_path: d.instance.schema_search_path,
59+
tag_prefix: d.instance.tag_prefix
60+
}
61+
assert_equal(expected, actual)
62+
tables = d.instance.instance_variable_get(:@tables)
63+
assert_equal(1, tables.size)
64+
messages = tables.first
65+
assert_equal("messages", messages.table)
66+
assert_equal("logs", messages.tag)
67+
end
68+
69+
def test_message
70+
d = create_driver(CONFIG + "select_interval 1")
71+
Message.create!(message: "message 1")
72+
Message.create!(message: "message 2")
73+
Message.create!(message: "message 3")
74+
75+
d.end_if do
76+
d.record_count >= 3
77+
end
78+
d.run
79+
80+
assert_equal("db.logs", d.events[0][0])
81+
expected = [
82+
[d.events[0][1], "message 1"],
83+
[d.events[1][1], "message 2"],
84+
[d.events[2][1], "message 3"],
85+
]
86+
actual = [
87+
[Fluent::EventTime.parse(d.events[0][2]["updated_at"]), d.events[0][2]["message"]],
88+
[Fluent::EventTime.parse(d.events[1][2]["updated_at"]), d.events[1][2]["message"]],
89+
[Fluent::EventTime.parse(d.events[2][2]["updated_at"]), d.events[2][2]["message"]],
90+
]
91+
assert_equal(expected, actual)
92+
end
93+
94+
class Message < ActiveRecord::Base
95+
end
96+
end

0 commit comments

Comments
 (0)