Module: SimpleCov::ResultMerger::ResultsetRunIdentity

Included in:
SimpleCov::ResultMerger
Defined in:
lib/simplecov/result_merger/resultset_run_identity.rb

Overview

Run and worker metadata queries used by resultset storage and parallel coordination.

Instance Method Summary collapse

Instance Method Details

#concurrent_runner_entry?(entry, incoming = nil) ⇒ Boolean

Either the entry shares our run id, or it was written strictly after our process started. A mismatched run id must not defeat the timestamp check: a subprocess we shelled out to generates its own random run id, and its freshly written entry is exactly the data #581 protects from being clobbered. Strict id matching is reserved for worker counting, where admitting a stale entry would end a sibling wait early.

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
# File 'lib/simplecov/result_merger/resultset_run_identity.rb', line 47

def concurrent_runner_entry?(entry, incoming = nil)
  return false unless entry.instance_of?(Hash)

  incoming_run_id = incoming["run_id"] if incoming.instance_of?(Hash)
  started_at = SimpleCov.process_start_time
  return true if current_run_entry?(entry, incoming_run_id, started_at)

  written_after_start?(entry, started_at)
end

#current_run_entry?(entry, run_id, started_at) ⇒ Boolean

eql? rather than ==: run ids are strings, and value equality already answers false for the nil run id a caller with no identity of its own passes.

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
# File 'lib/simplecov/result_merger/resultset_run_identity.rb', line 25

def current_run_entry?(entry, run_id, started_at)
  return false unless entry.instance_of?(Hash)

  entry_run_id = entry["run_id"]
  return fresh_entry?(entry, started_at) unless entry_run_id
  return false unless entry_run_id.eql?(run_id)
  return true if RunIdentity.authoritative?

  fresh_entry?(entry, started_at)
end

#fresh_entry?(entry, started_at) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/simplecov/result_merger/resultset_run_identity.rb', line 36

def fresh_entry?(entry, started_at)
  timestamp = entry["timestamp"]
  timestamp.is_a?(Numeric) && started_at && timestamp >= started_at.to_f
end

#worker_identities_for_run(results, run_id, started_at) ⇒ Object

One namespaced identity per distinct worker that wrote to the current run: [:worker, id] for entries carrying a worker id, [:legacy, command_name] for fresh entries written without one. Pairs rather than mangled strings, so a resultset mixing both kinds can never alias a real worker id to a synthesized legacy identity.



13
14
15
16
17
18
19
20
# File 'lib/simplecov/result_merger/resultset_run_identity.rb', line 13

def worker_identities_for_run(results, run_id, started_at)
  results.filter_map do |command_name, data|
    next unless current_run_entry?(data, run_id, started_at)

    worker_id = data["worker_id"]
    worker_id.to_s.empty? ? [:legacy, command_name] : [:worker, worker_id.to_s]
  end.uniq
end

#written_after_start?(entry, started_at) ⇒ Boolean

Strictly after, unlike fresh_entry?: an entry stamped at the exact instant we started is treated as leftover from a previous run.

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/simplecov/result_merger/resultset_run_identity.rb', line 59

def written_after_start?(entry, started_at)
  timestamp = entry["timestamp"]
  timestamp.is_a?(Numeric) && started_at && timestamp > started_at.to_f
end