require_relative "helper"
require "zhong/web"
require "tilt/erubis"
class TestWeb < Minitest::Test
include Rack::Test::Methods
def app
Zhong::Web
end
def setup
Zhong.clear
end
def test_index
get "/"
assert last_response.ok?
assert_contains "
[TEST] Zhong", last_response.body
assert_contains Zhong::VERSION, last_response.body
end
def test_index_job
Zhong.schedule do
every(10.minutes, "test_web_job") { nil }
end
get "/"
assert last_response.ok?
assert_contains "test_web_job", last_response.body
assert_contains "every 10 minutes", last_response.body
end
def test_disable_job
test_before_disable = 0
test_after_disable = 0
Zhong.schedule do
every(30.seconds, "test_disable_web_job") { nil }
on(:before_disable) { test_before_disable += 1 }
on(:after_disable) { test_after_disable += 1 }
end
job = Zhong.scheduler.find_by_name("test_disable_web_job")
job.enable
assert_equal false, job.disabled?
post "/", "disable" => job.id
assert last_response.ok?
assert_contains "test_disable_web_job", last_response.body
assert_contains "every 30 seconds", last_response.body
assert_contains 'name="enable"', last_response.body
assert_equal true, job.disabled?
assert_equal 1, test_before_disable
assert_equal 1, test_after_disable
end
def test_enable_job
test_before_enable = 0
test_after_enable = 0
Zhong.schedule do
every(12.hours, "test_enable_web_job") { nil }
on(:before_enable) { test_before_enable += 1 }
on(:after_enable) { test_after_enable += 1 }
end
job = Zhong.scheduler.find_by_name("test_enable_web_job")
job.disable
assert_equal true, job.disabled?
post "/", "enable" => job.id
assert last_response.ok?
assert_contains "test_enable_web_job", last_response.body
assert_contains "every 12 hours", last_response.body
assert_contains 'name="disable"', last_response.body
assert_equal false, job.disabled?
assert_equal 1, test_before_enable
assert_equal 1, test_after_enable
end
def test_heartbeat
hostname = `hostname`.strip
pid = Process.pid
t = Thread.new { Zhong.start }
sleep(1)
Zhong.stop
t.join
get "/"
assert last_response.ok?
assert_contains hostname, last_response.body
assert_contains "PID #{pid}", last_response.body
end
end