diff --git a/spec/git-spec.js b/spec/git-spec.js index ac51d274..7acd2c99 100644 --- a/spec/git-spec.js +++ b/spec/git-spec.js @@ -6,6 +6,7 @@ const wrench = require('wrench') const temp = require('temp') const _ = require('underscore') const {it, fit, beforeEach} = require('./async-spec-helper-functions') +const {Worker} = require('worker_threads') describe('git', () => { let repo @@ -1138,6 +1139,35 @@ describe('git', () => { }) }) +describe('Context Aware', () => { + it('should handle opening the same repository from different contexts', async () => { + // Based on it('returns the path to the .git directory') + let currentGitPath = path.join(path.dirname(__dirname), '.git/') + if (process.platform === 'win32') { currentGitPath = currentGitPath.replace(/\\/g, '/') } + const promises = []; + for (let i = 1; i <= 5; ++i) { + promises.push(new Promise((resolve, reject) => { + const worker = new Worker('./spec/worker.cjs'); + let repositoryPath; + worker.on('message', function(message) { + repositoryPath = message; + }); + worker.on('exit', function(code) { + if (code === 0) { + resolve(repositoryPath) + } + else { + reject(`Worker error ${code}`) + } + }); + })) + } + const responses = await Promise.all(promises) + const c = currentGitPath; + expect(responses).toEqual([c, c, c, c, c]) + }); +}) + function execCommands (commands, callback) { let command if (process.platform === 'win32') { diff --git a/spec/worker.cjs b/spec/worker.cjs new file mode 100644 index 00000000..e0022a58 --- /dev/null +++ b/spec/worker.cjs @@ -0,0 +1,6 @@ +const parentPort = require('worker_threads').parentPort + +const git = require('../src/git') + +const repositoryPath = git.open(__dirname).getPath() +parentPort.postMessage(repositoryPath); diff --git a/src/repository.cc b/src/repository.cc index fb995f05..ad56bd53 100644 --- a/src/repository.cc +++ b/src/repository.cc @@ -24,7 +24,7 @@ #include #include -void Repository::Init(Local target) { +NAN_MODULE_INIT(Repository::Init) { Nan::HandleScope scope; git_libgit2_init(); @@ -68,7 +68,7 @@ void Repository::Init(Local target) { Nan::GetFunction(newTemplate).ToLocalChecked()); } -NODE_MODULE(git, Repository::Init) +NAN_MODULE_WORKER_ENABLED(git, Repository::Init) NAN_METHOD(Repository::New) { Nan::HandleScope scope; diff --git a/src/repository.h b/src/repository.h index cddc4465..9ddbf370 100644 --- a/src/repository.h +++ b/src/repository.h @@ -31,7 +31,7 @@ using namespace v8; // NOLINT class Repository : public Nan::ObjectWrap { public: - static void Init(Local target); + static NAN_MODULE_INIT(Init); private: static NAN_METHOD(New);