From 6f176f8b305f58ca7fc38f119cc06de0b3de023b Mon Sep 17 00:00:00 2001
From: Kay Lukas <kay.lukas@gmail.com>
Date: Thu, 26 Jul 2018 15:34:24 +0200
Subject: [PATCH] Fix multiple workers

---
 src/worker/async_proxy.js |  2 +-
 test/general/openpgp.js   | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js
index d6f517e6..f1c767d7 100644
--- a/src/worker/async_proxy.js
+++ b/src/worker/async_proxy.js
@@ -135,7 +135,7 @@ AsyncProxy.prototype.delegate = function(method, options) {
 
   const id = this.getID();
   const requests = this.workers.map(worker => worker.requests);
-  const minRequests = Math.min(requests);
+  const minRequests = Math.min(...requests);
   let workerId = 0;
   for(; workerId < this.workers.length; workerId++) {
     if (this.workers[workerId].requests === minRequests) {
diff --git a/test/general/openpgp.js b/test/general/openpgp.js
index bb4a860e..fee80aa7 100644
--- a/test/general/openpgp.js
+++ b/test/general/openpgp.js
@@ -744,6 +744,40 @@ describe('OpenPGP.js public api tests', function() {
         });
       });
 
+      it('Test multiple workers', async function() {
+        openpgp.config.show_version = false;
+        openpgp.config.commentstring = 'different';
+        if (!openpgp.getWorker()) {
+          return;
+        }
+        const { workers } = openpgp.getWorker();
+        try {
+          await privateKey.keys[0].decrypt(passphrase)
+          openpgp.initWorker({path: '../dist/openpgp.worker.js', workers, n: 2});
+
+          const workerTest = (_, index) => {
+            const plaintext = input.createSomeMessage() + index + '\n한국어/조선말';
+            return openpgp.encrypt({
+              publicKeys: publicKey.keys,
+              data: plaintext
+            }).then(function (encrypted) {
+              expect(encrypted.data).to.exist;
+              expect(encrypted.data).not.to.match(/^Version:/);
+              expect(encrypted.data).to.match(/Comment: different/);
+              return openpgp.decrypt({
+                privateKeys: privateKey.keys[0],
+                message: openpgp.message.readArmored(encrypted.data)
+              });
+            }).then(function (decrypted) {
+              expect(decrypted.data).to.equal(plaintext);
+            });
+          };
+          await Promise.all(Array(10).fill(null).map(workerTest));
+        } finally {
+          openpgp.initWorker({path: '../dist/openpgp.worker.js', workers, n: 1 });
+        }
+      });
+
       it('Calling decrypt with not decrypted key leads to exception', function() {
         const encOpt = {
           data: plaintext,