Class: Fluent::Test::Driver::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/test/driver/base.rb

Direct Known Subclasses

BaseOwned, BaseOwner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, opts: {}, &block) ⇒ Base

Returns a new instance of Base.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/test/driver/base.rb', line 29

def initialize(klass, opts: {}, &block)
  if klass.is_a?(Class)
    if block
      # Create new class for test w/ overwritten methods
      #   klass.dup is worse because its ancestors does NOT include original class name
      klass_name = klass.name
      klass = Class.new(klass)
      klass.define_singleton_method("name") { klass_name }
      klass.module_eval(&block)
    end
    @instance = klass.new
  else
    @instance = klass
  end
  @instance.under_plugin_development = true

  @logs = []

  @run_post_conditions = []
  @run_breaking_conditions = []
  @broken = false
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



27
28
29
# File 'lib/fluent/test/driver/base.rb', line 27

def instance
  @instance
end

#logsObject (readonly)

Returns the value of attribute logs.



27
28
29
# File 'lib/fluent/test/driver/base.rb', line 27

def logs
  @logs
end

Instance Method Details

#break_if(&block) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
# File 'lib/fluent/test/driver/base.rb', line 61

def break_if(&block)
  raise ArgumentError, "block is not given" unless block_given?
  @run_breaking_conditions << block
end

#broken?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/fluent/test/driver/base.rb', line 66

def broken?
  @broken
end

#configure(conf, syntax: :v1) ⇒ Object

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/fluent/test/driver/base.rb', line 52

def configure(conf, syntax: :v1)
  raise NotImplementedError
end

#end_if(&block) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
# File 'lib/fluent/test/driver/base.rb', line 56

def end_if(&block)
  raise ArgumentError, "block is not given" unless block_given?
  @run_post_conditions << block
end

#instance_hook_after_startedObject



97
98
99
# File 'lib/fluent/test/driver/base.rb', line 97

def instance_hook_after_started
  # insert hooks for tests available after instance.start
end

#instance_shutdownObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fluent/test/driver/base.rb', line 101

def instance_shutdown
  @instance.stop            unless @instance.stopped?
  @instance.before_shutdown unless @instance.before_shutdown?
  @instance.shutdown        unless @instance.shutdown?

  if @instance.respond_to?(:event_loop_wait_until_stop)
    @instance.event_loop_wait_until_stop
  end

  @instance.after_shutdown  unless @instance.after_shutdown?
  @instance.close     unless @instance.closed?

  if @instance.respond_to?(:thread_wait_until_stop)
    @instance.thread_wait_until_stop
  end

  @instance.terminate unless @instance.terminated?
end

#instance_startObject



87
88
89
90
91
92
93
94
95
# File 'lib/fluent/test/driver/base.rb', line 87

def instance_start
  unless @instance.started?
    @instance.start
    instance_hook_after_started
  end
  unless @instance.after_started?
    @instance.after_start
  end
end

#run(timeout: nil, start: true, shutdown: true, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/test/driver/base.rb', line 70

def run(timeout: nil, start: true, shutdown: true, &block)
  instance_start if start

  if @instance.respond_to?(:thread_wait_until_start)
    @instance.thread_wait_until_start
  end
  if @instance.respond_to?(:event_loop_wait_until_start)
    @instance.event_loop_wait_until_start
  end

  begin
    run_actual(timeout: timeout, &block)
  ensure
    instance_shutdown if shutdown
  end
end

#run_actual(timeout: nil, &block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fluent/test/driver/base.rb', line 120

def run_actual(timeout: nil, &block)
  if @instance.respond_to?(:_threads)
    until @instance._threads.values.all?(&:alive?)
      sleep 0.01
    end
  end

  if @instance.respond_to?(:event_loop_running?)
    until @instance.event_loop_running?
      sleep 0.01
    end
  end

  if timeout
    stop_at = Time.now + timeout
    @run_breaking_conditions << ->(){ Time.now >= stop_at }
  end

  if !block_given? && @run_post_conditions.empty? && @run_breaking_conditions.empty?
    raise ArgumentError, "no stop conditions nor block specified"
  end

  proc = if block_given?
           ->(){ block.call; sleep(0.1) until stop? }
         else
           ->(){ sleep(0.1) until stop? }
         end

  if timeout
    begin
      Timeout.timeout(timeout * 1.1) do |sec|
        proc.call
      end
    rescue Timeout::Error
      @broken = true
    end
  else
    proc.call
  end
end

#stop?Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fluent/test/driver/base.rb', line 161

def stop?
  # Should stop running if post conditions are not registered.
  return true unless @run_post_conditions

  # Should stop running if all of the post conditions are true.
  return true if @run_post_conditions.all? {|proc| proc.call }

  # Should stop running if some of the breaking conditions is true.
  # In this case, some post conditions may be not true.
  if @run_breaking_conditions.any? {|proc| proc.call }
    @broken = true
    return true
  end

  false
end