Randomise slow file event length

This commit is contained in:
Andras Schmelczer 2025-05-20 20:08:02 +01:00
commit ec610c77fb
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 8 additions and 7 deletions

View file

@ -78,7 +78,7 @@ export class MockClient implements FileSystemOperations {
); );
this.localFiles.set(path, newContent); this.localFiles.set(path, newContent);
this.runCallback(() => { this.executeFileOperation(() => {
void this.client.syncLocallyCreatedFile(path); void this.client.syncLocallyCreatedFile(path);
}); });
} }
@ -120,7 +120,7 @@ export class MockClient implements FileSystemOperations {
`Updated file ${path} with:\n current content: ${currentContent}\n new content: ${newContent}` `Updated file ${path} with:\n current content: ${currentContent}\n new content: ${newContent}`
); );
this.runCallback(() => { this.executeFileOperation(() => {
void this.client.syncLocallyUpdatedFile({ void this.client.syncLocallyUpdatedFile({
relativePath: path relativePath: path
}); });
@ -137,7 +137,7 @@ export class MockClient implements FileSystemOperations {
`Updated file ${path} with:\n new content: ${new TextDecoder().decode(content)}` `Updated file ${path} with:\n new content: ${new TextDecoder().decode(content)}`
); );
this.runCallback(() => { this.executeFileOperation(() => {
if (hasExisted) { if (hasExisted) {
void this.client.syncLocallyUpdatedFile({ void this.client.syncLocallyUpdatedFile({
relativePath: path relativePath: path
@ -154,7 +154,7 @@ export class MockClient implements FileSystemOperations {
); );
this.localFiles.delete(path); this.localFiles.delete(path);
this.runCallback(() => { this.executeFileOperation(() => {
void this.client.syncLocallyDeletedFile(path); void this.client.syncLocallyDeletedFile(path);
}); });
} }
@ -176,7 +176,7 @@ export class MockClient implements FileSystemOperations {
`Renamed file: ${oldPath} -> ${newPath} with:\n content ${new TextDecoder().decode(file)}` `Renamed file: ${oldPath} -> ${newPath} with:\n content ${new TextDecoder().decode(file)}`
); );
this.runCallback(() => { this.executeFileOperation(() => {
void this.client.syncLocallyUpdatedFile({ void this.client.syncLocallyUpdatedFile({
oldPath, oldPath,
relativePath: newPath relativePath: newPath
@ -184,10 +184,10 @@ export class MockClient implements FileSystemOperations {
}); });
} }
private runCallback(callback: () => void): void { private executeFileOperation(callback: () => void): void {
if (this.useSlowFileEvents) { if (this.useSlowFileEvents) {
// we aren't the best client and it takes some time to notice changes // we aren't the best client and it takes some time to notice changes
setTimeout(callback, 100); setTimeout(callback, Math.random() * 100);
} else { } else {
callback(); callback();
} }

View file

@ -4,6 +4,7 @@ import { sleep } from "./utils/sleep";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { randomCasing } from "./utils/random-casing"; import { randomCasing } from "./utils/random-casing";
// Simulate async file access by injecting waiting time before returning from file operations.
let slowFileEvents = false; let slowFileEvents = false;
async function runTest({ async function runTest({