Project

General

Profile

Bug #20672 ยป test.rb

Show logic, including log of success vs failed run - danh337 (Dan H), 08/10/2024 02:41 AM

 
#!/usr/bin/env ruby

require "socket"

module Util
def log(*message_parts)
ts = Time.now
frame = caller_locations(1, 1)[0]
text = message_parts.map { |part| String === part ? part : part.inspect }.join
text.size > 100 and text = "#{text[0, 97]}..."
$stdout.write(
"[#{ts.strftime("%H:%M:%S.%6N")} #{$$}] " \
"#{text} " \
"[#{File.basename(frame.path)}:#{frame.lineno}]\n"
)
$stdout.flush
end

def new_line = UNIXSocket.pair

def send_data(data, writer) = Marshal.dump(data, writer)

def receive_data(reader, ignore_eof: true)
Marshal.load(reader)
rescue EOFError
ignore_eof ? nil : raise
end
end # Util

# Test driver, fails with larger values of `max_events` in `run` method.
#
class TestUnixSockets
include Util

def run(args)
max_events = (i = args.index("-c")) ? args.slice!(i, 2)[1].to_i : 3

# 1. Main sockets to share with child pids
main_p, main_c = new_line
log "main line ends shared across all pids ", [main_p, main_c]

# 2. New child pid
child_pid = Process.fork do
# 3. Clean unused copied line ends in this pid
main_p.close

# 4. Waiting for writer socket
log "child pid receiving writer socket"
received_w = UNIXSocket.for_fd(main_c.recv_io.fileno)
log "child pid got writer ", received_w.class, " ", received_w

# 5. Ack for received writer socket
log "child pid acking writer received"
send_data(:ok1, received_w)

# 6. Write to received socket
data = args[0] || "yoyoyo"
log "child pid writing data ", data.inspect
send_data(data, received_w)

# 7. Basic event loop
loop do
data = receive_data(main_c)
break if data.nil?
send_data("REPLY #{data}", received_w)
end
log "child pid done"
end
Process.detach(child_pid)
log "started child pid ", child_pid

# 8. Clean unused line ends in this pid
main_c.close

# 9. New sockets unknown to child pid
test_p, test_c = new_line
log "test lines unknown by child pid ", [test_p, test_c]

# 10. Send write socket to child pid
log "parent sending test writer ", test_c
main_p.send_io(test_c)

# 11. Receive ack for received writer
ok = receive_data(test_p)
log "parent received ack for writer ", ok

# 12. Receive data from child pid on test lines
log "parent receiving on test line ", test_p
data = receive_data(test_p)
log "parent got data ", data.inspect

# 13. Some test events
(1..max_events).each do |i|
data = Array.new(100) { i }.join(" ")
send_data(data, main_p)
log "parent sent ", data.inspect
data = receive_data(test_p)
log "parent got reply ", data.inspect
end

# 14. Stop child
main_p.close
log "parent closed main writer"
end
end # TestUnixSockets

if $0 == __FILE__
TestUnixSockets.new.run(ARGV.dup)
end

=begin
# [Sample output from a successful run and a failed run.]

