Improve testing
This commit is contained in:
parent
f73b5ecb71
commit
27423bf3cd
3 changed files with 165 additions and 63 deletions
|
|
@ -4,6 +4,7 @@ import { assert } from "../utils/assert";
|
||||||
import { LogLevel, SyncSettings } from "sync-client";
|
import { LogLevel, SyncSettings } from "sync-client";
|
||||||
import { MockClient } from "./mock-client";
|
import { MockClient } from "./mock-client";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
|
import { sleep } from "../utils/sleep";
|
||||||
|
|
||||||
export class MockAgent extends MockClient {
|
export class MockAgent extends MockClient {
|
||||||
private writtenContents: Array<string> = [];
|
private writtenContents: Array<string> = [];
|
||||||
|
|
@ -13,7 +14,8 @@ export class MockAgent extends MockClient {
|
||||||
globalFiles: Record<string, Uint8Array>,
|
globalFiles: Record<string, Uint8Array>,
|
||||||
initialSettings: Partial<SyncSettings>,
|
initialSettings: Partial<SyncSettings>,
|
||||||
public readonly name: string,
|
public readonly name: string,
|
||||||
private readonly color: string
|
private readonly color: string,
|
||||||
|
private readonly doDeletes: boolean
|
||||||
) {
|
) {
|
||||||
super(globalFiles, initialSettings);
|
super(globalFiles, initialSettings);
|
||||||
}
|
}
|
||||||
|
|
@ -47,31 +49,60 @@ export class MockAgent extends MockClient {
|
||||||
|
|
||||||
public async act(): Promise<void> {
|
public async act(): Promise<void> {
|
||||||
let options: Array<() => Promise<unknown>> = [
|
let options: Array<() => Promise<unknown>> = [
|
||||||
() =>
|
() => {
|
||||||
this.create(
|
const file = this.getFileName();
|
||||||
this.getFileName(),
|
this.client.logger.info(`Decided to create file ${file}`);
|
||||||
|
return this.create(
|
||||||
|
file,
|
||||||
new TextEncoder().encode(this.getContent())
|
new TextEncoder().encode(this.getContent())
|
||||||
),
|
);
|
||||||
() =>
|
},
|
||||||
this.client.settings.setSetting(
|
() => {
|
||||||
|
this.client.logger.info(
|
||||||
|
`Decided to change fetchChangesUpdateIntervalMs`
|
||||||
|
);
|
||||||
|
return this.client.settings.setSetting(
|
||||||
"fetchChangesUpdateIntervalMs",
|
"fetchChangesUpdateIntervalMs",
|
||||||
Math.random() * 1000
|
Math.random() * 1000
|
||||||
),
|
);
|
||||||
() => this.client.settings.setSetting("isSyncEnabled", false),
|
},
|
||||||
() => this.client.settings.setSetting("isSyncEnabled", true)
|
() => {
|
||||||
|
this.client.logger.info(`Decided to disable sync`);
|
||||||
|
return this.client.settings.setSetting("isSyncEnabled", false);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
this.client.logger.info(`Decided to enable sync`);
|
||||||
|
return this.client.settings.setSetting("isSyncEnabled", true);
|
||||||
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
let files = await this.listAllFiles();
|
let files = await this.listAllFiles();
|
||||||
|
|
||||||
if (files.length > 0) {
|
if (files.length > 0) {
|
||||||
options.push(
|
options.push(
|
||||||
() => this.rename(choose(files), this.getFileName()),
|
() => {
|
||||||
() =>
|
const file = choose(files);
|
||||||
this.atomicUpdateText(
|
|
||||||
choose(files),
|
const newName = this.getFileName();
|
||||||
|
this.client.logger.info(
|
||||||
|
`Decided to rename file ${file} to ${newName}`
|
||||||
|
);
|
||||||
|
return this.rename(file, newName);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
const file = choose(files);
|
||||||
|
|
||||||
|
this.client.logger.info(`Decided to update file ${file}`);
|
||||||
|
return this.atomicUpdateText(
|
||||||
|
file,
|
||||||
(old) => old + " " + this.getContent()
|
(old) => old + " " + this.getContent()
|
||||||
)
|
);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (this.doDeletes) {
|
||||||
|
options.push(() => this.delete(choose(files)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.pendingActions.push(choose(options)());
|
this.pendingActions.push(choose(options)());
|
||||||
|
|
@ -91,47 +122,68 @@ export class MockAgent extends MockClient {
|
||||||
await Promise.all(this.pendingActions);
|
await Promise.all(this.pendingActions);
|
||||||
await this.client.settings.setSetting("isSyncEnabled", true);
|
await this.client.settings.setSetting("isSyncEnabled", true);
|
||||||
await this.client.syncer.applyRemoteChangesLocally();
|
await this.client.syncer.applyRemoteChangesLocally();
|
||||||
|
await sleep(5000);
|
||||||
|
await this.client.syncer.waitForSyncQueue();
|
||||||
this.client.stop();
|
this.client.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public assertFileSystemIsConsistent(): void {
|
public assertFileSystemIsConsistent(): void {
|
||||||
const files = Object.keys(this.globalFiles);
|
const globalFiles = Object.keys(this.globalFiles);
|
||||||
const localFiles = Object.keys(this.files);
|
const localFiles = Object.keys(this.localFiles);
|
||||||
|
|
||||||
assert(
|
const missingInGlobal = localFiles.filter(
|
||||||
files.length === localFiles.length,
|
(file) => !(file in this.globalFiles)
|
||||||
`File count mismatch: ${files.length} != ${localFiles.length}`
|
);
|
||||||
|
const missingInLocal = globalFiles.filter(
|
||||||
|
(file) => !(file in this.localFiles)
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const file of files) {
|
assert(
|
||||||
assert(
|
missingInGlobal.length === 0,
|
||||||
file in this.globalFiles,
|
`Files missing in global files: ${missingInGlobal.join(", ")}`
|
||||||
`File ${file} missing in global files`
|
);
|
||||||
|
assert(
|
||||||
|
missingInLocal.length === 0,
|
||||||
|
`Files missing in local files: ${missingInLocal.join(", ")}`
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const file of globalFiles) {
|
||||||
|
const localContent = new TextDecoder().decode(
|
||||||
|
this.localFiles[file]
|
||||||
|
);
|
||||||
|
const globalContent = new TextDecoder().decode(
|
||||||
|
this.globalFiles[file]
|
||||||
);
|
);
|
||||||
assert(
|
assert(
|
||||||
new TextDecoder().decode(this.globalFiles[file]) ===
|
localContent === globalContent,
|
||||||
new TextDecoder().decode(this.files[file]),
|
`Content mismatch for file ${file}: ${localContent} <> ${globalContent}`
|
||||||
`File ${file} content mismatch`
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public assertAllContentIsPresentOnce(): void {
|
public assertAllContentIsPresentOnce(): void {
|
||||||
for (const content of this.writtenContents) {
|
for (const content of this.writtenContents) {
|
||||||
const found = Object.values(this.files).filter((file) => {
|
const found = Object.values(this.localFiles).filter((file) => {
|
||||||
return new TextDecoder().decode(file).includes(content);
|
return new TextDecoder().decode(file).includes(content);
|
||||||
});
|
});
|
||||||
|
|
||||||
assert(
|
if (this.doDeletes) {
|
||||||
found.length === 1,
|
assert(
|
||||||
`Content ${content} found in ${found.length} files`
|
found.length <= 1,
|
||||||
);
|
`Content ${content} found in ${found.length} files`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
assert(
|
||||||
|
found.length === 1,
|
||||||
|
`Content ${content} found in ${found.length} files`
|
||||||
|
);
|
||||||
|
|
||||||
const file = found[0];
|
const file = found[0];
|
||||||
assert(
|
assert(
|
||||||
new TextDecoder().decode(file).split(content).length === 2,
|
new TextDecoder().decode(file).split(content).length === 2,
|
||||||
`Content ${content} found more than once in a file`
|
`Content ${content} found more than once in a file`
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import {
|
||||||
import { assert } from "../utils/assert";
|
import { assert } from "../utils/assert";
|
||||||
|
|
||||||
export class MockClient implements FileSystemOperations {
|
export class MockClient implements FileSystemOperations {
|
||||||
protected readonly files: Record<string, Uint8Array> = {};
|
protected readonly localFiles: Record<string, Uint8Array> = {};
|
||||||
protected client!: SyncClient;
|
protected client!: SyncClient;
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
|
|
@ -37,31 +37,43 @@ export class MockClient implements FileSystemOperations {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async listAllFiles(): Promise<RelativePath[]> {
|
public async listAllFiles(): Promise<RelativePath[]> {
|
||||||
return Object.keys(this.files) as RelativePath[];
|
return Object.keys(this.localFiles) as RelativePath[];
|
||||||
}
|
}
|
||||||
|
|
||||||
public async read(path: RelativePath): Promise<Uint8Array> {
|
public async read(path: RelativePath): Promise<Uint8Array> {
|
||||||
return this.files[path];
|
if (!(path in this.localFiles)) {
|
||||||
|
throw new Error(`File ${path} does not exist`);
|
||||||
|
}
|
||||||
|
return this.localFiles[path];
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getFileSize(path: RelativePath): Promise<number> {
|
public async getFileSize(path: RelativePath): Promise<number> {
|
||||||
return this.files[path].length;
|
if (!(path in this.localFiles)) {
|
||||||
|
throw new Error(`File ${path} does not exist`);
|
||||||
|
}
|
||||||
|
return this.localFiles[path].length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getModificationTime(path: RelativePath): Promise<Date> {
|
public async getModificationTime(path: RelativePath): Promise<Date> {
|
||||||
|
if (!(path in this.localFiles)) {
|
||||||
|
throw new Error(`File ${path} does not exist`);
|
||||||
|
}
|
||||||
return new Date();
|
return new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async exists(path: RelativePath): Promise<boolean> {
|
public async exists(path: RelativePath): Promise<boolean> {
|
||||||
return path in this.files;
|
return path in this.localFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async create(
|
public async create(
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
newContent: Uint8Array
|
newContent: Uint8Array
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
if (path in this.localFiles) {
|
||||||
|
throw new Error(`File ${path} already exists`);
|
||||||
|
}
|
||||||
this.globalFiles[path] = newContent;
|
this.globalFiles[path] = newContent;
|
||||||
this.files[path] = newContent;
|
this.localFiles[path] = newContent;
|
||||||
this.client.syncer.syncLocallyCreatedFile(path, new Date());
|
this.client.syncer.syncLocallyCreatedFile(path, new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,55 +83,62 @@ export class MockClient implements FileSystemOperations {
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
updater: (currentContent: string) => string
|
updater: (currentContent: string) => string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const currentContent = new TextDecoder().decode(this.files[path]);
|
if (!(path in this.localFiles)) {
|
||||||
|
throw new Error(`File ${path} does not exist`);
|
||||||
|
}
|
||||||
|
const currentContent = new TextDecoder().decode(this.localFiles[path]);
|
||||||
const newContent = updater(currentContent);
|
const newContent = updater(currentContent);
|
||||||
const newContentUint8Array = new TextEncoder().encode(newContent);
|
const newContentUint8Array = new TextEncoder().encode(newContent);
|
||||||
this.globalFiles[path] = newContentUint8Array;
|
this.globalFiles[path] = newContentUint8Array;
|
||||||
this.files[path] = newContentUint8Array;
|
this.localFiles[path] = newContentUint8Array;
|
||||||
this.client.syncer.syncLocallyUpdatedFile({
|
|
||||||
|
void this.client.syncer.syncLocallyUpdatedFile({
|
||||||
relativePath: path,
|
relativePath: path,
|
||||||
updateTime: new Date()
|
updateTime: new Date()
|
||||||
});
|
});
|
||||||
|
|
||||||
return newContent;
|
return newContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
|
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
|
||||||
this.globalFiles[path] = content;
|
this.globalFiles[path] = content;
|
||||||
this.files[path] = content;
|
this.localFiles[path] = content;
|
||||||
this.client.syncer.syncLocallyUpdatedFile({
|
|
||||||
|
void this.client.syncer.syncLocallyUpdatedFile({
|
||||||
relativePath: path,
|
relativePath: path,
|
||||||
updateTime: new Date()
|
updateTime: new Date()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(path: RelativePath): Promise<void> {
|
public async delete(path: RelativePath): Promise<void> {
|
||||||
delete this.files[path];
|
delete this.localFiles[path];
|
||||||
if (path in this.globalFiles) {
|
if (path in this.globalFiles) {
|
||||||
delete this.globalFiles[path];
|
delete this.globalFiles[path];
|
||||||
}
|
}
|
||||||
this.client.syncer.syncLocallyDeletedFile(path);
|
|
||||||
|
void this.client.syncer.syncLocallyDeletedFile(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async rename(
|
public async rename(
|
||||||
oldPath: RelativePath,
|
oldPath: RelativePath,
|
||||||
newPath: RelativePath
|
newPath: RelativePath
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
this.files[newPath] = this.files[oldPath];
|
if (!(oldPath in this.localFiles)) {
|
||||||
delete this.files[oldPath];
|
throw new Error(`File ${oldPath} does not exist`);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.localFiles[newPath] = this.localFiles[oldPath];
|
||||||
|
delete this.localFiles[oldPath];
|
||||||
|
|
||||||
if (oldPath in this.globalFiles) {
|
if (oldPath in this.globalFiles) {
|
||||||
this.globalFiles[newPath] = this.files[oldPath];
|
this.globalFiles[newPath] = this.localFiles[oldPath];
|
||||||
delete this.globalFiles[oldPath];
|
delete this.globalFiles[oldPath];
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client.syncer.syncLocallyUpdatedFile({
|
void this.client.syncer.syncLocallyUpdatedFile({
|
||||||
oldPath,
|
oldPath,
|
||||||
relativePath: newPath,
|
relativePath: newPath,
|
||||||
updateTime: new Date()
|
updateTime: new Date()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public isFileEligibleForSync(path: RelativePath): boolean {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { v4 as uuidv4 } from "uuid";
|
||||||
|
|
||||||
const globalFiles: Record<string, Uint8Array> = {};
|
const globalFiles: Record<string, Uint8Array> = {};
|
||||||
const iterations = 100;
|
const iterations = 100;
|
||||||
|
const doDeletes = false;
|
||||||
|
|
||||||
async function runTest(): Promise<void> {
|
async function runTest(): Promise<void> {
|
||||||
console.info("Starting test");
|
console.info("Starting test");
|
||||||
|
|
@ -17,11 +18,41 @@ async function runTest(): Promise<void> {
|
||||||
};
|
};
|
||||||
|
|
||||||
const clients = [
|
const clients = [
|
||||||
new MockAgent(globalFiles, initialSettings, "agent-1", "#ff0000"),
|
new MockAgent(
|
||||||
new MockAgent(globalFiles, initialSettings, "agent-2", "#00ff00"),
|
globalFiles,
|
||||||
new MockAgent(globalFiles, initialSettings, "agent-3", "#0000ff"),
|
initialSettings,
|
||||||
new MockAgent(globalFiles, initialSettings, "agent-4", "#ffaa00"),
|
"agent-1",
|
||||||
new MockAgent(globalFiles, initialSettings, "agent-5", "#00ffaa")
|
"#ff0000",
|
||||||
|
doDeletes
|
||||||
|
),
|
||||||
|
new MockAgent(
|
||||||
|
globalFiles,
|
||||||
|
initialSettings,
|
||||||
|
"agent-2",
|
||||||
|
"#00ff00",
|
||||||
|
doDeletes
|
||||||
|
),
|
||||||
|
new MockAgent(
|
||||||
|
globalFiles,
|
||||||
|
initialSettings,
|
||||||
|
"agent-3",
|
||||||
|
"#0000ff",
|
||||||
|
doDeletes
|
||||||
|
),
|
||||||
|
new MockAgent(
|
||||||
|
globalFiles,
|
||||||
|
initialSettings,
|
||||||
|
"agent-4",
|
||||||
|
"#ffaa00",
|
||||||
|
doDeletes
|
||||||
|
),
|
||||||
|
new MockAgent(
|
||||||
|
globalFiles,
|
||||||
|
initialSettings,
|
||||||
|
"agent-5",
|
||||||
|
"#00ffaa",
|
||||||
|
doDeletes
|
||||||
|
)
|
||||||
];
|
];
|
||||||
|
|
||||||
await Promise.all(clients.map((client) => client.init()));
|
await Promise.all(clients.map((client) => client.init()));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue