aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/worker_test.rb
blob: 654f49821e8c144614b9e1997db49f756797c38a (plain) (blame)
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
require 'test_helper'

class WorkerTest < ActiveSupport::TestCase
  class Receiver
    attr_accessor :last_action

    def run
      @last_action = :run
    end

    def process(message)
      @last_action =  [ :process, message ]
    end

    def connection
      self
    end

    def logger
      # Impersonating a connection requires a TaggedLoggerProxy'ied logger.
      inner_logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }
      ActionCable::Connection::TaggedLoggerProxy.new(inner_logger, tags: [])
    end
  end

  setup do
    @worker = ActionCable::Server::Worker.new
    @receiver = Receiver.new
  end

  teardown do
    @receiver.last_action = nil
  end

  test "invoke" do
    @worker.invoke @receiver, :run
    assert_equal :run, @receiver.last_action
  end

  test "invoke with arguments" do
    @worker.invoke @receiver, :process, "Hello"
    assert_equal [ :process, "Hello" ], @receiver.last_action
  end

  test "running periodic timers with a proc" do
    @worker.run_periodic_timer @receiver, @receiver.method(:run)
    assert_equal :run, @receiver.last_action
  end

  test "running periodic timers with a method" do
    @worker.run_periodic_timer @receiver, :run
    assert_equal :run, @receiver.last_action
  end
end