dan@danbook:~/tmp/lab 1367 21:20:25
$ ./test.rb -c 5
[21:20:38.347217 49819] main line ends shared across all pids [#<UNIXSocket:fd 5>, #<UNIXSocket:fd 6>] [test.rb:200]
[21:20:38.347828 49819] started child pid 49835 [test.rb:230]
[21:20:38.347946 49819] test lines unknown by child pid [#<UNIXSocket:fd 6>, #<UNIXSocket:fd 7>] [test.rb:237]
[21:20:38.347967 49819] parent sending test writer #<UNIXSocket:fd 7> [test.rb:240]
[21:20:38.347938 49835] child pid receiving writer socket [test.rb:208]
[21:20:38.348047 49835] child pid got writer UNIXSocket #<UNIXSocket:fd 5> [test.rb:210]
[21:20:38.348065 49835] child pid acking writer received [test.rb:213]
[21:20:38.348100 49835] child pid writing data "yoyoyo" [test.rb:218]
[21:20:38.348118 49819] parent received ack for writer :ok1 [test.rb:245]
[21:20:38.348139 49819] parent receiving on test line #<UNIXSocket:fd 6> [test.rb:248]
[21:20:38.348160 49819] parent got data "yoyoyo" [test.rb:250]
[21:20:38.348342 49819] parent sent "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... [test.rb:256]
[21:20:38.348534 49819] parent got reply "REPLY 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1... [test.rb:258]
[21:20:38.348823 49819] parent sent "2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ... [test.rb:256]
[21:20:38.348880 49819] parent got reply "REPLY 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2... [test.rb:258]
[21:20:38.348958 49819] parent sent "3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ... [test.rb:256]
[21:20:38.349012 49819] parent got reply "REPLY 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3... [test.rb:258]
[21:20:38.349464 49819] parent sent "4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ... [test.rb:256]
[21:20:38.349503 49819] parent got reply "REPLY 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4... [test.rb:258]
[21:20:38.349535 49819] parent sent "5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ... [test.rb:256]
[21:20:38.349572 49819] parent got reply "REPLY 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5... [test.rb:258]
[21:20:38.349583 49819] parent closed main writer [test.rb:263]
[21:20:38.349721 49835] child pid done [test.rb:227]

dan@danbook:~/tmp/lab 1367 21:20:38
$ ./test.rb -c 40
[21:20:59.047263 49846] main line ends shared across all pids [#<UNIXSocket:fd 5>, #<UNIXSocket:fd 6>] [test.rb:200]
[21:20:59.048088 49846] started child pid 49862 [test.rb:230]
[21:20:59.048159 49862] child pid receiving writer socket [test.rb:208]
[21:20:59.048207 49846] test lines unknown by child pid [#<UNIXSocket:fd 6>, #<UNIXSocket:fd 7>] [test.rb:237]
[21:20:59.048227 49846] parent sending test writer #<UNIXSocket:fd 7> [test.rb:240]
[21:20:59.048295 49862] child pid got writer UNIXSocket #<UNIXSocket:fd 5> [test.rb:210]
[21:20:59.048316 49862] child pid acking writer received [test.rb:213]
[21:20:59.048352 49862] child pid writing data "yoyoyo" [test.rb:218]
[21:20:59.048369 49846] parent received ack for writer :ok1 [test.rb:245]
[21:20:59.048388 49846] parent receiving on test line #<UNIXSocket:fd 6> [test.rb:248]
[21:20:59.048417 49846] parent got data "yoyoyo" [test.rb:250]
[21:20:59.048592 49846] parent sent "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... [test.rb:256]
[21:20:59.048831 49846] parent got reply "REPLY 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1... [test.rb:258]
[21:20:59.049031 49846] parent sent "2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ... [test.rb:256]
[21:20:59.049117 49846] parent got reply "REPLY 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2... [test.rb:258]
[21:20:59.049170 49846] parent sent "3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ... [test.rb:256]
[21:20:59.049256 49846] parent got reply "REPLY 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3... [test.rb:258]
[21:20:59.049658 49846] parent sent "4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ... [test.rb:256]
[21:20:59.049708 49846] parent got reply "REPLY 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4... [test.rb:258]
[21:20:59.049739 49846] parent sent "5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ... [test.rb:256]
[21:20:59.049789 49846] parent got reply "REPLY 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5... [test.rb:258]
[21:20:59.049815 49846] parent sent "6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 ... [test.rb:256]
[21:20:59.049865 49846] parent got reply "REPLY 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6... [test.rb:258]
[21:20:59.049895 49846] parent sent "7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ... [test.rb:256]
[21:20:59.049933 49846] parent got reply "REPLY 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7... [test.rb:258]
[21:20:59.049960 49846] parent sent "8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 ... [test.rb:256]
[21:20:59.049998 49846] parent got reply "REPLY 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8... [test.rb:258]
[21:20:59.050027 49846] parent sent "9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 ... [test.rb:256]
[21:20:59.050065 49846] parent got reply "REPLY 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9... [test.rb:258]
[21:20:59.050091 49846] parent sent "10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ... [test.rb:256]
[21:20:59.050321 49846] parent got reply "REPLY 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1... [test.rb:258]
[21:20:59.050347 49846] parent sent "11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 ... [test.rb:256]
[21:20:59.050362 49846] parent got reply "REPLY 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 1... [test.rb:258]
[21:20:59.050400 49846] parent sent "12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 ... [test.rb:256]
[21:20:59.050413 49846] parent got reply "REPLY 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 1... [test.rb:258]
[21:20:59.050436 49846] parent sent "13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 ... [test.rb:256]
[21:20:59.050456 49846] parent got reply "REPLY 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 1... [test.rb:258]
[21:20:59.050485 49846] parent sent "14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 ... [test.rb:256]
[21:20:59.050502 49846] parent got reply "REPLY 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 1... [test.rb:258]
[21:20:59.050533 49846] parent sent "15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 ... [test.rb:256]
[21:20:59.050545 49846] parent got reply "REPLY 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 1... [test.rb:258]
[21:20:59.050572 49846] parent sent "16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 ... [test.rb:256]
[21:20:59.050584 49846] parent got reply "REPLY 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 1... [test.rb:258]
[21:20:59.050613 49846] parent sent "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 ... [test.rb:256]
[21:20:59.050628 49846] parent got reply "REPLY 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 1... [test.rb:258]
[21:20:59.050653 49846] parent sent "18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 ... [test.rb:256]
[21:20:59.050666 49846] parent got reply "REPLY 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 1... [test.rb:258]
[21:20:59.050727 49846] parent sent "19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 ... [test.rb:256]
[21:20:59.050741 49846] parent got reply "REPLY 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 1... [test.rb:258]
[21:20:59.050765 49846] parent sent "20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ... [test.rb:256]
[21:20:59.050777 49846] parent got reply "REPLY 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2... [test.rb:258]
[21:20:59.050821 49846] parent sent "21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 ... [test.rb:256]
[21:20:59.050841 49846] parent got reply "REPLY 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 2... [test.rb:258]
[21:20:59.050865 49846] parent sent "22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 ... [test.rb:256]
[21:20:59.050904 49846] parent got reply "REPLY 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 2... [test.rb:258]
[21:20:59.050928 49846] parent sent "23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 ... [test.rb:256]
./test.rb:187:in `write': Bad file descriptor (Errno::EBADF)
from ./test.rb:187:in `dump'
from ./test.rb:187:in `send_data'
from ./test.rb:225:in `block (2 levels) in run'
from <internal:kernel>:187:in `loop'
from ./test.rb:222:in `block in run'
from ./test.rb:203:in `fork'
from ./test.rb:203:in `run'
from ./test.rb:268:in `<main>'
^C<internal:marshal>:34:in `getbyte': Interrupt
from <internal:marshal>:34:in `load'
from ./test.rb:190:in `receive_data'
from ./test.rb:257:in `block in run'
from ./test.rb:253:in `each'
from ./test.rb:253:in `run'
from ./test.rb:268:in `<main>'

=end
    (1-1/1)