-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathinbound_test.go
More file actions
102 lines (87 loc) · 2.2 KB
/
Copy pathinbound_test.go
File metadata and controls
102 lines (87 loc) · 2.2 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
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
/*
* Copyright (c) 2020 Percipia
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Contributor(s):
* Glenn O. Larsen <glenn.larsen@gmail.com>
*/
package eslgo
import (
"context"
"errors"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInboundDial_ReturnsErrorWhenConnectionClosesBeforeAuthRequest(t *testing.T) {
listener := listenTCP(t)
defer listener.Close()
go func() {
conn, err := listener.Accept()
if err != nil {
return
}
conn.Close()
}()
opts := DefaultInboundOptions
opts.AuthTimeout = 100 * time.Millisecond
opts.Logger = NilLogger{}
disconnected := make(chan struct{})
opts.OnDisconnect = func() {
close(disconnected)
}
started := time.Now()
conn, err := opts.Dial(listener.Addr().String())
assert.Nil(t, conn)
require.Error(t, err)
assert.Contains(t, err.Error(), "before auth request")
assert.Less(t, time.Since(started), time.Second)
assertOnDisconnect(t, disconnected)
}
func TestInboundDial_ReturnsErrorWhenAuthRequestTimesOut(t *testing.T) {
listener := listenTCP(t)
defer listener.Close()
done := make(chan struct{})
defer close(done)
go func() {
conn, err := listener.Accept()
if err != nil {
return
}
defer conn.Close()
<-done
}()
opts := DefaultInboundOptions
opts.AuthTimeout = 100 * time.Millisecond
opts.Logger = NilLogger{}
disconnected := make(chan struct{})
opts.OnDisconnect = func() {
close(disconnected)
}
started := time.Now()
conn, err := opts.Dial(listener.Addr().String())
assert.Nil(t, conn)
require.Error(t, err)
assert.True(t, errors.Is(err, context.DeadlineExceeded))
assert.Less(t, time.Since(started), time.Second)
assertOnDisconnect(t, disconnected)
}
func assertOnDisconnect(t *testing.T, disconnected <-chan struct{}) {
t.Helper()
select {
case <-disconnected:
case <-time.After(time.Second):
t.Fatal("expected OnDisconnect to be called")
}
}
func listenTCP(t *testing.T) net.Listener {
t.Helper()
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
return listener
}