forked from mdlayher/wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
107 lines (86 loc) · 1.96 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package wifi
import (
"fmt"
"os"
"sync"
"testing"
)
func TestClientIntegrationConcurrent(t *testing.T) {
const (
workers = 16
iterations = 10000
)
c := mustNew(t)
ifis, err := c.Interfaces()
if err != nil {
t.Fatalf("failed to retrieve interfaces: %v", err)
}
if err := c.Close(); err != nil {
t.Fatalf("failed to close client: %v", err)
}
if len(ifis) == 0 {
t.Skip("found no wifi interfaces, skipping")
}
names := make([]string, 0)
for _, ifi := range ifis {
if ifi.Name == "" || ifi.Type != InterfaceTypeStation {
continue
}
names = append(names, ifi.Name)
}
t.Logf("workers: %d, iterations: %d, interfaces: %v",
workers, iterations, names)
clients := make([]*Client, 0, workers)
for i := 0; i < workers; i++ {
clients = append(clients, mustNew(t))
}
var wg sync.WaitGroup
wg.Add(workers)
for _, c := range clients {
go execN(&wg, c, iterations, names)
}
wg.Wait()
}
func mustNew(t *testing.T) *Client {
c, err := New()
if err != nil {
if os.IsNotExist(err) {
t.Skip("wifi data not available, skipping")
}
if err == errUnimplemented {
t.Skip(err)
}
t.Fatalf("failed to access wifi data: %v", err)
}
return c
}
func execN(wg *sync.WaitGroup, c *Client, n int, expect []string) {
names := make(map[string]int, 0)
for i := 0; i < n; i++ {
ifis, err := c.Interfaces()
if err != nil {
panic(fmt.Sprintf("failed to retrieve interfaces: %v", err))
}
for _, ifi := range ifis {
if ifi.Name == "" || ifi.Type != InterfaceTypeStation {
continue
}
if _, err := c.StationInfo(ifi); err != nil {
if !os.IsNotExist(err) {
panic(fmt.Sprintf("failed to retrieve station info for device %s: %v", ifi.Name, err))
}
}
names[ifi.Name]++
}
}
for _, e := range expect {
nn, ok := names[e]
if !ok {
panic(fmt.Sprintf("did not find interface %q during test", e))
}
if nn != n {
panic(fmt.Sprintf("wanted to find %q %d times, found %d", e, n, nn))
}
}
wg.Done()
}