Class: DuckTest::FrameWork::Queue

Inherits:
Object
  • Object
show all
Includes:
ConfigHelper, LoggerHelper
Defined in:
lib/duck_test/frame_work/queue.rb

Overview

Queue is responsible to managing and triggering events after being notified via a listener.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerHelper

#ducklog

Methods included from ConfigHelper

#autorun, #autorun=, #autorun?, #autorun_status, #root, #root=, #runnable_basedir, #runnable_basedir=, #watch_basedir, #watch_basedir=

Constructor Details

#initializeQueue

Returns a new instance of Queue.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/duck_test/frame_work/queue.rb', line 61

def initialize

  super

  self.force_run = false
  self.last_queue_event = Time.now
  self.latency = 0.65
  self.lock = Mutex.new
  self.queue = []
  self.queue_event_block = nil
  self.queue_event_lock = Mutex.new
  self.speed = 0.65 #25
  self.stop = false
  self.total_ran = 0

end

Instance Attribute Details

#force_runObject Also known as: force_run?

Boolean flag used to force the queue to empty and run any pending regardless of the current state of autorun.



10
11
12
# File 'lib/duck_test/frame_work/queue.rb', line 10

def force_run
  @force_run
end

#last_queue_eventObject

Holds the time when a queue event was last triggered. This value is a factor when calculating if the queue should be processed.



13
14
15
# File 'lib/duck_test/frame_work/queue.rb', line 13

def last_queue_event
  @last_queue_event
end

#latencyObject

Latency is the amount of time that should pass between each time the event queue is processed. The default value is: 0.15, however, you can use any valid float number. Use this value in conjuction with #speed to fine tune the overall behavior of the queue.

set_latency(10)    # wait 10 seconds since the last time the queue event was triggered.  Regardless of the setting of {#speed}.


20
21
22
# File 'lib/duck_test/frame_work/queue.rb', line 20

def latency
  @latency
end

#lockObject

A Mutex used when working on data items related specifically to the Queue.



23
24
25
# File 'lib/duck_test/frame_work/queue.rb', line 23

def lock
  @lock
end

#ok_to_runObject Also known as: ok_to_run?

A boolean flag used to determine if it is ok to process and run the set of tests currently in the queue. Depending on settings and the number of tests being run, the queue loop may need to wait for the queue_event_block to complete it's previous run. This flag helps control that situation.



28
29
30
# File 'lib/duck_test/frame_work/queue.rb', line 28

def ok_to_run
  @ok_to_run
end

#queueObject

An Array representing the list of files that have changed and need to be processed.



31
32
33
# File 'lib/duck_test/frame_work/queue.rb', line 31

def queue
  @queue
end

#queue_event_blockObject

The block to execute when after the queue has been processed and a list of runnable test files is ready to be run.



34
35
36
# File 'lib/duck_test/frame_work/queue.rb', line 34

def queue_event_block
  @queue_event_block
end

#queue_event_lockObject

A Mutex used when working on data items related specifically executing the queue event.



37
38
39
# File 'lib/duck_test/frame_work/queue.rb', line 37

def queue_event_lock
  @queue_event_lock
end

#speedObject

A float number indicating how fast the queue should run. The thread processing the queue will sleep for the value set by speed. The default value is 0.15, however, you can use any valid float number. Use this value in conjuction with #latency to fine tune the overall behavior of the queue. The higher the number, the slower the speed since the thread containing the loop that checks the queue uses speed as the time to sleep between each interation of the loop.

set_speed(5)    # sleep 5 seconds between each interation of the loop.


45
46
47
# File 'lib/duck_test/frame_work/queue.rb', line 45

def speed
  @speed
end

#stopObject Also known as: stop?

A boolean indicating if the queue should stop processing and have the thread end it's loop.



48
49
50
# File 'lib/duck_test/frame_work/queue.rb', line 48

def stop
  @stop
end

#threadObject

A reference to the thread object responsible for processing the queue.



51
52
53
# File 'lib/duck_test/frame_work/queue.rb', line 51

def thread
  @thread
end

#total_ranObject

