Module: SimpleCov::ResultMerger::ResultsetRunIdentity

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

Overview

Run/worker metadata queries used by resultset storage and parallel coordination. Kept separate from the coverage-folding implementation.

Instance Method Summary collapse

Instance Method Details

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

Whether entry was written by a runner concurrent with this process: either it shares our run id (current_run_entry?), 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 (fork+exec) generates its own random run id, and its freshly written entry is exactly the data issue #581 protects from being clobbered. Strict id matching is reserved for worker counting (worker_identities_for_run), where admitting a stale entry would end a sibling wait early.

Returns:

  • (Boolean)


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

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

  incoming_run_id = incoming["run_id"] if incoming.is_a?(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

Returns:

  • (Boolean)


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

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

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

  fresh_entry?(entry, started_at)
end

#fresh_entry?(entry, started_at) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/simplecov/result_merger/resultset_run_identity.rb', line 34

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 that carry a worker id, [:legacy, command_name] for fresh entries written without one (an old SimpleCov writing to the same resultset). Pairs rather than mangled strings, so a resultset mixing both kinds can never alias a real worker id to a synthesized legacy identity.



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

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, so a same-second stale entry is still overwritten.

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/simplecov/result_merger/resultset_run_identity.rb', line 61

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