forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
216 lines (204 loc) · 6.36 KB
/
worker.js
File metadata and controls
216 lines (204 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const path = require("path");
const assert = require("assert");
const fse = require("fs-extra");
const local = path.join.bind(path, __dirname);
const NodeGit = require("../../");
let filterName = "psuedo_filter";
let Worker;
try {
Worker = require("worker_threads").Worker;
} catch (e) {}
if (Worker) {
describe("Worker", function() {
const clonePath = local("../repos/clone");
// Set a reasonable timeout here now that our repository has grown.
this.timeout(30000);
beforeEach(function() {
return fse.remove(clonePath).catch(function(err) {
console.log(err);
throw err;
});
});
afterEach(function() {
return NodeGit.FilterRegistry.unregister(filterName)
.catch(function(error) {
if (error === NodeGit.Error.CODE.ERROR) {
throw new Error("Cannot unregister filter");
}
});
});
it("can perform basic functionality via worker thread", function(done) {
const workerPath = local("../utils/worker.js");
const worker = new Worker(workerPath, {
workerData: {
clonePath,
url: "https://github.com/nodegit/test.git"
}
});
worker.on("message", (message) => {
switch (message) {
case "init":
break;
case "success":
done();
break;
case "failure":
assert.fail();
break;
}
});
worker.on("error", () => assert.fail());
worker.on("exit", (code) => {
if (code !== 0) {
assert.fail();
}
});
});
for (let i = 0; i < 5; ++i) {
// disabled until we can address flakiness
it.skip(`can kill worker thread while in use #${i}`, function(done) { // jshint ignore:line
const workerPath = local("../utils/worker.js");
const worker = new Worker(workerPath, {
workerData: {
clonePath,
url: "https://github.com/nodegit/test.git"
}
});
worker.on("message", (message) => {
switch (message) {
case "init":
setTimeout(() => { worker.terminate(); }, 500);
break;
case "success":
assert.fail();
break;
case "failure":
assert.fail();
break;
}
});
worker.on("error", () => assert.fail());
worker.on("exit", (code) => {
if (code === 1) {
done();
} else {
assert.fail();
}
});
});
}
// NOTE: first try was to build a test measuring memory used, checking
// that memory allocated by objects was being freed, but it was problematic
// to obtain the memory freed by a different context (a worker) after the
// context was gone, and the data in the tests wasn't consistent.
// So instead this test checks that the count of objects created/destroyed
// during the test match the count of objects being tracked by the
// nodegit::Context, which will be destroyed on context shutdown. To check
// that they are actually being freed can be done with a debugger/profiler.
it("can track objects to free on context shutdown", function(done) {
let testOk;
const workerPath = local("../utils/worker_context_aware.js");
const worker = new Worker(workerPath, {
workerData: {
clonePath,
url: "https://github.com/nodegit/test.git"
}
});
worker.on("message", (message) => {
switch (message) {
case "numbersMatch":
testOk = true;
worker.terminate();
break;
case "numbersDoNotMatch":
testOk = false;
worker.terminate();
break;
case "failure":
assert.fail();
break;
}
});
worker.on("error", () => assert.fail());
worker.on("exit", (code) => {
if (code === 1 && testOk === true) {
done();
}
else {
assert.fail();
}
});
});
// This tests that while calling filter's apply callbacks and the worker
// is terminated, node exits gracefully. To make sure we terminate the
// worker during a checkout, continuous checkouts will be running in a loop.
it("can kill worker thread while doing a checkout and exit gracefully", function(done) { // jshint ignore:line
const workerPath = local("../utils/worker_checkout.js");
const worker = new Worker(workerPath, {
workerData: {
clonePath,
url: "https://github.com/nodegit/test.git"
}
});
worker.on("message", (message) => {
switch (message) {
case "init":
// give enough time for the worker to start applying the filter
// during continuous checkouts
setTimeout(() => { worker.terminate(); }, 10000);
break;
case "success":
assert.fail();
break;
case "failure":
assert.fail();
break;
}
});
worker.on("error", () => assert.fail());
worker.on("exit", (code) => {
if (code == 1) {
done();
} else {
assert.fail();
}
});
});
// This tests that after calling filter's apply callbacks and the worker
// is terminated, there will be no memory leaks.
it("can track objects to free on context shutdown after multiple checkouts", function(done) { // jshint ignore:line
let testOk;
const workerPath = local("../utils/worker_context_aware_checkout.js");
const worker = new Worker(workerPath, {
workerData: {
clonePath,
url: "https://github.com/nodegit/test.git"
}
});
worker.on("message", (message) => {
switch (message) {
case "numbersMatch":
testOk = true;
worker.terminate();
break;
case "numbersDoNotMatch":
testOk = false;
worker.terminate();
break;
case "failure":
assert.fail();
break;
}
});
worker.on("error", () => assert.fail());
worker.on("exit", (code) => {
if (code === 1 && testOk === true) {
done();
}
else {
assert.fail();
}
});
});
});
}