-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsubscribe_test.go
186 lines (169 loc) · 4 KB
/
subscribe_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package i3
import (
"context"
"fmt"
"os"
"os/exec"
"sync"
"testing"
"time"
"golang.org/x/sync/errgroup"
)
// TestSubscribeSubprocess runs in a process which has been started with
// DISPLAY= pointing to an Xvfb instance with i3 -c testdata/i3.config running.
func TestSubscribeSubprocess(t *testing.T) {
if os.Getenv("GO_WANT_XVFB") != "1" {
t.Skip("parent process")
}
// TODO(https://github.com/i3/i3/issues/2988): as soon as we are targeting
// i3 4.15, use SendTick to eliminate race conditions in this test.
t.Run("subscribe", func(t *testing.T) {
var eg errgroup.Group
ws := Subscribe(WorkspaceEventType)
received := make(chan Event)
eg.Go(func() error {
defer close(received)
if ws.Next() {
received <- ws.Event()
}
return ws.Close()
})
// As we can’t know when EventReceiver.Next() actually subscribes, we
// just continuously switch workspaces.
ctx, canc := context.WithCancel(context.Background())
defer canc()
go func() {
cnt := 2
for ctx.Err() == nil {
RunCommand(fmt.Sprintf("workspace %d", cnt))
cnt++
time.Sleep(10 * time.Millisecond)
}
}()
select {
case <-received:
case <-time.After(5 * time.Second):
t.Fatalf("timeout waiting for an event from i3")
}
if err := eg.Wait(); err != nil {
t.Fatal(err)
}
})
t.Run("subscribeParallel", func(t *testing.T) {
var mu sync.Mutex
received := make(map[string]int)
recv1 := Subscribe(WorkspaceEventType)
go func() {
for recv1.Next() {
ev := recv1.Event().(*WorkspaceEvent)
if ev.Change == "init" {
mu.Lock()
received[ev.Current.Name]++
mu.Unlock()
}
}
}()
recv2 := Subscribe(WorkspaceEventType)
go func() {
for recv2.Next() {
ev := recv2.Event().(*WorkspaceEvent)
if ev.Change == "init" {
mu.Lock()
received[ev.Current.Name]++
mu.Unlock()
}
}
}()
cnt := 2
start := time.Now()
for time.Since(start) < 5*time.Second {
mu.Lock()
done := received[fmt.Sprintf("%d", cnt-1)] == 2
mu.Unlock()
if done {
return // success
}
if _, err := RunCommand(fmt.Sprintf("workspace %d", cnt)); err != nil {
t.Fatal(err)
}
cnt++
time.Sleep(10 * time.Millisecond)
}
})
t.Run("subscribeMultiple", func(t *testing.T) {
var eg errgroup.Group
ws := Subscribe(WorkspaceEventType, ModeEventType)
received := make(chan struct{})
eg.Go(func() error {
defer close(received)
defer ws.Close()
seen := map[EventType]bool{
WorkspaceEventType: false,
ModeEventType: false,
}
Outer:
for ws.Next() {
switch ws.Event().(type) {
case *WorkspaceEvent:
seen[WorkspaceEventType] = true
case *ModeEvent:
seen[ModeEventType] = true
}
for _, seen := range seen {
if !seen {
continue Outer
}
}
return nil
}
return ws.Close()
})
// As we can’t know when EventReceiver.Next() actually subscribes, we
// just continuously switch workspaces and modes.
ctx, canc := context.WithCancel(context.Background())
defer canc()
go func() {
modes := []string{"default", "conf"}
cnt := 2
for ctx.Err() == nil {
RunCommand(fmt.Sprintf("workspace %d", cnt))
cnt++
RunCommand(fmt.Sprintf("mode %s", modes[cnt%len(modes)]))
time.Sleep(10 * time.Millisecond)
}
}()
select {
case <-received:
case <-time.After(5 * time.Second):
t.Fatalf("timeout waiting for an event from i3")
}
if err := eg.Wait(); err != nil {
t.Fatal(err)
}
})
}
func TestSubscribe(t *testing.T) {
t.Parallel()
ctx, canc := context.WithCancel(context.Background())
defer canc()
_, DISPLAY, err := launchXvfb(ctx)
if err != nil {
t.Fatal(err)
}
cleanup, err := launchI3(ctx, DISPLAY, "")
if err != nil {
t.Fatal(err)
}
defer cleanup()
cmd := exec.Command(os.Args[0], "-test.run=TestSubscribeSubprocess", "-test.v")
cmd.Env = []string{
"GO_WANT_XVFB=1",
"DISPLAY=" + DISPLAY,
"PATH=" + os.Getenv("PATH"),
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatal(err.Error())
}
}