Class: RSpec::CapturingFormatter::CaptureManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/capturing_formatter/stream_proxy.rb

Overview

Owns process-global stream capture and serializes captured writes with formatter callbacks.

Defined Under Namespace

Classes: Lease

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ CaptureManager

Returns a new instance of CaptureManager.



30
31
32
33
34
35
36
37
38
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 30

def initialize
  # Renderer writes can re-enter through a proxy, so capture synchronization must be reentrant.
  @monitor = Monitor.new
  @installed = false
  @active = false
  @pending_nonblock = {}
  @generation = 0
  @bypass_key = :rspec_capturing_formatter_bypass_depth
end

Instance Attribute Details

#stderr_proxy ⇒ Object (readonly)

Returns the value of attribute stderr_proxy.



28
29
30
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 28

def stderr_proxy
  @stderr_proxy
end

#stdout_proxy ⇒ Object (readonly)

Returns the value of attribute stdout_proxy.



28
29
30
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 28

def stdout_proxy
  @stdout_proxy
end

Class Method Details

.install! ⇒ Object



23
24
25
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 23

def install!
  instance.install!
end

.instance ⇒ Object



19
20
21
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 19

def instance
  @instance ||= new
end

Instance Method Details

#activate(output, formatter) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 55

def activate(output, formatter)
  existing = @monitor.synchronize { active_lease }
  return existing if existing

  install!
  @monitor.synchronize do
    existing = active_lease
    return existing if existing

    begin
      @formatter = formatter
      @active = true
      @generation += 1
      @stdout_proxy.activate!
      @stderr_proxy.activate!
      Lease.new(manager: self, owner: true, generation: @generation)
    # Activation mutates process-global capture state, so roll it back even outside StandardError.
    # standard:disable Lint/RescueException
    rescue Exception
      # standard:enable Lint/RescueException
      @active = false
      @formatter = nil
      @stdout_proxy.deactivate!
      @stderr_proxy.deactivate!
      restore_globals
      raise
    end
  end
end

#active? ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 85

def active?
  @monitor.synchronize { @active }
end

#bypass ⇒ Object



129
130
131
132
133
134
135
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 129

def bypass
  depth = Thread.current[@bypass_key].to_i
  Thread.current[@bypass_key] = depth + 1
  yield
ensure
  Thread.current[@bypass_key] = depth
end

#bypassing? ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 137

def bypassing?
  Thread.current[@bypass_key].to_i.positive?
end

#deactivate(lease) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 141

def deactivate(lease)
  return unless lease&.owner?
  return unless lease.manager.equal?(self)

  @monitor.synchronize do
    return unless @active && lease.generation == @generation
    begin
      @formatter.finish_capture
    ensure
      @active = false
      @formatter = nil
      @pending_nonblock.clear
      @stdout_proxy.deactivate!
      @stderr_proxy.deactivate!
      restore_globals
    end
  end
end

#generation ⇒ Object



89
90
91
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 89

def generation
  @monitor.synchronize { @generation }
end

#handle_write(proxy, value, method = :write, **options) ⇒ Object



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/capturing_formatter/stream_proxy.rb', line 97

def handle_write(proxy, value, method = :write, **options)
  @monitor.synchronize do
    pending = @pending_nonblock[proxy]
    if pending
      if nonblocking_method?(method) && pending != value
        raise Errno::EAGAIN
      end
      # This payload is already captured; remove its marker while a bypassed flush may re-enter here.
      @pending_nonblock.delete(proxy)
      begin
        @formatter.flush_pending
      rescue IO::WaitWritable, Errno::EAGAIN
        @pending_nonblock[proxy] = pending
        raise
      end
      return value.bytesize if nonblocking_method?(method) && pending == value
    end

    if !@active || bypassing? || proxy.raw_mode?
      proxy.write_backing(value, method, **options)
    else
      begin
        @formatter.capture(proxy.source, value, value.encoding)
      rescue IO::WaitWritable, Errno::EAGAIN
        @pending_nonblock[proxy] = value if nonblocking_method?(method)
        raise
      end
      value.bytesize
    end
  end
end

#install! ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 40

def install!
  @monitor.synchronize do
    return self if @installed && $stdout.equal?(@stdout_proxy) && $stderr.equal?(@stderr_proxy)

    @original_stdout ||= $stdout
    @original_stderr ||= $stderr
    @stdout_proxy ||= StreamProxy.new(@original_stdout, :stdout, self)
    @stderr_proxy ||= StreamProxy.new(@original_stderr, :stderr, self)
    $stdout = @stdout_proxy
    $stderr = @stderr_proxy
    @installed = true
  end
  self
end

#restore! ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 160

def restore!
  @monitor.synchronize do
    @active = false
    @formatter = nil
    @pending_nonblock.clear
    @stdout_proxy&.deactivate!
    @stderr_proxy&.deactivate!
    restore_globals
  end
end

#rollback_if_unchanged(expected_generation) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 171

def rollback_if_unchanged(expected_generation)
  @monitor.synchronize do
    return if @active || @generation != expected_generation

    restore_globals
  end
end

#synchronize(&block) ⇒ Object



93
94
95
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 93

def synchronize(&block)
  @monitor.synchronize(&block)
end

#uninstall! ⇒ Object



179
180
181
# File 'lib/rspec/capturing_formatter/stream_proxy.rb', line 179

def uninstall!
  restore!
end