Module: Cloudtasker::Testing

Defined in:
lib/cloudtasker/testing.rb

Overview

Enable/Disable test mode for Cloudtasker

Class Method Summary collapse

Class Method Details

.enable!(&block) ⇒ Object

Set cloudtasker to real mode temporarily

Parameters:

  • &block (Proc)

    The block to run in real mode



59
60
61
# File 'lib/cloudtasker/testing.rb', line 59

def enable!(&block)
  switch_test_mode(:enabled, &block)
end

.enabled?Boolean

Return true if Cloudtasker is enabled.

Returns:

  • (Boolean)


84
85
86
# File 'lib/cloudtasker/testing.rb', line 84

def enabled?
  !@test_mode || @test_mode == :enabled
end

.fake!(&block) ⇒ Object

Set cloudtasker to fake mode temporarily

Parameters:

  • &block (Proc)

    The block to run in fake mode



68
69
70
# File 'lib/cloudtasker/testing.rb', line 68

def fake!(&block)
  switch_test_mode(:fake, &block)
end

.fake?Boolean

Return true if Cloudtasker is in fake mode.

Returns:

  • (Boolean)

    True if jobs must be processed through drain calls.



93
94
95
# File 'lib/cloudtasker/testing.rb', line 93

def fake?
  @test_mode == :fake
end

.in_memory?Boolean

Return true if tasks should be managed in memory.

Returns:

  • (Boolean)

    True if jobs are managed in memory.



140
141
142
# File 'lib/cloudtasker/testing.rb', line 140

def in_memory?
  !enabled?
end

.inline!(&block) ⇒ Object

Set cloudtasker to inline mode temporarily

Parameters:

  • &block (Proc)

    The block to run in inline mode



77
78
79
# File 'lib/cloudtasker/testing.rb', line 77

def inline!(&block)
  switch_test_mode(:inline, &block)
end

.inline?Boolean

Return true if Cloudtasker is in inline mode.

Returns:

  • (Boolean)

    True if jobs are run inline.



102
103
104
# File 'lib/cloudtasker/testing.rb', line 102

def inline?
  @test_mode == :inline
end

.raise_errors!(&block) ⇒ Object

Temporarily raise errors in the same manner inline! does it.

This is used when you want to manually drain the jobs but still want to surface errors at runtime, instead of using the retry mechanic.



114
115
116
# File 'lib/cloudtasker/testing.rb', line 114

def raise_errors!(&block)
  switch_error_mode(:raise, &block)
end

.raise_errors?Boolean

Return true if jobs should raise errors immediately without relying on retries.

Returns:

  • (Boolean)

    True if jobs are run inline.



131
132
133
# File 'lib/cloudtasker/testing.rb', line 131

def raise_errors?
  @test_mode == :inline || @error_mode == :raise
end

.silence_errors!(&block) ⇒ Object

Temporarily silence errors. Job will follow the retry logic.



121
122
123
# File 'lib/cloudtasker/testing.rb', line 121

def silence_errors!(&block)
  switch_error_mode(:silence, &block)
end

.switch_error_mode(mode) ⇒ Symbol

Set the error mode, either permanently or temporarily (via block).

Parameters:

  • mode (Symbol)

    The error mode.

Returns:

  • (Symbol)

    The error mode.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloudtasker/testing.rb', line 40

def switch_error_mode(mode)
  if block_given?
    current_mode = @error_mode
    begin
      @error_mode = mode
      yield
    ensure
      @error_mode = current_mode
    end
  else
    @error_mode = mode
  end
end

.switch_test_mode(mode) ⇒ Symbol

Set the test mode, either permanently or temporarily (via block).

Parameters:

  • mode (Symbol)

    The test mode.

Returns:

  • (Symbol)

    The test mode.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloudtasker/testing.rb', line 18

def switch_test_mode(mode)
  if block_given?
    current_mode = @test_mode
    begin
      @test_mode = mode
      yield
    ensure
      @test_mode = current_mode
    end
  else
    @test_mode = mode
  end
end