Module: TestRunner::TestState

Defined in:
lib/test-runner/test_state.rb

Class Method Summary collapse

Class Method Details

.directory(base, test) ⇒ Object



9
10
11
12
# File 'lib/test-runner/test_state.rb', line 9

def self.directory(base, test)
  slug = test.path.gsub(/[^A-Za-z0-9_.-]+/, '__')
  File.join(base, "os-test-#{slug}-#{Digest::SHA256.hexdigest(test.path)[0, 8]}")
end

.fork(keep:, &block) ⇒ Object

Keep the evaluator's lock until it exits, even if its parent dies first. Close unrelated workers' locks so this child cannot prolong their lifetime.



38
39
40
41
42
43
44
45
46
47
# File 'lib/test-runner/test_state.rb', line 38

def self.fork(keep:, &block)
  @mutex.synchronize do
    Process.fork do
      @locks.each { |lock| lock.close unless lock.equal?(keep) }
      @locks = [keep]
      @mutex = Mutex.new
      block.call
    end
  end
end

.with_lock(directory) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/test-runner/test_state.rb', line 14

def self.with_lock(directory)
  FileUtils.mkdir_p(directory)
  lock = nil
  @mutex.synchronize do
    lock = File.open(File.join(directory, '.lock'), File::RDWR | File::CREAT, 0o600)
    unless lock.flock(File::LOCK_EX | File::LOCK_NB)
      raise "Test state is already in use: #{directory}"
    end

    @locks << lock
  end

  yield lock
ensure
  if lock
    @mutex.synchronize do
      @locks.delete(lock)
      lock.close
    end
  end
end