forked from mvidner/ruby-dbus
/
signal_test.rb
executable file
·91 lines (80 loc) · 2.25 KB
/
signal_test.rb
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
#!/usr/bin/env ruby
# Test the signal handlers
require File.expand_path("../test_helper", __FILE__)
require "test/unit"
require "dbus"
class SignalHandlerTest < Test::Unit::TestCase
def setup
@session_bus = DBus::ASessionBus.new
svc = @session_bus.service("org.ruby.service")
@obj = svc.object("/org/ruby/MyInstance")
@obj.introspect # necessary
@obj.default_iface = "org.ruby.Loop"
@intf = @obj["org.ruby.Loop"]
@loop = DBus::Main.new
@loop << @session_bus
end
# testing for commit 017c83 (kkaempf)
def test_overriding_a_handler
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 = 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"
@loop.quit
end
@loop.run
quitter.join
assert_equal 1, counter
end
def test_on_signal_overload
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 = Thread.new do
DBus.logger.debug "sleep before quit"
sleep 1
DBus.logger.debug "will quit"
@loop.quit
end
@loop.run
quitter.join
assert_equal true, started
assert_equal 1, counter
assert_raise(ArgumentError) {@intf.on_signal } # not enough
assert_raise(ArgumentError) {@intf.on_signal 'to', 'many', 'yarrrrr!'}
end
def test_too_many_rules
100.times do
@obj.on_signal "Whichever" do
puts "not called"
end
end
end
def test_removing_a_nonexistent_rule
@obj.on_signal "DoesNotExist"
end
end