-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_helper_test.go
117 lines (96 loc) · 2.7 KB
/
group_helper_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package hue
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"sync"
"testing"
"time"
)
func TestGroupService_TurnOn(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
bytes, _ := ioutil.ReadFile("testdata/Group_TurnOn.json")
mux.HandleFunc(fmt.Sprintf("/username/groups/%s/action", testGroupId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(bytes))
})
ctx := context.Background()
err := client.Groups.TurnOn(ctx, testGroupId)
if err != nil {
t.Errorf("Group.TurnOn returned error: %+v", err)
}
}
func TestGroupService_TurnOff(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
bytes, _ := ioutil.ReadFile("testdata/Group_TurnOff.json")
mux.HandleFunc(fmt.Sprintf("/username/groups/%s/action", testGroupId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(bytes))
})
ctx := context.Background()
err := client.Groups.TurnOff(ctx, testGroupId)
if err != nil {
t.Errorf("Group.TurnOff returned error: %+v", err)
}
}
func TestGroupService_TurnOnAll(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
var wg sync.WaitGroup
groupIds := []string{"1", "2", "3"}
bytes, _ := ioutil.ReadFile("testdata/Group_TurnOn.json")
for _, groupId := range groupIds {
wg.Add(1)
mux.HandleFunc(fmt.Sprintf("/username/groups/%s/action", groupId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(bytes))
wg.Done()
})
}
ctx := context.Background()
client.Groups.TurnOnAll(ctx, groupIds...)
done := make(chan bool)
go func() {
wg.Wait()
done <- true
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Errorf("Failed to turn on all groups on time")
}
}
func TestGroupService_TurnOffAll(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
var wg sync.WaitGroup
groupIds := []string{"1", "2", "3"}
bytes, _ := ioutil.ReadFile("testdata/Group_TurnOff.json")
for _, groupId := range groupIds {
wg.Add(1)
mux.HandleFunc(fmt.Sprintf("/username/groups/%s/action", groupId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(bytes))
wg.Done()
})
}
ctx := context.Background()
client.Groups.TurnOffAll(ctx, groupIds...)
done := make(chan bool)
go func() {
wg.Wait()
done <- true
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Errorf("Failed to turn off all groups on time")
}
}