@@ -6,11 +6,46 @@ import {
6
6
type Collection ,
7
7
type CommandStartedEvent ,
8
8
MongoClient ,
9
- MongoDriverError
9
+ MongoDriverError ,
10
+ MongoRuntimeError
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
+ context ( 'when session is started from a different client than operation is being run on' , ( ) =>
38
+ it ( 'the operation throws a MongoRuntimeError' , async ( ) => {
39
+ const db = client1 . db ( ) ;
40
+ const collection = db . collection ( 'test' ) ;
41
+ const session = client2 . startSession ( ) ;
42
+ const error = await collection . insertOne ( { } , { session } ) . catch ( error => error ) ;
43
+ expect ( error ) . to . be . instanceOf ( MongoRuntimeError ) ;
44
+ 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) ;
45
+ } )
46
+ ) ;
47
+ } ) ;
48
+
14
49
describe ( '14. Implicit sessions only allocate their server session after a successful connection checkout' , ( ) => {
15
50
let client : MongoClient ;
16
51
let testCollection : Collection < { _id : number ; a ?: number } > ;
0 commit comments