A running total of the total number of files that have been processed by the queue during the lifetime of a Queue session.



54
55
56
# File 'lib/duck_test/frame_work/queue.rb', line 54

def total_ran
  @total_ran
end

Instance Method Details

#push(file_spec) ⇒ NilClass

Adds a file_spec to the list of runnable test files that need to be run. It prevents duplicates by checking to see if the file_spec is already in the queue.

Parameters:

  • file_spec (String)

    The full file specification of the file or directory on which the event occured.

Returns:

  • (NilClass)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/duck_test/frame_work/queue.rb', line 163

def push(file_spec)

  self.lock.synchronize do

    if self.autorun? && !self.queue.include?(file_spec)
      self.queue.push(file_spec)
    end

    self.last_queue_event = Time.now

  end

  return nil
end

#queue_event(&block) ⇒ NilClass

Sets the block to execute when the queue has runnable test files ready to be run. The value of this block is actually set by FileManager.

Returns:

  • (NilClass)


82
83
84
85
86
87
88
89
# File 'lib/duck_test/frame_work/queue.rb', line 82

def queue_event(&block)
  if block_given?
    self.queue_event_block = block
    self.ok_to_run = true
  end

  return nil
end

#resetNilClass

Safely resets the queue to an empty Array.

Returns:

  • (NilClass)


181
182
183
184
185
186
# File 'lib/duck_test/frame_work/queue.rb', line 181

def reset
  self.lock.synchronize do
    self.queue = []
  end
  return nil
end

#run(list = []) ⇒ NilClass

Executes #queue_event_block and passes the list of runnable test files to it. A thread lock is obtained and #ok_to_run is set to false prior to executing the block. #ok_to_run is set back to true after the block completes execution.

Parameters:

  • list (Array) (defaults to: [])

    A list of full file specs pointing to runnable test files.

Returns:

  • (NilClass)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/duck_test/frame_work/queue.rb', line 97

def run(list = [])

  self.queue_event_lock.synchronize do

    self.ok_to_run = false

    unless self.queue_event_block.blank?
      self.queue_event_block.call QueueEvent.new(self, list)
    end

    self.ok_to_run = true

  end

  return nil
end

#set_latency(value) ⇒ NilClass

Safely sets the value of #latency

Returns:

  • (NilClass)


201
202
203
204
205
206
# File 'lib/duck_test/frame_work/queue.rb', line 201

def set_latency(value)
  self.lock.synchronize do
    self.latency = value unless value.blank?
  end
  return nil
end

#set_speed(value) ⇒ NilClass

Safely sets the value of #speed

Returns:

  • (NilClass)


191
192
193
194
195
196
# File 'lib/duck_test/frame_work/queue.rb', line 191

def set_speed(value)
  self.lock.synchronize do
    self.speed = value unless value.blank?
  end
  return nil
end

#startNilClass

Starts the queue thread which is the controlling body of the Queue class.

Returns:

  • (NilClass)


117
118
119
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
# File 'lib/duck_test/frame_work/queue.rb', line 117

def start

  self.thread = Thread.new do

    begin

      until self.stop do

        sleep(self.speed)

        if self.autorun? || self.force_run?
          if self.ok_to_run?
            buffer = []
            self.lock.synchronize do
              self.force_run = false
              if ((Time.now - self.last_queue_event) >= self.latency)
                length = self.queue.length
                length.times {|x| buffer.push(self.queue.pop)}
                self.total_ran += buffer.length
              end
            end

            if (buffer.length > 0)
              self.run(buffer)
            end
          else
            ducklog.console "Waiting on runner.  Files in queue: (#{self.queue.length})  Total ran during session: (#{self.total_ran})"
          end
        end

      end

    rescue Exception => e
      ducklog.exception e
    end

  end

  return nil
end

#tests_pendingNumber

Returns the total number of tests in the queue waiting to run.

Returns:

  • (Number)


211
212
213
214
215
216
217
# File 'lib/duck_test/frame_work/queue.rb', line 211

def tests_pending
  value = 0
  self.lock.synchronize do
    value = self.queue.length
  end
  return value
end