1
+
2
+ import com.mongodb.ClientSessionOptions
3
+ import com.mongodb.ConnectionString
4
+ import com.mongodb.MongoClientSettings
5
+ import com.mongodb.TransactionOptions
6
+ import com.mongodb.client.cursor.TimeoutMode
7
+ import com.mongodb.client.model.Filters
8
+ import com.mongodb.kotlin.client.coroutine.MongoClient
9
+ import config.getConfig
10
+ import kotlinx.coroutines.runBlocking
11
+ import org.bson.Document
12
+ import org.junit.jupiter.api.AfterAll
13
+ import org.junit.jupiter.api.BeforeAll
14
+ import org.junit.jupiter.api.Test
15
+ import java.util.concurrent.TimeUnit
16
+ import kotlin.test.Ignore
17
+
18
+ class CsotTest {
19
+
20
+ companion object {
21
+ val config = getConfig()
22
+ val CONNECTION_URI_PLACEHOLDER = config.connectionUri
23
+ val client = MongoClient .create(CONNECTION_URI_PLACEHOLDER )
24
+ val database = client.getDatabase(" db" )
25
+ val collection = database.getCollection<Document >(" people" )
26
+
27
+ @BeforeAll
28
+ @JvmStatic
29
+ fun beforeAll () {
30
+ runBlocking {
31
+ val people = listOf (
32
+ Document (" name" , " Shelley Price" ).append(" age" , 56 ),
33
+ Document (" name" , " Garrett John" ).append(" age" , 39 ),
34
+ Document (" name" , " Kalima Sheik" ).append(" age" , 26 )
35
+ )
36
+ collection.insertMany(people)
37
+ }
38
+ }
39
+
40
+ @AfterAll
41
+ @JvmStatic
42
+ fun afterAll () {
43
+ runBlocking {
44
+ database.drop()
45
+ client.close()
46
+ }
47
+ }
48
+ }
49
+
50
+ @Ignore
51
+ fun connectionStringTest () = runBlocking {
52
+ // :snippet-start: connection-string-timeout
53
+ val uri = " <connection string>/?timeoutMS=200"
54
+ val client = MongoClient .create(uri)
55
+ // :snippet-end:
56
+ }
57
+
58
+ // :replace-start: {
59
+ // "terms": {
60
+ // "uri": "\"<connection string>\""
61
+ // }
62
+ // }
63
+
64
+ @Test
65
+ fun mongoClientSettingsTest () = runBlocking {
66
+ val uri = CONNECTION_URI_PLACEHOLDER
67
+ // :snippet-start: mongoclientsettings-timeout
68
+ val settings = MongoClientSettings .builder()
69
+ .applyConnectionString(ConnectionString (uri))
70
+ .timeout(200L , TimeUnit .MILLISECONDS )
71
+ .build()
72
+
73
+ val client = MongoClient .create(settings)
74
+ // :snippet-end:
75
+ }
76
+
77
+ @Test
78
+ fun operationTimeoutTest () = runBlocking {
79
+ val uri = CONNECTION_URI_PLACEHOLDER
80
+ // :snippet-start: operation
81
+ val settings = MongoClientSettings .builder()
82
+ .applyConnectionString(ConnectionString (uri))
83
+ .timeout(200L , TimeUnit .MILLISECONDS )
84
+ .build()
85
+
86
+ val client = MongoClient .create(settings)
87
+ val collection = client
88
+ .getDatabase(" db" )
89
+ .getCollection<Document >(" people" )
90
+
91
+ collection.insertOne(Document (" name" , " Francine Loews" ))
92
+ // :snippet-end:
93
+ }
94
+
95
+ @Test
96
+ fun overrideTimeoutTest () = runBlocking {
97
+ val uri = CONNECTION_URI_PLACEHOLDER
98
+ // :snippet-start: override
99
+ val settings = MongoClientSettings .builder()
100
+ .applyConnectionString(ConnectionString (uri))
101
+ .timeout(200L , TimeUnit .MILLISECONDS )
102
+ .build()
103
+
104
+ val client = MongoClient .create(settings)
105
+ val database = client.getDatabase(" db" )
106
+ val collection = database
107
+ .getCollection<Document >(" people" )
108
+ .withTimeout(300L , TimeUnit .MILLISECONDS )
109
+ // :snippet-end:
110
+ }
111
+
112
+ @Test
113
+ fun transactionTimeoutTest () = runBlocking {
114
+ // :snippet-start: session
115
+ val opts = ClientSessionOptions .builder()
116
+ .defaultTimeout(200L , TimeUnit .MILLISECONDS )
117
+ .build()
118
+
119
+ val session = client.startSession(opts)
120
+ // ... perform operations on ClientSession
121
+ // :snippet-end:
122
+
123
+ // :snippet-start: transaction
124
+ val transactionOptions = TransactionOptions .builder()
125
+ .timeout(200L , TimeUnit .MILLISECONDS )
126
+ .build()
127
+ // :snippet-end:
128
+ }
129
+
130
+ @Test
131
+ fun cursorTimeoutTest () = runBlocking {
132
+ val collection = database
133
+ .getCollection<Document >(" people" )
134
+ .withTimeout(200L , TimeUnit .MILLISECONDS )
135
+
136
+ // :snippet-start: cursor-lifetime
137
+ val flowWithLifetimeTimeout = collection
138
+ .find(Filters .gte(" age" , 40 ))
139
+ .timeoutMode(TimeoutMode .CURSOR_LIFETIME )
140
+ // :snippet-end:
141
+ }
142
+ }
143
+
144
+ // :replace-end:
0 commit comments