-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
85 lines (70 loc) · 1.58 KB
/
client_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
package icinga2
import (
"encoding/json"
"github.com/bytemine/go-icinga2/event"
"log"
"sync"
"testing"
"time"
)
// ExampleVagrant connects to a icinga2 vagrant box, found at
// https://github.com/Icinga/icinga-vagrant.git
// Use icinga2x and add port 5665 to the forwarded ports.
func ExampleVagrant(t *testing.T) {
i, err := NewClient("https://localhost:5665", "root", "icinga", true)
if err != nil {
t.Fatal(err)
}
// Get an event stream for StateChange and CheckResult events.
es, err := i.EventStream("testing", "", event.StreamTypeStateChange, event.StreamTypeCheckResult)
if err != nil {
t.Fatal(err)
}
// Multiplex StateChange and CheckResult events into seperate readers.
m := event.NewMux(es, event.StreamTypeStateChange, event.StreamTypeCheckResult)
// Get the reader for StateChange events.
st, err := m.Reader(event.StreamTypeStateChange)
if err != nil {
t.Fatal(err)
}
// Get the reader for CheckResult events.
cr, err := m.Reader(event.StreamTypeCheckResult)
if err != nil {
t.Fatal(err)
}
// Receive the events with seperate goroutines.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
decx := json.NewDecoder(st)
var x event.StateChange
for {
err := decx.Decode(&x)
if err != nil {
return
}
log.Printf("%#v", x)
}
return
}()
wg.Add(1)
go func() {
defer wg.Done()
decy := json.NewDecoder(cr)
var y event.CheckResult
for {
err := decy.Decode(&y)
if err != nil {
return
}
log.Printf("%#v", y)
}
return
}()
go func() {
time.Sleep(time.Second * 5)
m.Close()
}()
wg.Wait()
}