File: listenertest.go

package info (click to toggle)
golang-github-mdlayher-netx 0.0~git20220422.c711c2f-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 156 kB
  • sloc: makefile: 3
file content (255 lines) | stat: -rw-r--r-- 7,135 bytes parent folder | download | duplicates (2)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2019 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.

// This code is copied and slightly modified from an open CL:
// https://go-review.googlesource.com/c/net/+/123056.

package nettestx

import (
	"net"
	"sync"
	"testing"
	"time"
)

var (
	aLongTimeAgo = time.Unix(233431200, 0)
	neverTimeout = time.Time{}
)

// MakeOpenerSet creates and returns a set of connection openers for
// both passive- and active-open sides.
// The stop function closes all resources including ln, cancels the
// dial operation, and should not be nil.
type MakeOpenerSet func() (ln net.Listener, dial func(addr net.Addr) (net.Conn, error), stop func(), err error)

// TestListener tests that a net.Listener implementation properly
// satisfies the interface.
// The tests should not produce any false positives, but may
// experience false negatives.
// Thus, some issues may only be detected when the test is run
// multiple times.
// For maximal effectiveness, run the tests under the race detector.
func TestListener(t *testing.T, mos MakeOpenerSet) {
	t.Run("Accept", func(t *testing.T) { openerSetTimeoutWrapper(t, mos, testOpenerSetAccept) })
	t.Run("RacyAccept", func(t *testing.T) { openerSetTimeoutWrapper(t, mos, testOpenerSetRacyAccept) })
	t.Run("PastTimeout", func(t *testing.T) { openerSetTimeoutWrapper(t, mos, testOpenerSetPastTimeout) })
	t.Run("PresentTimeout", func(t *testing.T) { openerSetTimeoutWrapper(t, mos, testOpenerSetPresentTimeout) })
	t.Run("FutureTimeout", func(t *testing.T) { openerSetTimeoutWrapper(t, mos, testOpenerSetFutureTimeout) })
	t.Run("CloseTimeout", func(t *testing.T) { openerSetTimeoutWrapper(t, mos, testOpenerSetCloseTimeout) })
	t.Run("ConcurrentMethods", func(t *testing.T) { openerSetTimeoutWrapper(t, mos, testOpenerSetConcurrentMethods) })
}

type listenerTester func(t *testing.T, ln net.Listener, dial func(addr net.Addr) (net.Conn, error))

func openerSetTimeoutWrapper(t *testing.T, mos MakeOpenerSet, f listenerTester) {
	t.Parallel()
	ln, dial, stop, err := mos()
	if err != nil {
		t.Fatalf("unable to make opener set: %v", err)
	}
	var once sync.Once
	defer once.Do(func() { stop() })
	timer := time.AfterFunc(time.Minute, func() {
		once.Do(func() {
			t.Error("test timed out; terminating opener set")
			stop()
		})
	})
	defer timer.Stop()
	f(t, ln, dial)
}

type deadlineListener interface {
	net.Listener

	SetDeadline(time.Time) error
}

// testOpenerSetAccept tests that the connection setup request invoked
// by dial is properly accepted on ln.
func testOpenerSetAccept(t *testing.T, ln net.Listener, dial func(net.Addr) (net.Conn, error)) {
	var wg sync.WaitGroup
	defer wg.Wait()
	wg.Add(1)
	go func() {
		defer wg.Done()
		c, err := dial(ln.Addr())
		if err != nil {
			t.Errorf("unexpected Dial error: %v", err)
			return
		}
		if err := c.Close(); err != nil {
			t.Errorf("unexpected Close error: %v", err)
			return
		}
	}()
	c, err := ln.Accept()
	if err != nil {
		t.Errorf("unexpected Accept error: %v", err)
		return
	}
	c.Close()
}

// testOpenerSetRacyAccept tests that it is safe to call Accept
// concurrently.
func testOpenerSetRacyAccept(t *testing.T, ln net.Listener, dial func(net.Addr) (net.Conn, error)) {
	dl, ok := ln.(deadlineListener)
	if !ok {
		t.Skip("deadline not implemented")
	}
	var wg sync.WaitGroup
	dl.SetDeadline(time.Now().Add(time.Millisecond))
	for i := 0; i < 5; i++ {
		wg.Add(2)
		go func() {
			defer wg.Done()
			for j := 0; j < 10; j++ {
				c, err := dl.Accept()
				if err != nil {
					checkForTimeoutError(t, err)
					dl.SetDeadline(time.Now().Add(time.Millisecond))
					continue
				}
				c.Close()
			}
		}()
		go func() {
			defer wg.Done()
			c, err := dial(dl.Addr())
			if err != nil {
				return
			}
			c.Close()
		}()
	}
	wg.Wait()
}

