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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
package dbus
import (
. "gopkg.in/check.v1"
)
func (s *S) TestConnectionWatchSignal(c *C) {
bus1, err := Connect(SessionBus)
c.Assert(err, IsNil)
defer bus1.Close()
// Set up a second bus connection to receive a signal.
watchReady := make(chan int)
complete := make(chan *Message)
go func(sender string, watchReady chan<- int, complete chan<- *Message) {
bus2, err := Connect(SessionBus)
if err != nil {
c.Error(err)
watchReady <- 0
complete <- nil
return
}
defer bus2.Close()
watch, err := bus2.WatchSignal(&MatchRule{
Type: TypeSignal,
Sender: sender,
Path: "/go/dbus/test",
Interface: "com.example.GoDbus",
Member: "TestSignal"})
watchReady <- 0
if err != nil {
c.Error(err)
bus2.Close()
complete <- nil
return
}
msg := <-watch.C
if err := watch.Cancel(); err != nil {
c.Error(err)
}
complete <- msg
}(bus1.UniqueName, watchReady, complete)
// Wait for the goroutine to configure the signal watch
<-watchReady
// Send the signal and wait for it to be received at the other end.
signal := NewSignalMessage("/go/dbus/test", "com.example.GoDbus", "TestSignal")
if err := bus1.Send(signal); err != nil {
c.Fatal(err)
}
signal2 := <-complete
c.Check(signal2, NotNil)
}
func (s *S) TestConnectionWatchSignalWithBusName(c *C) {
bus, err := Connect(SessionBus)
c.Assert(err, IsNil)
defer bus.Close()
// Request a bus name
result, err := bus.busProxy.RequestName("com.example.GoDbus", 0x4)
c.Assert(err, IsNil)
c.Assert(result, Equals, uint32(1)) // We are Primary Owner
// Set up a signal watch
received := make(chan *Message, 1)
watch, err := bus.WatchSignal(&MatchRule{
Type: TypeSignal,
Sender: "com.example.GoDbus",
Interface: "com.example.GoDbus",
Member: "TestSignal"})
c.Assert(err, IsNil)
defer watch.Cancel()
// pump received signals messages into our bufferred channel
go func() {
for msg := range watch.C {
received <- msg
}
}()
// Send the signal, and wait to receive it.
signal := NewSignalMessage("/go/dbus/test", "com.example.GoDbus", "TestSignal")
if err := bus.Send(signal); err != nil {
c.Fatal(err)
}
signal2 := <-received
c.Check(signal2, NotNil)
}
func (s *S) TestSignalWatchSetAdd(c *C) {
set := make(signalWatchSet)
watch := signalWatch{rule: MatchRule{
Type: TypeSignal,
Sender: ":1.42",
Path: "/foo",
Interface: "com.example.Foo",
Member: "Bar"}}
set.Add(&watch)
byInterface, ok := set["/foo"]
c.Assert(ok, Equals, true)
byMember, ok := byInterface["com.example.Foo"]
c.Assert(ok, Equals, true)
watches, ok := byMember["Bar"]
c.Assert(ok, Equals, true)
c.Check(watches, DeepEquals, []*signalWatch{&watch})
}
func (s *S) TestSignalWatchSetRemove(c *C) {
set := make(signalWatchSet)
watch1 := signalWatch{rule: MatchRule{
Type: TypeSignal,
Sender: ":1.42",
Path: "/foo",
Interface: "com.example.Foo",
Member: "Bar"}}
set.Add(&watch1)
watch2 := signalWatch{rule: MatchRule{
Type: TypeSignal,
Sender: ":1.43",
Path: "/foo",
Interface: "com.example.Foo",
Member: "Bar"}}
set.Add(&watch2)
c.Check(set.Remove(&watch1), Equals, true)
c.Check(set["/foo"]["com.example.Foo"]["Bar"], DeepEquals, []*signalWatch{&watch2})
// A second attempt at removal fails
c.Check(set.Remove(&watch1), Equals, false)
}
func (s *S) TestSignalWatchSetFindMatches(c *C) {
msg := NewSignalMessage("/foo", "com.example.Foo", "Bar")
msg.Sender = ":1.42"
set := make(signalWatchSet)
watch := signalWatch{rule: MatchRule{
Type: TypeSignal,
Sender: ":1.42",
Path: "/foo",
Interface: "com.example.Foo",
Member: "Bar"}}
set.Add(&watch)
c.Check(set.FindMatches(msg), DeepEquals, []*signalWatch{&watch})
set.Remove(&watch)
// An empty path also matches
watch.rule.Path = ""
set.Add(&watch)
c.Check(set.FindMatches(msg), DeepEquals, []*signalWatch{&watch})
set.Remove(&watch)
// Or an empty interface
watch.rule.Path = "/foo"
watch.rule.Interface = ""
set.Add(&watch)
c.Check(set.FindMatches(msg), DeepEquals, []*signalWatch{&watch})
set.Remove(&watch)
// Or an empty member
watch.rule.Interface = "com.example.Foo"
watch.rule.Member = ""
set.Add(&watch)
c.Check(set.FindMatches(msg), DeepEquals, []*signalWatch{&watch})
set.Remove(&watch)
}
|