File: names_test.go

package info (click to toggle)
golang-go-dbus 1~bzr20150122-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 328 kB
  • sloc: makefile: 13
file content (128 lines) | stat: -rw-r--r-- 3,339 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
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
package dbus

import (
	. "gopkg.in/check.v1"
)

func (s *S) TestConnectionWatchName(c *C) {
	bus, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus.Close()

	// Set up the name watch
	nameChanged := make(chan int, 1)
	owners := []string{}
	watch, err := bus.WatchName("com.example.GoDbus")
	c.Assert(err, IsNil)
	defer watch.Cancel()
	go func() {
		for newOwner := range watch.C {
			owners = append(owners, newOwner)
			nameChanged <- 0
		}
	}()

	// Our handler will be called once with the initial name owner
	<-nameChanged
	c.Check(owners, DeepEquals, []string{""})

	// Acquire the name, and wait for the process to complete.
	name := bus.RequestName("com.example.GoDbus", NameFlagDoNotQueue)
	c.Check(<-name.C, IsNil)

	<-nameChanged
	c.Check(owners, DeepEquals, []string{"", bus.UniqueName})

	err = name.Release()
	c.Assert(err, IsNil)
	<-nameChanged
	c.Check(owners, DeepEquals, []string{"", bus.UniqueName, ""})
}

func (s *S) TestConnectionRequestName(c *C) {
	bus, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus.Close()

	name := bus.RequestName("com.example.GoDbus", 0)
	c.Check(name, NotNil)
	c.Check(<-name.C, IsNil)

	owner, err := bus.busProxy.GetNameOwner("com.example.GoDbus")
	c.Check(err, IsNil)
	c.Check(owner, Equals, bus.UniqueName)

	c.Check(name.Release(), IsNil)
}

func (s *S) TestConnectionRequestNameQueued(c *C) {
	// Acquire the name on a second connection
	bus1, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus1.Close()

	bus2, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus2.Close()

	name1 := bus1.RequestName("com.example.GoDbus", 0)
	c.Check(<-name1.C, IsNil)
	c.Check(name1.checkNeedsRelease(), Equals, true)

	name2 := bus2.RequestName("com.example.GoDbus", 0)
	c.Check(<-name2.C, Equals, ErrNameInQueue)
	c.Check(name2.checkNeedsRelease(), Equals, true)

	// Release the name on the first connection
	c.Check(name1.Release(), IsNil)

	// And the second connection can now acquire it
	c.Check(<-name2.C, IsNil)
	c.Check(name2.Release(), IsNil)
}

func (s *S) TestConnectionRequestNameDoNotQueue(c *C) {
	// Acquire the name on a second connection
	bus1, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus1.Close()

	bus2, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus2.Close()

	name1 := bus1.RequestName("com.example.GoDbus", 0)
	defer name1.Release()
	c.Check(<-name1.C, IsNil)
	c.Check(name1.checkNeedsRelease(), Equals, true)

	name2 := bus2.RequestName("com.example.GoDbus", NameFlagDoNotQueue)
	c.Check(<-name2.C, Equals, ErrNameExists)
	c.Check(name2.checkNeedsRelease(), Equals, false)

	c.Check(name2.Release(), IsNil)
}

func (s *S) TestConnectionRequestNameAllowReplacement(c *C) {
	// Acquire the name on a second connection
	bus1, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus1.Close()

	bus2, err := Connect(SessionBus)
	c.Assert(err, IsNil)
	defer bus2.Close()

	name1 := bus1.RequestName("com.example.GoDbus", NameFlagAllowReplacement)
	defer name1.Release()
	c.Check(<-name1.C, IsNil)
	c.Check(name1.checkNeedsRelease(), Equals, true)

	name2 := bus2.RequestName("com.example.GoDbus", NameFlagReplaceExisting)
	defer name2.Release()
	c.Check(<-name2.C, IsNil)
	c.Check(name2.checkNeedsRelease(), Equals, true)

	// The first name owner loses possession.
	c.Check(<-name1.C, Equals, ErrNameLost)
}