File: signal_spec.rb

package info (click to toggle)
ruby-dbus 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 520 kB
  • sloc: ruby: 3,786; sh: 53; makefile: 8
file content (104 lines) | stat: -rwxr-xr-x 2,501 bytes parent folder | download | duplicates (2)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env rspec
# Test the signal handlers
require_relative "spec_helper"
require "dbus"

def new_quitter(main_loop)
  Thread.new do
    DBus.logger.debug "sleep before quit"
    # FIXME: if we sleep for too long
    # the socket will be drained and we deadlock in a select.
    # It could be worked around by sending ourselves a Unix signal
    # (with a dummy handler) to interrupt the select
    sleep 1
    DBus.logger.debug "will quit"
    main_loop.quit
  end
end

describe "SignalHandlerTest" do
  before(:each) do
    @session_bus = DBus::ASessionBus.new
    svc = @session_bus.service("org.ruby.service")
    @obj = svc.object("/org/ruby/MyInstance")
    @obj.default_iface = "org.ruby.Loop"
    @intf = @obj["org.ruby.Loop"]

    @loop = DBus::Main.new
    @loop << @session_bus
  end

  # testing for commit 017c83 (kkaempf)
  it "tests overriding a handler" do
    DBus.logger.debug "Inside test_overriding_a_handler"
    counter = 0

    @obj.on_signal "LongTaskEnd" do
      DBus.logger.debug "+10"
      counter += 10
    end
    @obj.on_signal "LongTaskEnd" do
      DBus.logger.debug "+1"
      counter += 1
    end

    DBus.logger.debug "will begin"
    @obj.LongTaskBegin 3

    quitter = new_quitter(@loop)
    @loop.run
    quitter.join

    expect(counter).to eq(1)
  end

  it "tests on signal overload" do
    DBus.logger.debug "Inside test_on_signal_overload"
    counter = 0
    started = false
    @intf.on_signal "LongTaskStart" do
      started = true
    end
    # Same as intf.on_signal("LongTaskEnd"), just the old way
    @intf.on_signal @obj.bus, "LongTaskEnd" do
      counter += 1
    end
    @obj.LongTaskBegin 3
    quitter = new_quitter(@loop)
    @loop.run
    quitter.join

    expect(started).to eq(true)
    expect(counter).to eq(1)
    expect { @intf.on_signal }.to raise_error(ArgumentError) # not enough
    expect { @intf.on_signal "to", "many", "yarrrrr!" }.to raise_error(ArgumentError)
  end

  it "is possible to add signal handlers from within handlers" do
    ended = false
    @intf.on_signal "LongTaskStart" do
      @intf.on_signal "LongTaskEnd" do
        ended = true
      end
    end

    @obj.LongTaskBegin 3
    quitter = new_quitter(@loop)
    @loop.run
    quitter.join

    expect(ended).to eq(true)
  end

  it "tests too many rules" do
    100.times do
      @obj.on_signal "Whichever" do
        puts "not called"
      end
    end
  end

  it "tests removing a nonexistent rule" do
    @obj.on_signal "DoesNotExist"
  end
end