-
Notifications
You must be signed in to change notification settings - Fork 50
/
error_test.go
102 lines (95 loc) · 2.57 KB
/
error_test.go
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
package libvirt
import (
"reflect"
"testing"
)
func TestGetLastError(t *testing.T) {
_, err := NewVirConnection("invalid_transport:///default")
if err == nil {
t.Fatalf("Expected an error when creating invalid connection")
}
got := GetLastError()
expected := VirError{0, 0, "", 0}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected error %+v, got %+v", expected, got)
}
if got != ErrNoError {
t.Errorf("Expected error to be ErrNoError")
}
}
func TestGlobalErrorCallback(t *testing.T) {
var nbErrors int
errors := make([]VirError, 0, 10)
callback := ErrorCallback(func(err VirError, f func()) {
errors = append(errors, err)
f()
})
SetErrorFunc(callback, func() {
nbErrors++
})
NewVirConnection("invalid_transport:///default")
if len(errors) == 0 {
t.Errorf("No errors were captured")
}
if len(errors) != nbErrors {
t.Errorf("Captured %d errors (%+v) but counted only %d errors",
len(errors), errors, nbErrors)
}
errors = make([]VirError, 0, 10)
SetErrorFunc(nil, nil)
NewVirConnection("invalid_transport:///default")
if len(errors) != 0 {
t.Errorf("More errors have been captured: %+v", errors)
}
}
func TestConnectionErrorCallback(t *testing.T) {
var nbErrors int
initialConnectionsLen := len(connections)
errors := make([]VirError, 0, 10)
callback := ErrorCallback(func(err VirError, f func()) {
errors = append(errors, err)
f()
})
conn := buildTestConnection()
conn.SetErrorFunc(callback, func() {
nbErrors++
})
defer func() {
if res, _ := conn.CloseConnection(); res != 0 {
t.Errorf("CloseConnection() == %d, expected 0", res)
}
if len(connections) != initialConnectionsLen {
t.Errorf("%d connections data leaked",
len(connections)-initialConnectionsLen)
}
}()
// To generate an error, we set memory of a domain to an insance value
domain, err := conn.LookupDomainByName("test")
if err != nil {
panic(err)
}
defer domain.Free()
err = domain.SetMemory(100000000000)
if err == nil {
t.Fatalf("Was expecting an error when setting memory to too high value")
}
if len(errors) == 0 {
t.Errorf("No errors were captured")
}
if len(errors) != nbErrors {
t.Errorf("Captured %d errors (%+v) but counted only %d errors",
len(errors), errors, nbErrors)
}
errors = make([]VirError, 0, 10)
conn.UnsetErrorFunc()
if len(goCallbacks) != 0 {
t.Errorf("goCallbacks entry wasn't removed: %+v", goCallbacks)
}
err = domain.SetMemory(100000000000)
if err == nil {
t.Fatalf("Was expecting an error when setting memory to too high value")
}
if len(errors) != 0 {
t.Errorf("More errors have been captured: %+v", errors)
}
}