4 <script src=
"../resources/js-test.js"></script>
7 <p id=
"description"></p>
8 <div id=
"console"></div>
11 description("Tests concurrent calls to crypto.randomValues from workers.");
13 // Generate random values from different 10 workers, and wait until they
15 var randomValues
= {};
16 var numRandomValues
= 0;
19 self
.jsTestIsAsync
= true;
21 // Asserts that each call to receivedRandomValue() contains unique bytes.
22 // The bytes are 100 randomly generated, and as such have an extremely low
23 // probability of matching each other.
24 function workerGeneratedRandomBytes(bytes
)
26 if (bytes
.length
!= 100)
27 throw "bytes is not the right length: " + bytes
.length
;
29 var bytesStr
= Array
.prototype.join
.call(bytes
, '');
31 if (bytesStr
in randomValues
)
32 debug("Generated a duplicate 'random' number: " + bytesStr
);
34 debug("Received random bytes from worker");
36 randomValues
[bytesStr
] = true;
38 if (++numRandomValues
== NUM_WORKERS
)
42 for (var i
= 0; i
< NUM_WORKERS
; ++i
) {
43 var worker
= new Worker('random-values-concurrent.js');
44 worker
.onmessage = function(event
)
46 if (event
.data
instanceof Uint8Array
)
47 workerGeneratedRandomBytes(event
.data
);
49 debug('[Worker] ' + event
.data
);
51 worker
.onerror = function(event
)
53 debug('Got error from worker: ' + event
.message
);