@@ -21,7 +21,6 @@ import { IPublishPacket, IPubrelPacket, ISubackPacket, QoS } from 'mqtt-packet'
21
21
import { DoneCallback , ErrorWithReasonCode } from 'src/lib/shared'
22
22
import { fail } from 'assert'
23
23
import { describe , it , beforeEach , afterEach } from 'node:test'
24
- import { nextTick } from 'process'
25
24
26
25
/**
27
26
* These tests try to be consistent with names for servers (brokers) and clients,
@@ -44,6 +43,10 @@ import { nextTick } from 'process'
44
43
*
45
44
*/
46
45
46
+ const fakeTimersOptions = {
47
+ shouldClearNativeTimers : true ,
48
+ }
49
+
47
50
export default function abstractTest ( server , config , ports ) {
48
51
const version = config . protocolVersion || 4
49
52
@@ -1963,7 +1966,7 @@ export default function abstractTest(server, config, ports) {
1963
1966
1964
1967
// eslint-disable-next-line
1965
1968
beforeEach ( ( ) => {
1966
- clock = sinon . useFakeTimers ( )
1969
+ clock = sinon . useFakeTimers ( fakeTimersOptions )
1967
1970
} )
1968
1971
1969
1972
afterEach ( ( ) => {
@@ -2054,7 +2057,7 @@ export default function abstractTest(server, config, ports) {
2054
2057
timeout : 10000 ,
2055
2058
} ,
2056
2059
function _test ( t , done ) {
2057
- const clock = sinon . useFakeTimers ( )
2060
+ const clock = sinon . useFakeTimers ( fakeTimersOptions )
2058
2061
2059
2062
t . after ( ( ) => {
2060
2063
clock . restore ( )
@@ -2076,6 +2079,7 @@ export default function abstractTest(server, config, ports) {
2076
2079
assert . equal ( err . message , 'Keepalive timeout' )
2077
2080
client . once ( 'connect' , ( ) => {
2078
2081
client . end ( true , done )
2082
+ clock . tick ( 100 )
2079
2083
client = null
2080
2084
} )
2081
2085
} )
@@ -2094,7 +2098,7 @@ export default function abstractTest(server, config, ports) {
2094
2098
'should not reconnect if pingresp is successful' ,
2095
2099
{ timeout : 1000 } ,
2096
2100
function _test ( t , done ) {
2097
- const clock = sinon . useFakeTimers ( )
2101
+ const clock = sinon . useFakeTimers ( fakeTimersOptions )
2098
2102
2099
2103
t . after ( ( ) => {
2100
2104
clock . restore ( )
@@ -2118,6 +2122,7 @@ export default function abstractTest(server, config, ports) {
2118
2122
if ( packet . cmd === 'pingreq' ) {
2119
2123
client . removeAllListeners ( 'close' )
2120
2124
client . end ( true , done )
2125
+ clock . tick ( 100 )
2121
2126
client = null
2122
2127
}
2123
2128
} )
@@ -2910,7 +2915,7 @@ export default function abstractTest(server, config, ports) {
2910
2915
} )
2911
2916
2912
2917
it ( 'should reconnect after stream disconnect' , function _test ( t , done ) {
2913
- const clock = sinon . useFakeTimers ( )
2918
+ const clock = sinon . useFakeTimers ( fakeTimersOptions )
2914
2919
2915
2920
t . after ( ( ) => {
2916
2921
clock . restore ( )
@@ -2929,12 +2934,13 @@ export default function abstractTest(server, config, ports) {
2929
2934
tryReconnect = false
2930
2935
} else {
2931
2936
client . end ( true , done )
2937
+ clock . tick ( 100 )
2932
2938
}
2933
2939
} )
2934
2940
} )
2935
2941
2936
2942
it ( "should emit 'reconnect' when reconnecting" , function _test ( t , done ) {
2937
- const clock = sinon . useFakeTimers ( )
2943
+ const clock = sinon . useFakeTimers ( fakeTimersOptions )
2938
2944
2939
2945
t . after ( ( ) => {
2940
2946
clock . restore ( )
@@ -2960,12 +2966,13 @@ export default function abstractTest(server, config, ports) {
2960
2966
} else {
2961
2967
assert . isTrue ( reconnectEvent )
2962
2968
client . end ( true , done )
2969
+ clock . tick ( 100 )
2963
2970
}
2964
2971
} )
2965
2972
} )
2966
2973
2967
2974
it ( "should emit 'offline' after going offline" , function _test ( t , done ) {
2968
- const clock = sinon . useFakeTimers ( )
2975
+ const clock = sinon . useFakeTimers ( fakeTimersOptions )
2969
2976
2970
2977
t . after ( ( ) => {
2971
2978
clock . restore ( )
@@ -2991,6 +2998,7 @@ export default function abstractTest(server, config, ports) {
2991
2998
} else {
2992
2999
assert . isTrue ( offlineEvent )
2993
3000
client . end ( true , done )
3001
+ clock . tick ( 100 )
2994
3002
}
2995
3003
} )
2996
3004
} )
@@ -3030,7 +3038,7 @@ export default function abstractTest(server, config, ports) {
3030
3038
timeout : 10000 ,
3031
3039
} ,
3032
3040
function _test ( t , done ) {
3033
- const clock = sinon . useFakeTimers ( )
3041
+ const clock = sinon . useFakeTimers ( fakeTimersOptions )
3034
3042
3035
3043
t . after ( ( ) => {
3036
3044
clock . restore ( )
@@ -3070,6 +3078,7 @@ export default function abstractTest(server, config, ports) {
3070
3078
)
3071
3079
}
3072
3080
} )
3081
+ clock . tick ( 100 )
3073
3082
}
3074
3083
} )
3075
3084
} ,
@@ -3084,8 +3093,8 @@ export default function abstractTest(server, config, ports) {
3084
3093
} )
3085
3094
// bind client.end so that when it is called it is automatically passed in the done callback
3086
3095
setTimeout ( ( ) => {
3087
- client . end . call ( client , done )
3088
- } , 50 )
3096
+ client . end ( done )
3097
+ } , 100 )
3089
3098
} )
3090
3099
3091
3100
it ( 'should emit connack timeout error' , function _test ( t , done ) {
0 commit comments