1+ /* Welcome to the so called hammer test, or if you want, moron test.
2+ * Here we test hammering the library in all kinds of crazy ways.
3+ * Establish connections, have them perform random operations so to
4+ * randomly explore the entire state space while performing random
5+ * operations to test strange edge cases. */
6+
7+ /* Run with AddressSanitizer enabled for best effect. */
8+
9+ /* We use websockets/ws as client counterpart */
10+ const WebSocket = require ( 'ws' ) ;
11+
12+ const uWS = require ( '../dist/uws.js' ) ;
13+ const port = 9001 ;
14+
15+ let openedClientConnections = 0 ;
16+ let closedClientConnections = 0 ;
17+
18+ let listenSocket ;
19+
20+ /* Perform random websockets/ws action */
21+ function performRandomClientAction ( ws ) {
22+ console . log ( "performing random ws action" ) ;
23+
24+ ws . send ( 'test message from client' ) ;
25+ }
26+
27+ /* Perform random uWebSockets.js action */
28+
29+ function establishNewConnection ( ) {
30+ const ws = new WebSocket ( 'ws://localhost:' + port ) ;
31+
32+ ws . on ( 'open' , ( ) => {
33+ /* Open more connections */
34+ if ( ++ openedClientConnections < 10 ) {
35+ establishNewConnection ( ) ;
36+ } else {
37+ /* Stop listening */
38+ uWS . us_listen_socket_close ( listenSocket ) ;
39+ }
40+
41+ console . log ( 'Opened ' + openedClientConnections + ' client connections so far.' ) ;
42+ performRandomClientAction ( ws ) ;
43+ } ) ;
44+
45+ ws . on ( 'message' , ( data ) => {
46+ performRandomClientAction ( ws ) ;
47+ } ) ;
48+
49+ ws . on ( 'close' , ( ) => {
50+ performRandomClientAction ( ws ) ;
51+ } ) ;
52+ }
53+
54+
55+ function performRandomServerAction ( ws ) {
56+ console . log ( "performing random server action" ) ;
57+
58+ ws . send ( 'a test message' ) ;
59+ }
60+
61+ const app = uWS . /*SSL*/ App ( {
62+ key_file_name : 'misc/key.pem' ,
63+ cert_file_name : 'misc/cert.pem' ,
64+ passphrase : '1234'
65+ } ) . ws ( '/*' , {
66+ /*compression: 0,
67+ maxPayloadLength: 16 * 1024 * 1024,
68+ idleTimeout: 10,*/
69+
70+ open : ( ws , req ) => {
71+ performRandomServerAction ( ws ) ;
72+ } ,
73+ message : ( ws , message , isBinary ) => {
74+ performRandomServerAction ( ws ) ;
75+ } ,
76+ drain : ( ws ) => {
77+ //performRandomServerAction(ws);
78+ } ,
79+ close : ( ws , code , message ) => {
80+ //performRandomServerAction(ws);
81+ }
82+ } ) . any ( '/*' , ( res , req ) => {
83+ res . end ( 'Nothing to see here!' ) ;
84+ } ) . listen ( port , ( token ) => {
85+ if ( token ) {
86+ console . log ( 'Hammering on port ' + port ) ;
87+
88+ /* Establish first connection */
89+ establishNewConnection ( ) ;
90+
91+ /* Use this to stop listening when done */
92+ listenSocket = token ;
93+ }
94+ } ) ;
95+
96+ /* Yes we do this crazy thing here */
97+ process . on ( 'beforeExit' , ( ) => {
98+ app . forcefully_free ( ) ;
99+ } ) ;
0 commit comments