-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathconnection_pool_test.go
49 lines (45 loc) · 1.05 KB
/
connection_pool_test.go
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
// Copyright 2019 Kuei-chun Chen. All rights reserved.
package examples
import (
"context"
"fmt"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func TestConnectionPool(t *testing.T) {
t.Log("TestConnectionPool")
var err error
var client *mongo.Client
var collection *mongo.Collection
var ctx = context.Background()
if client, err = getMongoClient(); err != nil {
t.Fatal(err)
}
defer client.Disconnect(ctx)
seedCarsData(client, dbName)
channel := make(chan string)
nThreads := 3
for i := 0; i < nThreads; i++ {
go func(client *mongo.Client, channel chan string, i int) {
var doc bson.M
collection = client.Database(dbName).Collection(collectionName)
filter := bson.D{{Key: "color", Value: "Red"}}
if err = collection.FindOne(ctx, filter).Decode(&doc); err != nil {
t.Fatal(err)
}
channel <- fmt.Sprintf("child %d completed", i)
}(client, channel, i)
}
cnt := 0
for {
msg := <-channel
t.Log(msg)
cnt++
if cnt == nThreads {
break
}
time.Sleep(time.Second * 1)
}
}