Class: RSpecBackgroundProcess::ProcessPool::ProcessDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-background-process/server.rb,
lib/rspec-background-process/process_pool.rb,
lib/rspec-background-process/refresh_actions.rb,
lib/rspec-background-process/readiness_checks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, group, path, type, options) ⇒ ProcessDefinition

Returns a new instance of ProcessDefinition.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rspec-background-process/process_pool.rb', line 11

def initialize(pool, group, path, type, options)
  @pool = pool
  @group = group
  @path = path
  @type = type

  @extensions = Set.new
  @options = {
    ready_timeout: 10,
    term_timeout: 10,
    kill_timeout: 10,
    ready_test: ->(p){fail 'no readiness check defined'},
    refresh_action: ->(p){p.restart},
    logging: false
  }.merge(options)
  @working_directory = nil
  @arguments = []
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



30
31
32
# File 'lib/rspec-background-process/process_pool.rb', line 30

def group
  @group
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/rspec-background-process/process_pool.rb', line 31

def path
  @path
end

Instance Method Details

#argument(*value) ⇒ Object



87
88
89
# File 'lib/rspec-background-process/process_pool.rb', line 87

def argument(*value)
  @arguments += value
end

#argumentsObject



83
84
85
# File 'lib/rspec-background-process/process_pool.rb', line 83

def arguments
  @arguments
end

#extend(mod, options = {}) ⇒ Object



46
47
48
49
# File 'lib/rspec-background-process/process_pool.rb', line 46

def extend(mod, options = {})
  @extensions << mod
  @options.merge! options
end

#http_port_allocated_form(base_port, port_count = 1) ⇒ Object



4
5
6
# File 'lib/rspec-background-process/server.rb', line 4

def http_port_allocated_form(base_port, port_count = 1)
  extend RSpecBackgroundProcess::BackgroundProcess::Server, base_port: base_port, port_count: port_count
end

#initialize_copy(old) ⇒ Object



33
34
35
36
37
38
# File 'lib/rspec-background-process/process_pool.rb', line 33

def initialize_copy(old)
  # need own copy
  @extensions = @extensions.dup
  @options = @options.dup
  @arguments = @arguments.dup
end

#instanceObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rspec-background-process/process_pool.rb', line 91

def instance
  # disallow changes to the definition once we have instantiated
  @options.freeze
  @arguments.freeze
  @working_directory.freeze
  @extensions.freeze

  # instance is requested
  # we calculate key based on current definition
  _key = key

  # already crated
  if instance = @pool[_key]
    # always make sure options are up to date with definition
    instance.reset_options(@options)
    return instance
  end

  # can only use parts of the key for instance name
  name = Pathname.new(@path).basename

  # need to crate new one
  instance = @type.new(
    "#{@group}-#{name}-#{_key}",
    @path,
    @arguments,
    @working_directory || [name, _key],
    @options
  )

  # ports get allocated here...
  @extensions.each do |mod|
    instance.extend(mod)
  end

  @pool[_key] = instance
end

#keyObject



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
# File 'lib/rspec-background-process/process_pool.rb', line 134

def key
  hash = Digest::SHA256.new
  hash.update @group.to_s
  hash.update @path.to_s
  hash.update @type.name
  @extensions.each do |mod|
    hash.update mod.name
  end
  hash.update @working_directory.to_s
  @arguments.each do |argument|
    case argument
    when Pathname
      begin
        # use file content as part of the hash
        hash.update argument.read
      rescue Errno::ENOENT
        # use file name if it does not exist
        hash.update argument.to_s
      end
    else
      hash.update argument.to_s
    end
  end
  Digest.hexencode(hash.digest)[0..16]
end

#kill_timeout(seconds) ⇒ Object



75
76
77
# File 'lib/rspec-background-process/process_pool.rb', line 75

def kill_timeout(seconds)
  @options[:kill_timeout] = seconds
end

#logging_enabledObject



51
52
53
# File 'lib/rspec-background-process/process_pool.rb', line 51

def logging_enabled
  @options[:logging] = true
end

#logging_enabled?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rspec-background-process/process_pool.rb', line 55

def logging_enabled?
  @options[:logging]
end

#ready_test(&block) ⇒ Object



59
60
61
# File 'lib/rspec-background-process/process_pool.rb', line 59

def ready_test(&block)
  @options[:ready_test] = block
end

#ready_timeout(seconds) ⇒ Object



67
68
69
# File 'lib/rspec-background-process/process_pool.rb', line 67

def ready_timeout(seconds)
  @options[:ready_timeout] = seconds
end

#ready_when_log_includes(log_line) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec-background-process/readiness_checks.rb', line 10

def ready_when_log_includes(log_line)
  ready_test do |instance|
    log_line = instance.render(log_line)

    # NOTE: log file my not be crated just after process is started (spawned) so we need to retry
    with_retries(
      max_tries: 10000,
      base_sleep_seconds: 0.01,
      max_sleep_seconds: 0.2,
      rescue: Errno::ENOENT
    ) do
      File::Tail::Logfile.tail(instance.log_file, forward: 0, interval: 0.01, max_interval: 1, suspicious_interval: 4) do |line|
        line.include?(log_line) and break true
      end
    end
  end
end

#ready_when_url_response_status(uri, status = 'OK') ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rspec-background-process/readiness_checks.rb', line 28

def ready_when_url_response_status(uri, status = 'OK')
  ready_test do |instance|
    _uri = instance.render(uri) # NOTE: new variable (_uri) is needed or strange things happen...

    begin
      with_retries(
      max_tries: 10000,
      base_sleep_seconds: 0.06,
      max_sleep_seconds: 0.2,
      rescue: Errno::ECONNREFUSED
      ) do
        open(_uri).status.last.strip == status and break true
      end
    end
  end
end

#refresh_action(&block) ⇒ Object



63
64
65
# File 'lib/rspec-background-process/process_pool.rb', line 63

def refresh_action(&block)
  @options[:refresh_action] = block
end

#refresh_command(command) ⇒ Object



6
7
8
9
10
11
# File 'lib/rspec-background-process/refresh_actions.rb', line 6

def refresh_command(command)
  refresh_action do |instance|
    _command = instance.render(command)
    system _command
  end
end

#startObject

shortcut



130
131
132
# File 'lib/rspec-background-process/process_pool.rb', line 130

def start
  instance.start
end

#term_timeout(seconds) ⇒ Object



71
72
73
# File 'lib/rspec-background-process/process_pool.rb', line 71

def term_timeout(seconds)
  @options[:term_timeout] = seconds
end

#with {|process| ... } ⇒ Object

Yields:

  • (process)


40
41
42
43
44
# File 'lib/rspec-background-process/process_pool.rb', line 40

def with
  process = dup
  yield process
  process
end

#working_directory(dir) ⇒ Object



79
80
81
# File 'lib/rspec-background-process/process_pool.rb', line 79

def working_directory(dir)
  @working_directory = dir
end