Skip to content

Commit

Permalink
Fix CI correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
leohubert committed Nov 9, 2020
1 parent 9ea66a8 commit ee0c3d3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@
"**/*.spec.js"
]
}
}
}
9 changes: 9 additions & 0 deletions src/lib/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ export class Dispatcher {
}
}
}

clean() {
Object.values(this.events).forEach((event: DispatcherEvent) => {
event.callbacks.forEach((callback) => {
event.unregisterCallback(callback);
});
});
this.events = {};
}
}
30 changes: 10 additions & 20 deletions src/lib/Downloader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function waitDownloadForEnd(downloader) {
downloader.on('error', (err) => {
reject(err);
});
downloader.start();
});
}

Expand All @@ -63,7 +64,6 @@ function checkFileIntegrity(
return false;
}
}

return true;
}

Expand All @@ -81,7 +81,7 @@ test('createDownloader return Downloader instance', (t) => {
t.true(downloader instanceof Downloader);
});

test('addFile must increment files in stats', (t) => {
test('addFile must increment files in stats', async (t) => {
const downloader = createDownloader();
const file = filesLibrary[0];
t.is(0, downloader.stats().files);
Expand All @@ -93,10 +93,9 @@ test('start function must start downloads', async (t) => {
const downloader = createDownloader();

t.is(0, downloader.stats().fileDownloaded);
filesLibrary.forEach((file) => {
for (const file of filesLibrary) {
downloader.addFile(file.url, installPath);
});
await downloader.start();
}
await waitDownloadForEnd(downloader);
t.is(filesLibrary.length, downloader.stats().fileDownloaded);

Expand All @@ -110,10 +109,9 @@ test('downloads files checksum must be created if specified', async (t) => {
downloader.checksumAlgo = 'sha1';

t.is(0, downloader.stats().fileDownloaded);
filesLibrary.forEach((file) => {
for (const file of filesLibrary) {
downloader.addFile(file.url, installPath, null, file.sha1);
});
await downloader.start();
}
await waitDownloadForEnd(downloader);
t.is(filesLibrary.length, downloader.stats().fileDownloaded);

Expand All @@ -127,10 +125,9 @@ test('downloads files checksum with another algo must work', async (t) => {
downloader.checksumAlgo = 'md5';

t.is(0, downloader.stats().fileDownloaded);
filesLibrary.forEach((file) => {
for (const file of filesLibrary) {
downloader.addFile(file.url, installPath, null, file.md5);
});
await downloader.start();
}
await waitDownloadForEnd(downloader);
t.is(filesLibrary.length, downloader.stats().fileDownloaded);

Expand All @@ -145,8 +142,6 @@ test('downloads files with wrong checksum must fail', async (t) => {
downloader.checksumAlgo = 'md5';

downloader.addFile(filesLibrary[0].url, installPath, null, badChecksum);
await downloader.start();

try {
await waitDownloadForEnd(downloader);
t.fail();
Expand All @@ -160,10 +155,9 @@ test('downloads with low simulataneousDownload must download all files', async (
downloader.simultaneusDownloads = 1;

t.is(0, downloader.stats().fileDownloaded);
filesLibrary.forEach((file) => {
for (const file of filesLibrary) {
downloader.addFile(file.url, installPath);
});
await downloader.start();
}
await waitDownloadForEnd(downloader);
t.is(filesLibrary.length, downloader.stats().fileDownloaded);
});
Expand All @@ -177,7 +171,6 @@ test('download with existing file and correct checksum must not restart download
return newDownloader;
};
let downloader = createDownload();
await downloader.start();
await waitDownloadForEnd(downloader);

let stats = getFileStats(file.name);
Expand All @@ -187,7 +180,6 @@ test('download with existing file and correct checksum must not restart download
// @ts-ignore
setTimeout(async () => {
downloader = createDownload();
await downloader.start();
await waitDownloadForEnd(downloader);

stats = getFileStats(file.name);
Expand All @@ -210,7 +202,6 @@ test('two files with same name must redownload', async (t) => {
return newDownloader;
};
let downloader = createDownload(file1);
await downloader.start();
await waitDownloadForEnd(downloader);
t.true(checkFileIntegrity(fileName, file1.sha1, downloader.checksumAlgo));

Expand All @@ -221,7 +212,6 @@ test('two files with same name must redownload', async (t) => {
// @ts-ignore
setTimeout(async () => {
downloader = createDownload(file2);
await downloader.start();
await waitDownloadForEnd(downloader);

t.true(checkFileIntegrity(fileName, file2.sha1, downloader.checksumAlgo));
Expand Down
30 changes: 13 additions & 17 deletions src/lib/Downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Downloader {
public maxRetries = 3;
public checksumAlgo = 'sha256';

private downloaderOptions = {};
private readonly downloaderOptions = {};

constructor(downloaderOptions = {}) {
this.downloaderOptions = downloaderOptions;
Expand Down Expand Up @@ -63,12 +63,7 @@ export class Downloader {
} else {
return true;
}

if (localChecksum !== checksum) {
return true;
}

return false;
return localChecksum !== checksum;
}

private async startDownloader(downloader) {
Expand Down Expand Up @@ -116,14 +111,15 @@ export class Downloader {
checksum
);
}
await this.downloaderCompleted(downloader);

this.downloaderCompleted(downloader);
});

if (
!this.forceDownload &&
!(await this.isFileNeedUpdate(downloader.filePath, downloader.checksum))
) {
await this.downloaderCompleted(downloader, true);
this.downloaderCompleted(downloader, true);
return;
}

Expand All @@ -133,13 +129,11 @@ export class Downloader {
await downloader.start();
}

private async downloaderCompleted(downloader, pass = false) {
private downloaderCompleted(downloader, pass = false) {
this.filesDownloaded++;
const stats = downloader.getStats();
if (pass) {
const stats = await downloader.getTotalSize();
const fileSize = stats.total;
this.bytesDownloaded += fileSize;
this.bytesDownloaded += downloader.fileSize;
}

this.progress = (this.bytesDownloaded * 100) / this.bytesToDownload;
Expand Down Expand Up @@ -190,6 +184,7 @@ export class Downloader {
if (this.state === DownloaderState.DOWNLOADING) {
throw new Error('Cannot clean while downloading.');
}
//this.dispatcher.clean();
this.downloadersQueue = [];
this.downloadersInProgress = [];
this.filesToDownload = 0;
Expand Down Expand Up @@ -242,11 +237,12 @@ export class Downloader {

this.filesToDownload = this.downloadersQueue.length;

await this.downloadersQueue.forEach(async (downloader) => {
for (const downloader of this.downloadersQueue) {
const stats = await downloader.getTotalSize();
const fileSize = stats.total;
this.bytesToDownload = this.bytesToDownload + fileSize;
});
downloader.fileSize = fileSize;
}

this.downloadersQueue.forEach(() => {
if (this.startNextDownloader() == false) {
Expand All @@ -270,15 +266,15 @@ export class Downloader {
});
}

resume() {
async resume() {
this.state = DownloaderState.DOWNLOADING;

if (this.downloadersInProgress.length > 0) {
this.downloadersInProgress.forEach((downloader) => {
downloader.resume();
});
} else {
this.startNextDownloader();
await this.startNextDownloader();
}
}

Expand Down

0 comments on commit ee0c3d3

Please sign in to comment.