| 1 |
// Copyright (C) MongoDB, Inc. 2024-present. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
// not use this file except in compliance with the License. You may obtain |
| 5 |
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"fmt" |
| 12 |
"os" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
"go.mongodb.org/mongo-driver/bson" |
| 18 |
"go.mongodb.org/mongo-driver/mongo" |
| 19 |
"go.mongodb.org/mongo-driver/mongo/options" |
| 20 |
"go.uber.org/goleak" |
| 21 |
) |
| 22 |
|
| 23 |
var dbName = fmt.Sprintf("goleak-%d", time.Now().Unix()) |
| 24 |
|
| 25 |
// TestGoroutineLeak creates clients with various client configurations, runs |
| 26 |
// some operations with each one, then disconnects the client. It asserts that |
| 27 |
// no goroutines were leaked after the client is disconnected. |
| 28 |
func TestGoroutineLeak(t *testing.T) { |
| 29 |
testCases := []struct { |
| 30 |
desc string |
| 31 |
opts *options.ClientOptions |
| 32 |
}{ |
| 33 |
{ |
| 34 |
desc: "base", |
| 35 |
opts: options.Client(), |
| 36 |
}, |
| 37 |
{ |
| 38 |
desc: "compressors=snappy", |
| 39 |
opts: options.Client().SetCompressors([]string{"snappy"}), |
| 40 |
}, |
| 41 |
{ |
| 42 |
desc: "compressors=zlib", |
| 43 |
opts: options.Client().SetCompressors([]string{"zlib"}), |
| 44 |
}, |
| 45 |
{ |
| 46 |
desc: "compressors=zstd", |
| 47 |
opts: options.Client().SetCompressors([]string{"zstd"}), |
| 48 |
}, |
| 49 |
{ |
| 50 |
desc: "minPoolSize=10", |
| 51 |
opts: options.Client().SetMinPoolSize(10), |
| 52 |
}, |
| 53 |
{ |
| 54 |
desc: "serverMonitoringMode=poll", |
| 55 |
opts: options.Client().SetServerMonitoringMode(options.ServerMonitoringModePoll), |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, tc := range testCases { |
| 60 |
// These can't be run in parallel because goleak currently can't filter |
| 61 |
// out goroutines from other parallel subtests. |
| 62 |
t.Run(tc.desc, func(t *testing.T) { |
| 63 |
defer goleak.VerifyNone(t) |
| 64 |
|
| 65 |
base := options.Client() |
| 66 |
if u := os.Getenv("MONGODB_URI"); u != "" { |
| 67 |
base.ApplyURI(u) |
| 68 |
} |
| 69 |
client, err := mongo.Connect(context.Background(), base, tc.opts) |
| 70 |
require.NoError(t, err) |
| 71 |
|
| 72 |
defer func() { |
| 73 |
err = client.Disconnect(context.Background()) |
| 74 |
require.NoError(t, err) |
| 75 |
}() |
| 76 |
|
| 77 |
db := client.Database(dbName) |
| 78 |
defer func() { |
| 79 |
err := db.Drop(context.Background()) |
| 80 |
require.NoError(t, err) |
| 81 |
}() |
| 82 |
|
| 83 |
coll := db.Collection(collectionName(t)) |
| 84 |
|
| 85 |
// Start a change stream to simulate a change listener workload. |
| 86 |
cs, err := coll.Watch(context.Background(), mongo.Pipeline{}) |
| 87 |
require.NoError(t, err) |
| 88 |
defer cs.Close(context.Background()) |
| 89 |
|
| 90 |
// Run some Insert and FindOne operations to simulate a writing and |
| 91 |
// reading workload. Run 50 iterations to increase the probability |
| 92 |
// that a goroutine leak will happen if a problem exists. |
| 93 |
for i := 0; i < 50; i++ { |
| 94 |
_, err = coll.InsertOne(context.Background(), bson.M{"x": 123}) |
| 95 |
require.NoError(t, err) |
| 96 |
|
| 97 |
var res bson.D |
| 98 |
err = coll.FindOne(context.Background(), bson.D{}).Decode(&res) |
| 99 |
require.NoError(t, err) |
| 100 |
} |
| 101 |
|
| 102 |
// Intentionally cause some timeouts. Ignore any errors. |
| 103 |
for i := 0; i < 50; i++ { |
| 104 |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Microsecond) |
| 105 |
coll.FindOne(ctx, bson.D{}).Err() |
| 106 |
cancel() |
| 107 |
} |
| 108 |
|
| 109 |
// Finish simulating the change listener workload. Use "Next" to |
| 110 |
// fetch at least one change stream document batch and decode the |
| 111 |
// first document. |
| 112 |
cs.Next(context.Background()) |
| 113 |
var res bson.D |
| 114 |
err = cs.Decode(&res) |
| 115 |
require.NoError(t, err) |
| 116 |
}) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func collectionName(t *testing.T) string { |
| 121 |
return fmt.Sprintf("%s-%d", t.Name(), time.Now().Unix()) |
| 122 |
} |