Class: Nonnative::FaultInjectionProxy
- Defined in:
- lib/nonnative/fault_injection_proxy.rb
Overview
Fault-injection proxy for TCP services.
This proxy accepts incoming TCP connections and forwards traffic to the configured upstream (service.proxy.host / service.proxy.port) via a socket-pair implementation. It can also inject failures to help validate client resilience.
This class exposes a small public control surface for tests:
-
#close_all: close connections immediately on accept
-
#delay: delay reads by a configured duration (default: 2 seconds)
-
#invalid_data: corrupt outbound data by shuffling characters
-
#reset: return to healthy pass-through behavior
State changes terminate any active connections so new connections observe the new behavior.
## Wiring
When enabled, your test/client should typically connect to #host:#port (the proxy endpoint), and the proxy will connect onward to the underlying service.
## Configuration
The proxy is configured via the runner’s proxy hash:
Instance Method Summary collapse
-
#close_all ⇒ void
Forces new connections to be closed immediately.
-
#delay ⇒ void
Delays reads before forwarding.
-
#host ⇒ String
Returns the host clients should connect to when using this proxy.
-
#initialize(service) ⇒ FaultInjectionProxy
constructor
A new instance of FaultInjectionProxy.
-
#invalid_data ⇒ void
Corrupts forwarded data by shuffling characters.
-
#port ⇒ Integer
Returns the port clients should connect to when using this proxy.
-
#reset ⇒ void
Resets the proxy back to healthy pass-through behavior.
-
#start ⇒ void
Starts the proxy accept loop in a background thread.
-
#stop ⇒ void
Stops the proxy and closes its listening socket.
Constructor Details
#initialize(service) ⇒ FaultInjectionProxy
Returns a new instance of FaultInjectionProxy.
39 40 41 42 43 44 45 46 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 39 def initialize(service) @connections = Concurrent::Hash.new @logger = Logger.new(service.proxy.log) @mutex = Mutex.new @state = :none super end |
Instance Method Details
#close_all ⇒ void
This method returns an undefined value.
Forces new connections to be closed immediately.
74 75 76 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 74 def close_all apply_state :close_all end |
#delay ⇒ void
This method returns an undefined value.
Delays reads before forwarding.
The delay duration is controlled by service.proxy.options[:delay] and defaults to 2 seconds.
83 84 85 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 83 def delay apply_state :delay end |
#host ⇒ String
Returns the host clients should connect to when using this proxy.
104 105 106 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 104 def host service.proxy.host end |
#invalid_data ⇒ void
This method returns an undefined value.
Corrupts forwarded data by shuffling characters.
90 91 92 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 90 def invalid_data apply_state :invalid_data end |
#port ⇒ Integer
Returns the port clients should connect to when using this proxy.
111 112 113 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 111 def port service.proxy.port end |
#reset ⇒ void
This method returns an undefined value.
Resets the proxy back to healthy pass-through behavior.
97 98 99 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 97 def reset apply_state :none end |
#start ⇒ void
54 55 56 57 58 59 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 54 def start @tcp_server = ::TCPServer.new(service.host, service.port) @thread = Thread.new { perform_start } Nonnative.logger.info "started with host '#{service.host}' and port '#{service.port}' for proxy 'fault_injection'" end |
#stop ⇒ void
This method returns an undefined value.
Stops the proxy and closes its listening socket.
64 65 66 67 68 69 |
# File 'lib/nonnative/fault_injection_proxy.rb', line 64 def stop thread&.terminate tcp_server&.close Nonnative.logger.info "stopped with host '#{service.host}' and port '#{service.port}' for proxy 'fault_injection'" end |