19
19
import { ConnectionProvider , Session , Connection , TransactionPromise , Transaction , BookmarkManager , bookmarkManager , NotificationFilter , int } from '../src'
20
20
import { bookmarks } from '../src/internal'
21
21
import { ACCESS_MODE_READ , FETCH_ALL } from '../src/internal/constants'
22
+ import { Logger } from '../src/internal/logger'
22
23
import ManagedTransaction from '../src/transaction-managed'
23
- import { AuthToken } from '../src/types'
24
+ import { AuthToken , LoggerFunction } from '../src/types'
24
25
import FakeConnection from './utils/connection.fake'
25
26
import { validNotificationFilters } from './utils/notification-filters.fixtures'
26
27
import fc from 'fast-check'
@@ -497,6 +498,56 @@ describe('session', () => {
497
498
)
498
499
)
499
500
} )
501
+
502
+ it ( 'should log a warning for timeout configurations with sub milliseconds' , async ( ) => {
503
+ return await fc . assert (
504
+ fc . asyncProperty (
505
+ fc
506
+ . float ( { min : 0 , noNaN : true } )
507
+ . filter ( ( timeout : number ) => ! Number . isInteger ( timeout ) ) ,
508
+ async ( timeout : number ) => {
509
+ const connection = mockBeginWithSuccess ( newFakeConnection ( ) )
510
+
511
+ const { session, loggerFunction } = setupSession ( {
512
+ connection,
513
+ beginTx : false ,
514
+ database : 'neo4j'
515
+ } )
516
+
517
+ await session . beginTransaction ( { timeout } )
518
+
519
+ expect ( loggerFunction ) . toBeCalledWith (
520
+ 'info' ,
521
+ `Transaction timeout expected to be an integer, got: ${ timeout } . The value will be round up.`
522
+ )
523
+ }
524
+ )
525
+ )
526
+ } )
527
+
528
+ it ( 'should not log a warning for timeout configurations without sub milliseconds' , async ( ) => {
529
+ return await fc . assert (
530
+ fc . asyncProperty (
531
+ fc . nat ( ) ,
532
+ async ( timeout : number ) => {
533
+ const connection = mockBeginWithSuccess ( newFakeConnection ( ) )
534
+
535
+ const { session, loggerFunction } = setupSession ( {
536
+ connection,
537
+ beginTx : false ,
538
+ database : 'neo4j'
539
+ } )
540
+
541
+ await session . beginTransaction ( { timeout } )
542
+
543
+ expect ( loggerFunction ) . not . toBeCalledWith (
544
+ 'info' ,
545
+ `Transaction timeout expected to be an integer, got: ${ timeout } . The value will be round up.`
546
+ )
547
+ }
548
+ )
549
+ )
550
+ } )
500
551
} )
501
552
502
553
describe ( '.commit()' , ( ) => {
@@ -634,6 +685,68 @@ describe('session', () => {
634
685
)
635
686
)
636
687
} )
688
+
689
+ it ( 'should log a warning for timeout configurations with sub milliseconds' , async ( ) => {
690
+ return await fc . assert (
691
+ fc . asyncProperty (
692
+ fc
693
+ . float ( { min : 0 , noNaN : true } )
694
+ . filter ( ( timeout : number ) => ! Number . isInteger ( timeout ) ) ,
695
+ async ( timeout : number ) => {
696
+ const connection = mockBeginWithSuccess ( newFakeConnection ( ) )
697
+ const { session, loggerFunction } = setupSession ( {
698
+ connection,
699
+ beginTx : false ,
700
+ fetchSize : FETCH_ALL
701
+ } )
702
+
703
+ // @ts -expect-error
704
+ jest . spyOn ( Transaction . prototype , 'run' ) . mockImplementation ( async ( ) => await Promise . resolve ( ) )
705
+ const query = 'RETURN $a'
706
+ const params = { a : 1 }
707
+
708
+ await execute ( session ) ( async ( tx : ManagedTransaction ) => {
709
+ await tx . run ( query , params )
710
+ } , { timeout } )
711
+
712
+ expect ( loggerFunction ) . toBeCalledWith (
713
+ 'info' ,
714
+ `Transaction timeout expected to be an integer, got: ${ timeout } . The value will be round up.`
715
+ )
716
+ }
717
+ )
718
+ )
719
+ } )
720
+
721
+ it ( 'should not log a warning for timeout configurations without sub milliseconds' , async ( ) => {
722
+ return await fc . assert (
723
+ fc . asyncProperty (
724
+ fc . nat ( ) ,
725
+ async ( timeout : number ) => {
726
+ const connection = mockBeginWithSuccess ( newFakeConnection ( ) )
727
+ const { session, loggerFunction } = setupSession ( {
728
+ connection,
729
+ beginTx : false ,
730
+ fetchSize : FETCH_ALL
731
+ } )
732
+
733
+ // @ts -expect-error
734
+ jest . spyOn ( Transaction . prototype , 'run' ) . mockImplementation ( async ( ) => await Promise . resolve ( ) )
735
+ const query = 'RETURN $a'
736
+ const params = { a : 1 }
737
+
738
+ await execute ( session ) ( async ( tx : ManagedTransaction ) => {
739
+ await tx . run ( query , params )
740
+ } , { timeout } )
741
+
742
+ expect ( loggerFunction ) . not . toBeCalledWith (
743
+ 'info' ,
744
+ `Transaction timeout expected to be an integer, got: ${ timeout } . The value will be round up.`
745
+ )
746
+ }
747
+ )
748
+ )
749
+ } )
637
750
} )
638
751
639
752
describe ( '.run()' , ( ) => {
@@ -986,6 +1099,56 @@ describe('session', () => {
986
1099
)
987
1100
)
988
1101
} )
1102
+
1103
+ it ( 'should log a warning for timeout configurations with sub milliseconds' , async ( ) => {
1104
+ return await fc . assert (
1105
+ fc . asyncProperty (
1106
+ fc
1107
+ . float ( { min : 0 , noNaN : true } )
1108
+ . filter ( ( timeout : number ) => ! Number . isInteger ( timeout ) ) ,
1109
+ async ( timeout : number ) => {
1110
+ const connection = newFakeConnection ( )
1111
+
1112
+ const { session, loggerFunction } = setupSession ( {
1113
+ connection,
1114
+ beginTx : false ,
1115
+ database : 'neo4j'
1116
+ } )
1117
+
1118
+ await session . run ( 'query' , { } , { timeout } )
1119
+
1120
+ expect ( loggerFunction ) . toBeCalledWith (
1121
+ 'info' ,
1122
+ `Transaction timeout expected to be an integer, got: ${ timeout } . The value will be round up.`
1123
+ )
1124
+ }
1125
+ )
1126
+ )
1127
+ } )
1128
+
1129
+ it ( 'should not log a warning for timeout configurations without sub milliseconds' , async ( ) => {
1130
+ return await fc . assert (
1131
+ fc . asyncProperty (
1132
+ fc . nat ( ) ,
1133
+ async ( timeout : number ) => {
1134
+ const connection = newFakeConnection ( )
1135
+
1136
+ const { session, loggerFunction } = setupSession ( {
1137
+ connection,
1138
+ beginTx : false ,
1139
+ database : 'neo4j'
1140
+ } )
1141
+
1142
+ await session . run ( 'query' , { } , { timeout } )
1143
+
1144
+ expect ( loggerFunction ) . not . toBeCalledWith (
1145
+ 'info' ,
1146
+ `Transaction timeout expected to be an integer, got: ${ timeout } . The value will be round up.`
1147
+ )
1148
+ }
1149
+ )
1150
+ )
1151
+ } )
989
1152
} )
990
1153
} )
991
1154
@@ -1049,8 +1212,10 @@ function setupSession ({
1049
1212
bookmarkManager ?: BookmarkManager
1050
1213
notificationFilter ?: NotificationFilter
1051
1214
auth ?: AuthToken
1052
- } ) : { session : Session , connectionProvider : ConnectionProvider } {
1215
+ } ) : { session : Session , connectionProvider : ConnectionProvider , loggerFunction : LoggerFunction } {
1053
1216
const connectionProvider = new ConnectionProvider ( )
1217
+ const loggerFunction = jest . fn ( )
1218
+ const log = new Logger ( 'debug' , loggerFunction )
1054
1219
connectionProvider . acquireConnection = jest . fn ( async ( ) => await Promise . resolve ( connection ) )
1055
1220
connectionProvider . close = async ( ) => await Promise . resolve ( )
1056
1221
@@ -1064,13 +1229,14 @@ function setupSession ({
1064
1229
bookmarks : lastBookmarks ,
1065
1230
bookmarkManager,
1066
1231
notificationFilter,
1067
- auth
1232
+ auth,
1233
+ log
1068
1234
} )
1069
1235
1070
1236
if ( beginTx ) {
1071
1237
session . beginTransaction ( ) . catch ( e => { } ) // force session to acquire new connection
1072
1238
}
1073
- return { session, connectionProvider }
1239
+ return { session, connectionProvider, loggerFunction }
1074
1240
}
1075
1241
1076
1242
function newFakeConnection ( ) : FakeConnection {
0 commit comments