@@ -6,11 +6,64 @@ import {
6
6
type Collection ,
7
7
type CommandStartedEvent ,
8
8
MongoClient ,
9
- MongoDriverError
9
+ MongoDriverError ,
10
+ MongoInvalidArgumentError
10
11
} from '../../mongodb' ;
11
12
import { sleep } from '../../tools/utils' ;
12
13
13
14
describe ( 'Sessions Prose Tests' , ( ) => {
15
+ describe ( '5. Session argument is for the right client' , ( ) => {
16
+ let client1 : MongoClient ;
17
+ let client2 : MongoClient ;
18
+ beforeEach ( async function ( ) {
19
+ client1 = this . configuration . newClient ( ) ;
20
+ client2 = this . configuration . newClient ( ) ;
21
+ } ) ;
22
+
23
+ afterEach ( async function ( ) {
24
+ await client1 ?. close ( ) ;
25
+ await client2 ?. close ( ) ;
26
+ } ) ;
27
+
28
+ /**
29
+ * Steps:
30
+ * - Create client1 and client2
31
+ * - Get database from client1
32
+ * - Get collection from database
33
+ * - Start session from client2
34
+ * - Call collection.insertOne(session,...)
35
+ * - Assert that an error was reported because session was not started from client1
36
+ *
37
+ * This validation lives in our executeOperation layer so it applies universally.
38
+ * A find and an insert provide enough coverage, we determined we do not need to enumerate every possible operation.
39
+ */
40
+ context (
41
+ 'when session is started from a different client than operation is being run on' ,
42
+ ( ) => {
43
+ it ( 'insertOne operation throws a MongoInvalidArgumentError' , async ( ) => {
44
+ const db = client1 . db ( ) ;
45
+ const collection = db . collection ( 'test' ) ;
46
+ const session = client2 . startSession ( ) ;
47
+ const error = await collection . insertOne ( { } , { session } ) . catch ( error => error ) ;
48
+ expect ( error ) . to . be . instanceOf ( MongoInvalidArgumentError ) ;
49
+ expect ( error ) . to . match ( / C l i e n t S e s s i o n m u s t b e f r o m t h e s a m e M o n g o C l i e n t / i) ;
50
+ } ) ;
51
+
52
+ it ( 'find operation throws a MongoInvalidArgumentError' , async ( ) => {
53
+ const db = client1 . db ( ) ;
54
+ const collection = db . collection ( 'test' ) ;
55
+ const session = client2 . startSession ( ) ;
56
+ const error = await collection
57
+ . find ( { } , { session } )
58
+ . toArray ( )
59
+ . catch ( error => error ) ;
60
+ expect ( error ) . to . be . instanceOf ( MongoInvalidArgumentError ) ;
61
+ expect ( error ) . to . match ( / C l i e n t S e s s i o n m u s t b e f r o m t h e s a m e M o n g o C l i e n t / i) ;
62
+ } ) ;
63
+ }
64
+ ) ;
65
+ } ) ;
66
+
14
67
describe ( '14. Implicit sessions only allocate their server session after a successful connection checkout' , ( ) => {
15
68
let client : MongoClient ;
16
69
let testCollection : Collection < { _id : number ; a ?: number } > ;
0 commit comments