...
1
2
3
4
5
6
7 package integration
8
9 import (
10 "context"
11 "net"
12 "sync/atomic"
13 "testing"
14
15 "go.mongodb.org/mongo-driver/bson"
16 "go.mongodb.org/mongo-driver/internal/integtest"
17 "go.mongodb.org/mongo-driver/internal/require"
18 "go.mongodb.org/mongo-driver/mongo"
19 "go.mongodb.org/mongo-driver/mongo/options"
20 )
21
22 func TestClientOptions_CustomDialer(t *testing.T) {
23 td := &testDialer{d: &net.Dialer{}}
24 cs := integtest.ConnString(t)
25 opts := options.Client().ApplyURI(cs.String()).SetDialer(td)
26 integtest.AddTestServerAPIVersion(opts)
27 client, err := mongo.NewClient(opts)
28 require.NoError(t, err)
29 err = client.Connect(context.Background())
30 require.NoError(t, err)
31 _, err = client.ListDatabases(context.Background(), bson.D{})
32 require.NoError(t, err)
33 got := atomic.LoadInt32(&td.called)
34 if got < 1 {
35 t.Errorf("Custom dialer was not used when dialing new connections")
36 }
37 }
38
39 type testDialer struct {
40 called int32
41 d mongo.Dialer
42 }
43
44 func (td *testDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
45 atomic.AddInt32(&td.called, 1)
46 return td.d.DialContext(ctx, network, address)
47 }
48
View as plain text