@@ -332,8 +332,14 @@ module.exports = (common) => {
332
332
} )
333
333
} )
334
334
335
- describe ( 'load tests' , ( ) => {
336
- it ( 'send/receive 10k messages' , ( done ) => {
335
+ describe ( 'load tests' , function ( ) {
336
+ // Write the progress to stdout when in Node.js, silent when in the browser
337
+ const log = process && process . stdout ? ( s ) => process . stdout . write ( s ) : ( ) => { }
338
+
339
+ it ( 'send/receive 10k messages' , function ( done ) {
340
+ // js-ipfs is a little slow atm, so make sure we have enough time
341
+ this . timeout ( 2 * 60 * 1000 )
342
+
337
343
const expectedString = 'hello'
338
344
const count = 10000
339
345
let sendCount = 0
@@ -350,23 +356,36 @@ module.exports = (common) => {
350
356
expect ( err ) . to . not . exists
351
357
352
358
const outputProgress = ( ) => {
353
- /*
354
- process.stdout.write(' \r')
355
- process.stdout.write('Sent: ' + sendCount + ' of ' + count + ', Received: ' + receivedCount + '\r')
356
- */
359
+ log ( ' \r' )
360
+ log ( 'Sent: ' + sendCount + ' of ' + count + ', Received: ' + receivedCount + '\r' )
357
361
}
358
362
359
363
subscription . on ( 'data' , ( d ) => {
360
364
expect ( d . data . toString ( ) ) . to . equal ( expectedString )
365
+
361
366
receivedCount ++
362
367
outputProgress ( )
363
368
if ( receivedCount >= count ) {
364
369
const duration = new Date ( ) . getTime ( ) - startTime
365
- console . log ( `Send/Receive 10k messages took: ${ duration } ms, ${ Math . floor ( count / ( duration / 1000 ) ) } ops / s` )
370
+ log ( `Send/Receive 10k messages took: ${ duration } ms, ${ Math . floor ( count / ( duration / 1000 ) ) } ops / s\n ` )
366
371
subscription . cancel ( )
367
372
. then ( ( ) => subscription2 . cancel ( ) )
368
373
. then ( done )
369
374
}
375
+
376
+ // Check for early cancel.
377
+ // We had bugs in the past where all 10k messages would be
378
+ // buffered first and only then the receiving stream would
379
+ // process them.
380
+ // This check will make sure, that while the sending peer is
381
+ // sending messages the receiving peer has started receiving
382
+ // and processing them.
383
+ if ( sendCount - ( count / 10 ) > receivedCount ) {
384
+ sendCount = count
385
+ subscription . cancel ( )
386
+ . then ( ( ) => subscription2 . cancel ( ) )
387
+ . then ( ( ) => done ( new Error ( `Didn't receive enough messages on time!` ) ) )
388
+ }
370
389
} )
371
390
372
391
function loop ( ) {
@@ -396,10 +415,8 @@ module.exports = (common) => {
396
415
function loop ( ) {
397
416
if ( sendCount < count ) {
398
417
sendCount ++
399
- /*
400
- process.stdout.write(' \r')
401
- process.stdout.write('Sending messages: ' + sendCount + ' of ' + count + '\r')
402
- */
418
+ log ( ' \r' )
419
+ log ( 'Sending messages: ' + sendCount + ' of ' + count + '\r' )
403
420
ipfs1 . pubsub . publish ( topic , expectedString , ( err ) => {
404
421
expect ( err ) . to . not . exist
405
422
process . nextTick ( ( ) => loop ( ) )
@@ -420,10 +437,8 @@ module.exports = (common) => {
420
437
function loop ( ) {
421
438
if ( sendCount < count ) {
422
439
sendCount ++
423
- /*
424
- process.stdout.write(' \r')
425
- process.stdout.write('Subscribing: ' + sendCount + ' of ' + count + '\r')
426
- */
440
+ log ( ' \r' )
441
+ log ( 'Subscribing: ' + sendCount + ' of ' + count + '\r' )
427
442
ipfs1 . pubsub . subscribe ( topic , ( err , res ) => {
428
443
receivedCount ++
429
444
// First call should go through normally
@@ -447,13 +462,10 @@ module.exports = (common) => {
447
462
it ( 'subscribe/unsubscribe 1k times' , ( done ) => {
448
463
const count = 1000
449
464
let sendCount = 0
450
- // let receivedCount = 0
451
465
452
466
function outputProgress ( ) {
453
- /*
454
- process.stdout.write(' \r')
455
- process.stdout.write('Subscribe: ' + sendCount + ' of ' + count + ', Unsubscribe: ' + receivedCount + '\r')
456
- */
467
+ log ( ' \r' )
468
+ log ( 'Subscribe/Unsubscribe: ' + sendCount + ' of ' + count + '\r' )
457
469
}
458
470
459
471
function loop ( ) {
@@ -464,7 +476,6 @@ module.exports = (common) => {
464
476
expect ( err ) . to . not . exist
465
477
subscription . cancel ( ( err ) => {
466
478
expect ( err ) . to . not . exist
467
- // receivedCount++
468
479
outputProgress ( )
469
480
process . nextTick ( ( ) => loop ( ) )
470
481
} )
0 commit comments