2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "stubs/test_connection"
|
|
|
|
require "stubs/room"
|
|
|
|
require "active_support/time"
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2018-06-08 16:19:39 -04:00
|
|
|
class ActionCable::Channel::PeriodicTimersTest < ActionCable::TestCase
|
2015-07-12 11:07:31 -04:00
|
|
|
class ChatChannel < ActionCable::Channel::Base
|
2016-04-18 14:54:00 -04:00
|
|
|
# Method name arg
|
2015-07-12 11:07:31 -04:00
|
|
|
periodically :send_updates, every: 1
|
|
|
|
|
2016-04-18 14:54:00 -04:00
|
|
|
# Proc arg
|
|
|
|
periodically -> { ping }, every: 2
|
|
|
|
|
|
|
|
# Block arg
|
|
|
|
periodically every: 3 do
|
|
|
|
ping
|
|
|
|
end
|
|
|
|
|
2015-07-12 11:07:31 -04:00
|
|
|
private
|
|
|
|
def ping
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@connection = TestConnection.new
|
|
|
|
end
|
|
|
|
|
|
|
|
test "periodic timers definition" do
|
|
|
|
timers = ChatChannel.periodic_timers
|
|
|
|
|
2016-04-18 14:54:00 -04:00
|
|
|
assert_equal 3, timers.size
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2016-04-18 14:54:00 -04:00
|
|
|
timers.each_with_index do |timer, i|
|
|
|
|
assert_kind_of Proc, timer[0]
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal i + 1, timer[1][:every]
|
2016-04-18 14:54:00 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
test "disallow negative and zero periods" do
|
|
|
|
[ 0, 0.0, 0.seconds, -1, -1.seconds, "foo", :foo, Object.new ].each do |invalid|
|
2017-01-24 19:46:29 -05:00
|
|
|
e = assert_raise ArgumentError do
|
2016-04-18 14:54:00 -04:00
|
|
|
ChatChannel.periodically :send_updates, every: invalid
|
|
|
|
end
|
2017-01-24 19:46:29 -05:00
|
|
|
assert_match(/Expected every:/, e.message)
|
2016-04-18 14:54:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
test "disallow block and arg together" do
|
2017-01-24 19:46:29 -05:00
|
|
|
e = assert_raise ArgumentError do
|
2016-04-18 14:54:00 -04:00
|
|
|
ChatChannel.periodically(:send_updates, every: 1) { ping }
|
|
|
|
end
|
2017-01-24 19:46:29 -05:00
|
|
|
assert_match(/not both/, e.message)
|
2016-04-18 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
test "disallow unknown args" do
|
|
|
|
[ "send_updates", Object.new, nil ].each do |invalid|
|
2017-01-24 19:46:29 -05:00
|
|
|
e = assert_raise ArgumentError do
|
2016-04-18 14:54:00 -04:00
|
|
|
ChatChannel.periodically invalid, every: 1
|
|
|
|
end
|
2017-01-24 19:46:29 -05:00
|
|
|
assert_match(/Expected a Symbol/, e.message)
|
2016-04-18 14:54:00 -04:00
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "timer start and stop" do
|
2018-05-27 14:50:04 -04:00
|
|
|
mock = Minitest::Mock.new
|
|
|
|
3.times { mock.expect(:shutdown, nil) }
|
|
|
|
|
|
|
|
assert_called(
|
|
|
|
@connection.server.event_loop,
|
|
|
|
:timer,
|
|
|
|
times: 3,
|
|
|
|
returns: mock
|
|
|
|
) do
|
|
|
|
channel = ChatChannel.new @connection, "{id: 1}", id: 1
|
|
|
|
|
|
|
|
channel.subscribe_to_channel
|
|
|
|
channel.unsubscribe_from_channel
|
|
|
|
assert_equal [], channel.send(:active_periodic_timers)
|
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2018-05-27 14:50:04 -04:00
|
|
|
assert mock.verify
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
|
|
|
end
|