@@ -20,6 +20,7 @@ import {
20
20
NftEvent ,
21
21
Transaction ,
22
22
} from 'client/src/types' ;
23
+ import { Socket } from 'node:net' ;
23
24
24
25
describe ( 'socket-io' , ( ) => {
25
26
let apiServer : ApiServer ;
@@ -40,6 +41,62 @@ describe('socket-io', () => {
40
41
await migrate ( 'down' ) ;
41
42
} ) ;
42
43
44
+ test ( 'socket-io-client > reconnect' , async ( ) => {
45
+ const serverSocketConnectWaiter = waiter < Socket > ( ) ;
46
+ apiServer . server . once ( 'upgrade' , ( _req , socket : Socket ) => {
47
+ serverSocketConnectWaiter . finish ( socket ) ;
48
+ } ) ;
49
+
50
+ const client = new StacksApiSocketClient ( {
51
+ url : `http://${ apiServer . address } ` ,
52
+ // socketOpts: { reconnection: false },
53
+ } ) ;
54
+
55
+ const updateWaiter : Waiter < Block > = waiter ( ) ;
56
+ const subResult = client . subscribeBlocks ( block => updateWaiter . finish ( block ) ) ;
57
+
58
+ // subscriptions should be saved in the client query obj
59
+ expect ( client . socket . io . opts . query ) . toMatchObject ( { subscriptions : 'block' } ) ;
60
+
61
+ // wait for initial client connection
62
+ await new Promise < void > ( resolve => client . socket . once ( 'connect' , resolve ) ) ;
63
+
64
+ const connectAttempt = waiter ( ) ;
65
+ client . socket . io . once ( 'reconnect_attempt' , attempt => {
66
+ // subscriptions should be saved in the client query obj
67
+ expect ( client . socket . io . opts . query ) . toMatchObject ( { subscriptions : 'block' } ) ;
68
+ connectAttempt . finish ( ) ;
69
+ } ) ;
70
+
71
+ const reconnectWaiter = waiter ( ) ;
72
+ client . socket . io . once ( 'reconnect' , ( ) => reconnectWaiter . finish ( ) ) ;
73
+
74
+ // force kill client connection on the server to trigger reconnect
75
+ const serverSocket = await serverSocketConnectWaiter ;
76
+ serverSocket . resetAndDestroy ( ) ;
77
+
78
+ await connectAttempt ;
79
+ await reconnectWaiter ;
80
+
81
+ // ensure client still waiting for block update
82
+ expect ( updateWaiter . isFinished ) . toBe ( false ) ;
83
+
84
+ const block = new TestBlockBuilder ( { block_hash : '0x1234' , burn_block_hash : '0x5454' } )
85
+ . addTx ( { tx_id : '0x4321' } )
86
+ . build ( ) ;
87
+ await db . update ( block ) ;
88
+
89
+ const result = await updateWaiter ;
90
+ try {
91
+ expect ( result . hash ) . toEqual ( '0x1234' ) ;
92
+ expect ( result . burn_block_hash ) . toEqual ( '0x5454' ) ;
93
+ expect ( result . txs [ 0 ] ) . toEqual ( '0x4321' ) ;
94
+ } finally {
95
+ subResult . unsubscribe ( ) ;
96
+ client . socket . close ( ) ;
97
+ }
98
+ } ) ;
99
+
43
100
test ( 'socket-io-client > block updates' , async ( ) => {
44
101
const client = new StacksApiSocketClient ( {
45
102
url : `http://${ apiServer . address } ` ,
0 commit comments