Skip to content

Commit eec3055

Browse files
authored
CloudWebManage/cwm-worker-cluster#180: Update tests and improve code coverage. (#5)
* [CWM-180] Update tests and improve code coverage. Signed-off-by: Azeem Sajid <azeem.sajid@gmail.com> * Iterate on events for flushing instead of lines. * Rename last action method. Update tests.
1 parent 3107077 commit eec3055

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

lib/fluent/plugin/in_http_cwm.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,7 @@ def start
9292

9393
# start interval timer to flush last action entry
9494
timer_execute(:last_action_flush_timer, '1s') do
95-
if @last_action_entry.empty?
96-
@last_action_entry = @last_action_queue.deq.split('|')
97-
log.debug("Dequed last action entry. #{@last_action_entry}")
98-
else
99-
deploymentid, last_action = @last_action_entry
100-
@last_action_entry = [] if update_deployment_last_action(deploymentid, last_action)
101-
end
95+
flush_last_action
10296
end
10397

10498
log.info("Starting HTTP server [#{@host}:#{@port}]...")
@@ -155,6 +149,16 @@ def route(data)
155149
router.emit(@tag, time, record)
156150
end
157151

152+
def flush_last_action
153+
if @last_action_entry.empty?
154+
@last_action_entry = @last_action_queue.deq.split('|')
155+
log.debug("Dequed last action entry. #{@last_action_entry}")
156+
else
157+
deploymentid, last_action = @last_action_entry
158+
@last_action_entry = [] if update_deployment_last_action(deploymentid, last_action)
159+
end
160+
end
161+
158162
def datetime_diff_in_secs(dt_begin, dt_end)
159163
seconds = ((dt_end - dt_begin) * 24 * 60 * 60)
160164
seconds.to_i

test/plugin/test_in_http_cwm.rb

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@ class CwmHttpInputTest < Test::Unit::TestCase
88
setup do
99
Fluent::Test.setup
1010

11-
@default_conf = %(
12-
tag test
13-
)
11+
@default_conf = config_element('ROOT', '', { 'tag' => 'test' })
12+
@custom_conf = config_element('ROOT', '', { 'tag' => 'test' }, [
13+
config_element('redis', '', {
14+
'grace_period' => '1s',
15+
'flush_interval' => '1s'
16+
})
17+
])
18+
19+
@expected_redis_output = {
20+
'deploymentid:last_action:docker-compose-http' => '',
21+
'deploymentid:last_action:docker-compose-https' => '',
22+
'deploymentid:minio-metrics:docker-compose-http:bytes_in' => '61852',
23+
'deploymentid:minio-metrics:docker-compose-https:bytes_in' => '14875',
24+
'deploymentid:minio-metrics:docker-compose-http:bytes_out' => '112012',
25+
'deploymentid:minio-metrics:docker-compose-https:bytes_out' => '2755',
26+
'deploymentid:minio-metrics:docker-compose-http:num_requests_out' => '7',
27+
'deploymentid:minio-metrics:docker-compose-http:num_requests_in' => '10',
28+
'deploymentid:minio-metrics:docker-compose-https:num_requests_in' => '5',
29+
'deploymentid:minio-metrics:docker-compose-http:num_requests_misc' => '5'
30+
}.freeze
1431
end
1532

1633
def create_driver(conf)
@@ -27,13 +44,7 @@ def create_driver(conf)
2744
end
2845

2946
test 'redis default configuration test' do
30-
conf = %(
31-
tag test
32-
<redis>
33-
</redis>
34-
)
35-
36-
driver = create_driver(conf)
47+
driver = create_driver(@default_conf)
3748
plugin = driver.instance
3849
redis = plugin.redis_config
3950
assert_equal 'localhost', redis.host
@@ -47,7 +58,9 @@ def create_driver(conf)
4758

4859
sub_test_case 'route#emit' do
4960
test 'emit test' do
50-
driver = create_driver(@default_conf)
61+
driver = create_driver(@custom_conf)
62+
plugin = driver.instance
63+
redis = plugin.redis_config
5164

5265
res_codes = []
5366
lines = 0
@@ -63,6 +76,25 @@ def create_driver(conf)
6376
assert_equal lines, res_codes.size
6477
assert_equal '200', res_codes[0]
6578
assert_equal 1, res_codes.uniq.size
79+
80+
# run and test private flushing methods
81+
`redis-cli FLUSHALL`
82+
sleep(redis.grace_period)
83+
driver.events.each do
84+
plugin.send(:flush_api_metrics)
85+
plugin.send(:flush_last_action)
86+
end
87+
88+
# verify from Redis server
89+
@expected_redis_output.each do |key, expected_value|
90+
if key.include? 'last_action'
91+
exists = `redis-cli EXISTS #{key}`.chomp
92+
assert_equal '1', exists
93+
else
94+
actual_value = `redis-cli GET #{key}`.chomp
95+
assert_equal expected_value, actual_value
96+
end
97+
end
6698
end
6799
end
68100

0 commit comments

Comments
 (0)