feat: ManualClock 및 StepClock 클래스 추가와 관련 테스트 케이스 작성
feat: vitest를 devDependencies에 추가하고 테스트 스크립트 수정 docs: 테스트에서 제어 가능한 시계 사용에 대한 내용 추가
This commit is contained in:
@@ -13,3 +13,64 @@ export class SystemClock implements Clock {
|
||||
await new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
|
||||
export class ManualClock implements Clock {
|
||||
// 테스트에서 시간을 직접 이동시키는 수동 시계.
|
||||
private currentMs: number;
|
||||
|
||||
constructor(initialMs = 0) {
|
||||
this.currentMs = initialMs;
|
||||
}
|
||||
|
||||
nowMs(): number {
|
||||
return this.currentMs;
|
||||
}
|
||||
|
||||
async sleepMs(ms: number): Promise<void> {
|
||||
if (ms <= 0) {
|
||||
return;
|
||||
}
|
||||
this.currentMs += ms;
|
||||
}
|
||||
|
||||
advanceMs(ms: number): void {
|
||||
if (ms <= 0) {
|
||||
return;
|
||||
}
|
||||
this.currentMs += ms;
|
||||
}
|
||||
|
||||
setMs(ms: number): void {
|
||||
this.currentMs = ms;
|
||||
}
|
||||
}
|
||||
|
||||
export class StepClock implements Clock {
|
||||
// 호출마다 일정 간격씩 시간이 진행되는 시계.
|
||||
private currentMs: number;
|
||||
private readonly stepMs: number;
|
||||
|
||||
constructor(stepMs: number, initialMs = 0) {
|
||||
if (stepMs <= 0) {
|
||||
throw new Error('stepMs must be positive');
|
||||
}
|
||||
this.stepMs = stepMs;
|
||||
this.currentMs = initialMs;
|
||||
}
|
||||
|
||||
nowMs(): number {
|
||||
this.currentMs += this.stepMs;
|
||||
return this.currentMs;
|
||||
}
|
||||
|
||||
async sleepMs(ms: number): Promise<void> {
|
||||
if (ms <= 0) {
|
||||
return;
|
||||
}
|
||||
this.currentMs += ms;
|
||||
}
|
||||
|
||||
setMs(ms: number): void {
|
||||
this.currentMs = ms;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user