Class: Cybertrain::Test::Receiver

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/test/receiver.rb

Overview

The base of HttpReceiver and SmtpReceiver.

Direct Known Subclasses

HttpReceiver, SmtpReceiver, WebSocketReceiver

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#port ⇒ Integer (readonly)

Returns the bound port, 0 before #start.

Returns:

  • (Integer) —

    the bound port, 0 before #start



40
41
42
# File 'cybertrain/test/receiver.rb', line 40

def port
  @port
end

Instance Method Details

#serving? ⇒ Boolean

Whether the thread is still accepting or serving.

Returns:

  • (Boolean)


99
100
101
# File 'cybertrain/test/receiver.rb', line 99

def serving?
  @serving
end

#start(port: 0) ⇒ Integer

Binds 127.0.0.1:port (0 for any free port) and starts the thread that accepts the connections. Returns once listening.

Parameters:

  • port (Integer) (defaults to: 0)

Returns:

  • (Integer) —

    the bound port

Raises:

  • (ArgumentError) —

    when already started



70
71
72
73
74
75
76
77
78
79
80
81
# File 'cybertrain/test/receiver.rb', line 70

def start(port: 0)
  raise ArgumentError, "the receiver is already started" if @serving

  # spikes/NOTES.md rule 22: before the first Thread.new.
  ENV["SPINEL_WORKERS"] = "1" unless ENV["SPINEL_WORKERS"]
  listener = TCPServer.new("127.0.0.1", port.to_i)
  @port = listener.addr[1].to_i
  @stopping = false
  @serving = true
  @thread = rx_spawn(listener)
  @port
end

#stop ⇒ nil

Stops accepting, ends the connection in progress at its next wait, and joins the thread (at most read_timeout + 1 seconds). What has arrived stays readable. Safe to call twice, or before #start.

Returns:

  • (nil)


88
89
90
91
92
93
94
# File 'cybertrain/test/receiver.rb', line 88

def stop
  @stopping = true
  thread = @thread
  thread.join(@read_timeout + 1.0) unless thread.nil?
  @thread = nil
  nil
end