-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathnettest_test.go
More file actions
55 lines (43 loc) · 1.06 KB
/
nettest_test.go
File metadata and controls
55 lines (43 loc) · 1.06 KB
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
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
package dtls
import (
"net"
"sync"
"testing"
"time"
"github.com/pion/transport/v4/test"
"golang.org/x/net/nettest"
)
// closeOnceConn wraps a net.Conn to make Close() idempotent,
// returning nil on subsequent calls instead of ErrConnClosed.
type closeOnceConn struct {
net.Conn
closeOnce sync.Once
closeErr error
}
func (c *closeOnceConn) Close() error {
c.closeOnce.Do(func() {
c.closeErr = c.Conn.Close()
})
return c.closeErr
}
func TestNetTest(t *testing.T) {
lim := test.TimeOut(time.Minute*1 + time.Second*10)
defer lim.Stop()
nettest.TestConn(t, func() (c1, c2 net.Conn, stop func(), err error) {
c1, c2, err = pipeMemory()
if err != nil {
return nil, nil, nil, err
}
// Wrap connections to handle ErrConnClosed gracefully
c1Wrapper := &closeOnceConn{Conn: c1}
c2Wrapper := &closeOnceConn{Conn: c2}
stop = func() {
_ = c1Wrapper.Close()
_ = c2Wrapper.Close()
}
return c1Wrapper, c2Wrapper, stop, nil
})
}