File: signal_spec.cr

package info (click to toggle)
crystal 1.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,956 kB
  • sloc: javascript: 1,712; sh: 592; cpp: 541; makefile: 243; ansic: 119; python: 105; xml: 32
file content (77 lines) | stat: -rw-r--r-- 1,616 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
65
66
67
68
69
70
71
72
73
74
75
76
77
{% skip_file if flag?(:win32) || flag?(:wasm32) %}

require "spec"
require "signal"

describe "Signal" do
  typeof(Signal::PIPE.reset)
  typeof(Signal::PIPE.ignore)
  typeof(Signal::PIPE.trap { 1 })

  it "runs a signal handler" do
    ran = false
    Signal::USR1.trap do
      ran = true
    end
    Process.signal Signal::USR1, Process.pid
    10.times do |i|
      break if ran
      sleep 0.1
    end
    ran.should be_true
  end

  it "ignores a signal" do
    Signal::USR2.ignore
    Process.signal Signal::USR2, Process.pid
  end

  it "CHLD.reset sets default Crystal child handler" do
    Signal::CHLD.reset
    child = Process.new("true", shell: true)
    child.wait # doesn't block forever
  end

  it "CHLD.ignore sets default Crystal child handler" do
    Signal::CHLD.ignore
    child = Process.new("true", shell: true)
    child.wait # doesn't block forever
  end

  it "CHLD.trap is called after default Crystal child handler" do
    chan = Channel(Process).new

    Signal::CHLD.trap do
      child_process = chan.receive
      Process.exists?(child_process.pid).should be_false
    end

    child = Process.new("true", shell: true)
    child.wait # doesn't block forever
    chan.send(child)
  ensure
    Signal::CHLD.reset
  end

  it "CHLD.reset removes previously set trap" do
    call_count = 0

    Signal::CHLD.trap do
      call_count += 1
    end

    Process.new("true", shell: true).wait
    Fiber.yield

    call_count.should eq(1)

    Signal::CHLD.reset

    Process.new("true", shell: true).wait
    Fiber.yield

    call_count.should eq(1)
  end

  # TODO: test Signal::X.reset
end