@@ -11,8 +11,12 @@ package topology
11
11
12
12
import (
13
13
"context"
14
+ "crypto/tls"
15
+ "crypto/x509"
14
16
"errors"
17
+ "io/ioutil"
15
18
"net"
19
+ "os"
16
20
"runtime"
17
21
"sync"
18
22
"sync/atomic"
@@ -100,36 +104,47 @@ type timeoutDialer struct {
100
104
}
101
105
102
106
func (d * timeoutDialer ) DialContext (ctx context.Context , network , address string ) (net.Conn , error ) {
103
- var dialer net.Dialer
104
- c , e := dialer .DialContext (ctx , network , address )
105
- return & timeoutConn {c , d .errors }, e
106
- }
107
+ c , e := d .Dialer .DialContext (ctx , network , address )
107
108
108
- type timeoutErr struct {
109
- net.UnknownNetworkError
110
- }
109
+ if caFile := os .Getenv ("MONGO_GO_DRIVER_CA_FILE" ); len (caFile ) > 0 {
110
+ pem , err := ioutil .ReadFile (caFile )
111
+ if err != nil {
112
+ return nil , err
113
+ }
111
114
112
- func (e * timeoutErr ) Timeout () bool {
113
- return true
114
- }
115
+ ca := x509 .NewCertPool ()
116
+ if ! ca .AppendCertsFromPEM (pem ) {
117
+ return nil , errors .New ("unable to load CA file" )
118
+ }
115
119
116
- var timeout = & timeoutErr {"test timeout" }
120
+ config := & tls.Config {
121
+ InsecureSkipVerify : true ,
122
+ RootCAs : ca ,
123
+ }
124
+ c = tls .Client (c , config )
125
+ }
126
+ return & timeoutConn {c , d .errors }, e
127
+ }
117
128
118
129
// TestServerHeartbeatTimeout tests timeout retry for GODRIVER-2577.
119
130
func TestServerHeartbeatTimeout (t * testing.T ) {
131
+ networkTimeoutError := & net.DNSError {
132
+ IsTimeout : true ,
133
+ }
134
+
120
135
testCases := []struct {
121
136
desc string
122
137
ioErrors []error
123
138
expectPoolCleared bool
124
139
}{
125
140
{
126
141
desc : "one single timeout should not clear the pool" ,
127
- ioErrors : []error {nil , timeout , nil , timeout , nil },
142
+ ioErrors : []error {nil , networkTimeoutError , nil , networkTimeoutError , nil },
128
143
expectPoolCleared : false ,
129
144
},
130
145
{
131
146
desc : "continuous timeouts should clear the pool" ,
132
- ioErrors : []error {nil , timeout , timeout , nil },
147
+ ioErrors : []error {nil , networkTimeoutError , networkTimeoutError , nil },
133
148
expectPoolCleared : true ,
134
149
},
135
150
}
@@ -165,6 +180,9 @@ func TestServerHeartbeatTimeout(t *testing.T) {
165
180
},
166
181
}
167
182
}),
183
+ WithHeartbeatInterval (func (time.Duration ) time.Duration {
184
+ return 2 * time .Second
185
+ }),
168
186
)
169
187
require .NoError (t , server .Connect (nil ))
170
188
wg .Wait ()
0 commit comments