Skip to content

Commit a32bd16

Browse files
committed
[blocking_event_loop] Make verification more robust.
Needs proper translation timoxley#93.
1 parent 5fdda24 commit a32bd16

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

exercises/blocking_event_loop/wrapper.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ var count = 0
88
var CYCLES = 10000
99

1010
function operation() {
11-
for (var i = 0; i < 1000000; i++) {} // burn some CPU cycles
12-
count++ // count how many times this function was called
11+
for (var i = 0; i < 1000000; i++) {
12+
// burn some CPU cycles
13+
}
14+
// count how many times this function was called
15+
count++
1316
}
1417

1518

@@ -27,10 +30,13 @@ repeat(operation, CYCLES)
2730
setTimeout(function() {
2831
var end = Date.now()
2932
console.error('Performed %d operations.', count)
30-
if (count === CYCLES) console.log('Fail! Should not have completed all operations!')
31-
// TODO calculate ideal ops per second?
32-
//if (count < 1000) console.log('Fail! Should have performed more operations!')
33-
if (end - start < 1500) console.log('Interrupted in approximately 1 second!')
34-
else console.log('Fail! Interrupted in %d milliseconds', end - start)
33+
34+
if (count >= CYCLES) {
35+
console.log('Fail! Should not have completed all operations!')
36+
process.exit(1)
37+
}
38+
39+
console.log('Operations successfully interrupted!')
40+
console.error('Interrupted after %d milliseconds.', end - start)
3541
process.exit()
36-
}, 1000)
42+
})

exercises/blocking_event_loop/wrapper_fr.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ function operation() {
1111
for (var i = 0; i < 1000000; i++) {
1212
// burn some CPU cycles
1313
}
14-
count++ // count how many times this function was called
14+
// count how many times this function was called
15+
count++
1516
}
1617

1718
console.error()
@@ -28,8 +29,13 @@ repeat(operation, CYCLES)
2829
setTimeout(function() {
2930
var end = Date.now()
3031
console.error('J’ai effectué %d opérations.', count)
31-
if (count === CYCLES) console.log('Raté ! Je n’aurais pas du pouvoir aller au bout !')
32-
if (end - start < 1500) console.log('Interruption au bout d’environ 1 seconde !')
33-
else console.log('Raté ! Interruption au bout de %d millisecondes', end - start)
32+
33+
if (count >= CYCLES) {
34+
console.log('Raté ! Je n’aurais pas du pouvoir aller au bout !')
35+
process.exit(1)
36+
}
37+
38+
console.log('Opérations interrompues succès !')
39+
console.error('Interrompu après %d millisecondes.', end - start)
3440
process.exit()
35-
}, 1000)
41+
})

0 commit comments

Comments
 (0)