Repository navigation
Expand file tree
/
Copy pathclient_test.go
More file actions
103 lines (82 loc) · 2.12 KB
/
Copy pathclient_test.go
File metadata and controls
103 lines (82 loc) · 2.12 KB
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
// CGo binding for Avahi
//
// Copyright (C) 2024 and up by Alexander Pevzner (pzz@apevzner.com)
// See LICENSE for license terms and conditions
//
// Avahi Client tests
//go:build linux || freebsd
package avahi
import (
"context"
"testing"
"time"
)
// newTestClient creates a Client with a timeout, so tests fail instead of
// blocking indefinitely if the Avahi daemon is unavailable.
func newTestClient(t *testing.T) *Client {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
clnt, err := NewClientWait(ctx, ClientLoopbackWorkarounds)
if err != nil {
t.Fatal(err)
}
return clnt
}
// mustPanicWith tests that function f panics with the expected
// reason.
func mustPanicWith(t *testing.T, want string, f func()) {
t.Helper()
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected panic %q", want)
}
if r != want {
t.Fatalf("unexpected panic: got %q, want %q", r, want)
}
}()
f()
}
// TestClientClose tests that closing a Client more than once is safe.
func TestClientClose(t *testing.T) {
clnt := newTestClient(t)
clnt.Close()
clnt.Close()
}
// TestClientUseAfterClose tests that using a Client after Close panics.
func TestClientUseAfterClose(t *testing.T) {
clnt := newTestClient(t)
clnt.Close()
mustPanicWith(t, "Client used after Client.Close()", func() {
clnt.GetVersionString()
})
}
// TestNewChildAfterClientClose tests that creating a child object after
// Client.Close panics.
func TestNewChildAfterClientClose(t *testing.T) {
clnt := newTestClient(t)
clnt.Close()
mustPanicWith(t, "Client used after Client.Close()", func() {
_, _ = NewEntryGroup(clnt)
})
}
// TestClientCloseClosesChildren tests that Client.Close closes existing
// child objects.
func TestClientCloseClosesChildren(t *testing.T) {
clnt := newTestClient(t)
egrp, err := NewEntryGroup(clnt)
if err != nil {
t.Fatal(err)
}
clnt.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
evnt, err := egrp.Get(ctx)
if err != nil {
t.Fatal(err)
}
if evnt != nil {
t.Fatalf("unexpected EntryGroup event: %v", evnt)
}
}