summaryrefslogtreecommitdiff
path: root/vendor/gopkg.in/fsnotify.v1/inotify_poller_test.go
blob: 26623efeffa3d382b05edb9c25747921e584fcc6 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux

package fsnotify

import (
	"testing"
	"time"

	"golang.org/x/sys/unix"
)

type testFd [2]int

func makeTestFd(t *testing.T) testFd {
	var tfd testFd
	errno := unix.Pipe(tfd[:])
	if errno != nil {
		t.Fatalf("Failed to create pipe: %v", errno)
	}
	return tfd
}

func (tfd testFd) fd() int {
	return tfd[0]
}

func (tfd testFd) closeWrite(t *testing.T) {
	errno := unix.Close(tfd[1])
	if errno != nil {
		t.Fatalf("Failed to close write end of pipe: %v", errno)
	}
}

func (tfd testFd) put(t *testing.T) {
	buf := make([]byte, 10)
	_, errno := unix.Write(tfd[1], buf)
	if errno != nil {
		t.Fatalf("Failed to write to pipe: %v", errno)
	}
}

func (tfd testFd) get(t *testing.T) {
	buf := make([]byte, 10)
	_, errno := unix.Read(tfd[0], buf)
	if errno != nil {
		t.Fatalf("Failed to read from pipe: %v", errno)
	}
}

func (tfd testFd) close() {
	unix.Close(tfd[1])
	unix.Close(tfd[0])
}

func makePoller(t *testing.T) (testFd, *fdPoller) {
	tfd := makeTestFd(t)
	poller, err := newFdPoller(tfd.fd())
	if err != nil {
		t.Fatalf("Failed to create poller: %v", err)
	}
	return tfd, poller
}

func TestPollerWithBadFd(t *testing.T) {
	_, err := newFdPoller(-1)
	if err != unix.EBADF {
		t.Fatalf("Expected EBADF, got: %v", err)
	}
}

func TestPollerWithData(t *testing.T) {
	tfd, poller := makePoller(t)
	defer tfd.close()
	defer poller.close()

	tfd.put(t)
	ok, err := poller.wait()
	if err != nil {
		t.Fatalf("poller failed: %v", err)
	}
	if !ok {
		t.Fatalf("expected poller to return true")
	}
	tfd.get(t)
}

func TestPollerWithWakeup(t *testing.T) {
	tfd, poller := makePoller(t)
	defer tfd.close()
	defer poller.close()

	err := poller.wake()
	if err != nil {
		t.Fatalf("wake failed: %v", err)
	}
	ok, err := poller.wait()
	if err != nil {
		t.Fatalf("poller failed: %v", err)
	}
	if ok {
		t.Fatalf("expected poller to return false")
	}
}

func TestPollerWithClose(t *testing.T) {
	tfd, poller := makePoller(t)
	defer tfd.close()
	defer poller.close()

	tfd.closeWrite(t)
	ok, err := poller.wait()
	if err != nil {
		t.Fatalf("poller failed: %v", err)
	}
	if !ok {
		t.Fatalf("expected poller to return true")
	}
}

func TestPollerWithWakeupAndData(t *testing.T) {
	tfd, poller := makePoller(t)
	defer tfd.close()
	defer poller.close()

	tfd.put(t)
	err := poller.wake()
	if err != nil {
		t.Fatalf("wake failed: %v", err)
	}

	// both data and wakeup
	ok, err := poller.wait()
	if err != nil {
		t.Fatalf("poller failed: %v", err)
	}
	if !ok {
		t.Fatalf("expected poller to return true")
	}

	// data is still in the buffer, wakeup is cleared
	ok, err = poller.wait()
	if err != nil {
		t.Fatalf("poller failed: %v", err)
	}
	if !ok {
		t.Fatalf("expected poller to return true")
	}

	tfd.get(t)
	// data is gone, only wakeup now
	err = poller.wake()
	if err != nil {
		t.Fatalf("wake failed: %v", err)
	}
	ok, err = poller.wait()
	if err != nil {
		t.Fatalf("poller failed: %v", err)
	}
	if ok {
		t.Fatalf("expected poller to return false")
	}
}

func TestPollerConcurrent(t *testing.T) {
	tfd, poller := makePoller(t)
	defer tfd.close()
	defer poller.close()

	oks := make(chan bool)
	live := make(chan bool)
	defer close(live)
	go func() {
		defer close(oks)
		for {
			ok, err := poller.wait()
			if err != nil {
				t.Fatalf("poller failed: %v", err)
			}
			oks <- ok
			if !<-live {
				return
			}
		}
	}()

	// Try a write
	select {
	case <-time.After(50 * time.Millisecond):
	case <-oks:
		t.Fatalf("poller did not wait")
	}
	tfd.put(t)
	if !<-oks {
		t.Fatalf("expected true")
	}
	tfd.get(t)
	live <- true

	// Try a wakeup
	select {
	case <-time.After(50 * time.Millisecond):
	case <-oks:
		t.Fatalf("poller did not wait")
	}
	err := poller.wake()
	if err != nil {
		t.Fatalf("wake failed: %v", err)
	}
	if <-oks {
		t.Fatalf("expected false")
	}
	live <- true

	// Try a close
	select {
	case <-time.After(50 * time.Millisecond):
	case <-oks:
		t.Fatalf("poller did not wait")
	}
	tfd.closeWrite(t)
	if !<-oks {
		t.Fatalf("expected true")
	}
	tfd.get(t)
}