refactor(ngcc): separate (Async/Sync)Locker and LockFile (#35861)

The previous implementation mixed up the management
of locking a piece of code (both sync and async) with the
management of writing and removing the lockFile that is
used as the flag for which process has locked the code.

This change splits these two concepts up. Apart from
avoiding the awkward base class it allows the `LockFile`
implementation to be replaced cleanly.

PR Close #35861
This commit is contained in:
Pete Bacon Darwin
2020-03-04 12:35:52 +00:00
committed by Matias Niemelä
parent bdaab4184d
commit 94fa140888
9 changed files with 379 additions and 372 deletions

View File

@ -5,40 +5,20 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {MockFileSystemNative} from '../../../src/ngtsc/file_system/testing';
import {LockFileAsync, LockFileSync} from '../../src/execution/lock_file';
import {MockLogger} from './mock_logger';
import {FileSystem} from '../../../src/ngtsc/file_system';
import {LockFile} from '../../src/execution/lock_file';
export class MockLockFileSync extends LockFileSync {
log: string[] = [];
constructor(private options: {throwOnCreate?: boolean, throwOnRemove?: boolean} = {}) {
// This `MockLockFile` is not used in tests that are run via `runInEachFileSystem()`
// So we cannot use `getFileSystem()` but instead just instantiate a mock file-system.
super(new MockFileSystemNative());
}
create() {
this.log.push('create()');
if (this.options.throwOnCreate) throw new Error('LockFile.create() error');
}
remove() {
this.log.push('remove()');
if (this.options.throwOnRemove) throw new Error('LockFile.remove() error');
}
}
export class MockLockFileAsync extends LockFileAsync {
log: string[] = [];
constructor(private options: {throwOnCreate?: boolean, throwOnRemove?: boolean} = {}) {
// This `MockLockFile` is not used in tests that are run via `runInEachFileSystem()`
// So we cannot use `getFileSystem()` but instead just instantiate a mock file-system.
super(new MockFileSystemNative(), new MockLogger(), 200, 2);
}
async create() {
this.log.push('create()');
if (this.options.throwOnCreate) throw new Error('LockFile.create() error');
}
remove() {
this.log.push('remove()');
if (this.options.throwOnRemove) throw new Error('LockFile.remove() error');
/**
* A mock implementation of `LockFile` that just logs its calls.
*/
export class MockLockFile implements LockFile {
constructor(
fs: FileSystem, private log: string[] = [], public path = fs.resolve('/lockfile'),
private pid = '1234') {}
write() { this.log.push('write()'); }
read(): string {
this.log.push('read()');
return this.pid;
}
remove() { this.log.push('remove()'); }
}