// testOpenerSetPastTimeout tests that a deadline set in the past
// immediately times out Accept operations.
func testOpenerSetPastTimeout(t *testing.T, ln net.Listener, dial func(net.Addr) (net.Conn, error)) {
	dl, ok := ln.(deadlineListener)
	if !ok {
		t.Skip("deadline not implemented")
	}
	dl.SetDeadline(aLongTimeAgo)
	_, err := dl.Accept()
	checkForTimeoutError(t, err)
}

// testOpenerSetPresentTimeout tests that a deadline set while there
// are pending Accept operations immediately times out those
// operations.
func testOpenerSetPresentTimeout(t *testing.T, ln net.Listener, dial func(net.Addr) (net.Conn, error)) {
	dl, ok := ln.(deadlineListener)
	if !ok {
		t.Skip("deadline not implemented")
	}
	var wg sync.WaitGroup
	wg.Add(2)
	deadlineSet := make(chan bool, 1)
	go func() {
		defer wg.Done()
		time.Sleep(10 * time.Millisecond)
		deadlineSet <- true
		dl.SetDeadline(aLongTimeAgo)
	}()
	go func() {
		defer wg.Done()
		_, err := dl.Accept()
		checkForTimeoutError(t, err)
		if len(deadlineSet) == 0 {
			t.Error("Accept timed out before deadline is set")
		}
	}()
	wg.Wait()
}

// testOpenerSetFutureTimeout tests that a future deadline will
// eventually time out Accept operations.
func testOpenerSetFutureTimeout(t *testing.T, ln net.Listener, dial func(net.Addr) (net.Conn, error)) {
	dl, ok := ln.(deadlineListener)
	if !ok {
		t.Skip("deadline not implemented")
	}
	var wg sync.WaitGroup
	wg.Add(1)
	dl.SetDeadline(time.Now().Add(100 * time.Millisecond))
	go func() {
		defer wg.Done()
		_, err := dl.Accept()
		checkForTimeoutError(t, err)
	}()
	wg.Wait()
}

// testOpenerSetCloseTimeout tests that calling Close immediately
// times out pending Accept operations.
func testOpenerSetCloseTimeout(t *testing.T, ln net.Listener, dial func(net.Addr) (net.Conn, error)) {
	dl, ok := ln.(deadlineListener)
	if !ok {
		t.Skip("deadline not implemented")
	}
	var wg sync.WaitGroup
	wg.Add(2)
	// Test for cancelation upon connection closure.
	dl.SetDeadline(neverTimeout)
	go func() {
		defer wg.Done()
		time.Sleep(100 * time.Millisecond)
		dl.Close()
	}()
	go func() {
		defer wg.Done()
		var err error
		for err == nil {
			_, err = dl.Accept()
		}
	}()
	wg.Wait()
}

// testOpennerSetConcurrentMethods tests that the methods of
// net.Listener can safely be called concurrently.
func testOpenerSetConcurrentMethods(t *testing.T, ln net.Listener, dial func(net.Addr) (net.Conn, error)) {
	dl, ok := ln.(deadlineListener)
	if !ok {
		t.Skip("deadline not implemented")
	}
	// The results of the calls may be nonsensical, but this
	// should not trigger a race detector warning.
	var wg sync.WaitGroup
	for i := 0; i < 100; i++ {
		wg.Add(3)
		go func() {
			defer wg.Done()
			dl.Accept()
		}()
		go func() {
			defer wg.Done()
			dl.SetDeadline(time.Now().Add(10 * time.Millisecond))
		}()
		go func() {
			defer wg.Done()
			dl.Addr()
		}()
	}
	wg.Wait() // At worst, the deadline is set 10ms into the future
}

// checkForTimeoutError checks that the error satisfies the Error interface
// and that Timeout returns true.
func checkForTimeoutError(t *testing.T, err error) {
	t.Helper()
	if nerr, ok := err.(net.Error); ok {
		if !nerr.Timeout() {
			t.Errorf("err.Timeout() = false, want true")
		}
	} else {
		t.Errorf("got %T, want net.Error", err)
	}
}