Class: TestBench::Session::Isolate

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/session/isolate.rb,
lib/test_bench/session/isolate/substitute.rb

Defined Under Namespace

Modules: Status, Substitute

Constant Summary collapse

Isolated =
Telemetry::Event.define(:file, :result)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#apex_directoryObject



9
10
11
# File 'lib/test_bench/session/isolate.rb', line 9

def apex_directory
  @apex_directory ||= ::Dir.pwd
end

#file_path_writerObject

Returns the value of attribute file_path_writer.



17
18
19
# File 'lib/test_bench/session/isolate.rb', line 17

def file_path_writer
  @file_path_writer
end

#subprocess_idObject

Returns the value of attribute subprocess_id.



14
15
16
# File 'lib/test_bench/session/isolate.rb', line 14

def subprocess_id
  @subprocess_id
end

#subprocess_sequenceObject



4
5
6
# File 'lib/test_bench/session/isolate.rb', line 4

def subprocess_sequence
  @subprocess_sequence ||= 0
end

#telemetry_readerObject

Returns the value of attribute telemetry_reader.



16
17
18
# File 'lib/test_bench/session/isolate.rb', line 16

def telemetry_reader
  @telemetry_reader
end

Class Method Details

.build(apex_directory: nil) ⇒ Object



19
20
21
22
23
# File 'lib/test_bench/session/isolate.rb', line 19

def self.build(apex_directory: nil)
  instance = new
  instance.apex_directory = apex_directory
  instance
end

.configure(receiver, apex_directory: nil, attr_name: nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/test_bench/session/isolate.rb', line 25

def self.configure(receiver, apex_directory: nil, attr_name: nil)
  attr_name ||= :isolate

  instance = build(apex_directory:)
  receiver.public_send(:"#{attr_name}=", instance)
end

Instance Method Details

#call(file_path, &probe) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/test_bench/session/isolate.rb', line 32

def call(file_path, &probe)
  case status
  when Status.stopping
    stop
    start
  when Status.stopped
    start
  end

  file_path_writer.puts(file_path)

  loop do
    event_data_text = telemetry_reader.gets

    event_data = Telemetry::EventData.load(event_data_text)

    if Isolated === event_data
      executed_file_path, result = event_data.data

      if executed_file_path == file_path
        if result == Result.aborted
          stop!
        end

        break
      else
        next
      end
    end

    probe.(event_data)
  end
end

#startObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/test_bench/session/isolate.rb', line 66

def start
  file_path_reader, file_path_writer = IO.pipe
  telemetry_reader, telemetry_writer = IO.pipe

  subprocess_id = fork do
    file_path_writer.close
    telemetry_reader.close

    ::Dir.chdir(apex_directory)

    session = Session.build

    telemetry_sink = Telemetry::Sink::File.new(telemetry_writer)
    session.register_telemetry_sink(telemetry_sink)

    Session.establish(session)

    while file_path = file_path_reader.gets(chomp: true)
      pending_event = Isolated.build(file_path)

      absolute_file_path = ::File.expand_path(file_path)

      session.evaluate(pending_event) do
        load(absolute_file_path)
      end
    end

  ensure
    # Ensure any exception raised by the subprocess is likely to be printed after the session's test output
    one_millisecond = 0.001
    sleep(one_millisecond)
  end

  telemetry_writer.close
  file_path_reader.close

  self.subprocess_id = subprocess_id
  self.subprocess_sequence += 1

  self.telemetry_reader = telemetry_reader
  self.file_path_writer = file_path_writer

  subprocess_id
end

#statusObject



130
131
132
133
134
135
136
137
138
# File 'lib/test_bench/session/isolate.rb', line 130

def status
  if subprocess_id.nil?
    Status.stopped
  elsif telemetry_reader.closed? && file_path_writer.closed?
    Status.stopping
  else
    Status.active
  end
end

#stopObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/test_bench/session/isolate.rb', line 111

def stop
  stop!

  if subprocess_id.nil?
    return nil
  end

  subprocess_status = ::Process::Status.wait(subprocess_id)

  self.subprocess_id = nil

  subprocess_status.exitstatus
end

#stop!Object



125
126
127
128
# File 'lib/test_bench/session/isolate.rb', line 125

def stop!
  telemetry_reader&.close
  file_path_writer&.close
end