Skip to content

Commit f82e10c

Browse files
committed
More reliable worker thread test
1 parent 7c50ce5 commit f82e10c

File tree

4 files changed

+119
-66
lines changed

4 files changed

+119
-66
lines changed

test/tests/clone.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var { Worker } = require("worker_threads");
21
var path = require("path");
32
var assert = require("assert");
43
var fse = require("fs-extra");
@@ -55,52 +54,6 @@ describe("Clone", function() {
5554
});
5655
});
5756

58-
it("can clone with https via worker thread", function(done) {
59-
const workerPath = path.join(__dirname, "../utils/clone_worker.js");
60-
const worker = new Worker(workerPath, {
61-
workerData: {
62-
clonePath,
63-
url: "https://github.com/nodegit/test.git"
64-
}
65-
});
66-
worker.on("message", (success) => {
67-
if (success) {
68-
done();
69-
} else {
70-
assert.fail();
71-
}
72-
});
73-
worker.on("error", () => assert.fail());
74-
worker.on("exit", (code) => {
75-
if (code !== 0) {
76-
assert.fail();
77-
}
78-
});
79-
});
80-
81-
for (let i = 0; i < 5; ++i) {
82-
it(`can kill worker thread while cloning #${i}`, function(done) { // jshint ignore:line
83-
const workerPath = path.join(__dirname, "../utils/clone_worker.js");
84-
const worker = new Worker(workerPath, {
85-
workerData: {
86-
clonePath,
87-
url: "https://github.com/nodegit/test.git"
88-
}
89-
});
90-
worker.on("error", () => assert.fail());
91-
worker.on("message", () => assert.fail());
92-
worker.on("exit", (code) => {
93-
if (code === 1) {
94-
done();
95-
} else {
96-
assert.fail();
97-
}
98-
});
99-
100-
setTimeout(() => { worker.terminate(); }, 500);
101-
});
102-
}
103-
10457
it("can clone twice with https using same config object", function() {
10558
var test = this;
10659
var url = "https://github.com/nodegit/test.git";

test/tests/worker.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const { Worker } = require("worker_threads");
2+
const path = require("path");
3+
const assert = require("assert");
4+
const fse = require("fs-extra");
5+
const local = path.join.bind(path, __dirname);
6+
7+
describe("Worker", function() {
8+
const clonePath = local("../repos/clone");
9+
10+
// Set a reasonable timeout here now that our repository has grown.
11+
this.timeout(30000);
12+
13+
beforeEach(function() {
14+
return fse.remove(clonePath).catch(function(err) {
15+
console.log(err);
16+
17+
throw err;
18+
});
19+
});
20+
21+
it("can perform basic functionality via worker thread", function(done) {
22+
const workerPath = local("../utils/worker.js");
23+
const worker = new Worker(workerPath, {
24+
workerData: {
25+
clonePath,
26+
url: "https://github.com/nodegit/test.git"
27+
}
28+
});
29+
worker.on("message", (message) => {
30+
switch (message) {
31+
case "init":
32+
break;
33+
case "success":
34+
done();
35+
break;
36+
case "failure":
37+
assert.fail();
38+
break;
39+
}
40+
});
41+
worker.on("error", () => assert.fail());
42+
worker.on("exit", (code) => {
43+
if (code !== 0) {
44+
assert.fail();
45+
}
46+
});
47+
});
48+
49+
for (let i = 0; i < 5; ++i) {
50+
it(`can kill worker thread while in use #${i}`, function(done) { // jshint ignore:line
51+
const workerPath = local("../utils/worker.js");
52+
const worker = new Worker(workerPath, {
53+
workerData: {
54+
clonePath,
55+
url: "https://github.com/nodegit/test.git"
56+
}
57+
});
58+
worker.on("message", (message) => {
59+
switch (message) {
60+
case "init":
61+
setTimeout(() => { worker.terminate(); }, 500);
62+
break;
63+
case "success":
64+
assert.fail();
65+
break;
66+
case "failure":
67+
assert.fail();
68+
break;
69+
}
70+
});
71+
worker.on("error", () => assert.fail());
72+
worker.on("exit", (code) => {
73+
if (code === 1) {
74+
done();
75+
} else {
76+
assert.fail();
77+
}
78+
});
79+
});
80+
}
81+
});

test/utils/clone_worker.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/utils/worker.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const {
2+
isMainThread,
3+
parentPort,
4+
workerData
5+
} = require("worker_threads");
6+
const assert = require("assert");
7+
const NodeGit = require("../../");
8+
9+
if (isMainThread) {
10+
throw new Error("Must be run via worker thread");
11+
}
12+
13+
parentPort.postMessage("init");
14+
15+
const { clonePath, url } = workerData;
16+
const opts = {
17+
fetchOpts: {
18+
callbacks: {
19+
certificateCheck: () => 0
20+
}
21+
}
22+
};
23+
24+
let repository;
25+
return NodeGit.Clone(url, clonePath, opts).then((_repository) => {
26+
repository = _repository;
27+
assert.ok(repository instanceof NodeGit.Repository);
28+
return repository.index();
29+
}).then((index) => {
30+
assert.ok(index instanceof NodeGit.Index);
31+
return repository.getRemoteNames();
32+
}).then((remotes) => {
33+
assert.ok(Array.isArray(remotes));
34+
return repository.getCurrentBranch();
35+
}).then((branch) => {
36+
assert.ok(branch instanceof NodeGit.Reference);
37+
parentPort.postMessage("success");
38+
}).catch(() => parentPort.postMessage("failure"));

0 commit comments

Comments
 (0)