Repository navigation
Expand file tree
/
Copy paththread_safety.js
More file actions
64 lines (53 loc) · 1.95 KB
/
Copy paththread_safety.js
File metadata and controls
64 lines (53 loc) · 1.95 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
var assert = require("assert");
var path = require("path");
var local = path.join.bind(path, __dirname);
describe("ThreadSafety", function() {
var NodeGit = require("../../");
var Repository = NodeGit.Repository;
var reposPath = local("../repos/workdir");
beforeEach(function() {
var test = this;
return Repository.open(reposPath)
.then(function(repo) {
test.repository = repo;
return repo.openIndex();
})
.then(function(index) {
test.index = index;
});
});
it("can enable and disable thread safety", function() {
var originalValue = NodeGit.isThreadSafetyEnabled();
NodeGit.enableThreadSafety();
assert.equal(true, NodeGit.isThreadSafetyEnabled());
NodeGit.disableThreadSafety();
assert.equal(false, NodeGit.isThreadSafetyEnabled());
// flip the switch again, to make sure we test all transitions
// (we could have started with thread safety enabled)
NodeGit.enableThreadSafety();
assert.equal(true, NodeGit.isThreadSafetyEnabled());
if (originalValue) {
NodeGit.enableThreadSafety();
} else {
NodeGit.disableThreadSafety();
}
});
it("can lock something and cleanup mutex", function() {
// call a sync method to guarantee that it stores a mutex,
// and that it will clean up the mutex in a garbage collection cycle
this.repository.headDetached();
var diagnostics = NodeGit.getThreadSafetyDiagnostics();
if (NodeGit.isThreadSafetyEnabled()) {
// this is a fairly vague test - it just tests that something
// had a mutex created for it at some point (i.e., the thread safety
// code is not completely dead)
assert.ok(diagnostics.storedMutexesCount > 0);
// now test that GC cleans up mutexes
global.gc();
diagnostics = NodeGit.getThreadSafetyDiagnostics();
assert.equal(0, diagnostics.storedMutexesCount);
} else {
assert.equal(0, diagnostics.storedMutexesCount);
}
});
});