test: replace late portfolio sleep with started and done barriers (WealthLedger/backend#63) (#101)
This commit was merged in pull request #101.
This commit is contained in:
@@ -3,6 +3,7 @@ import type { Server } from 'node:http';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import assert from 'node:assert/strict';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import request from 'supertest';
|
||||
|
||||
@@ -573,16 +574,30 @@ describe('local accounts and portfolio HTTP (SQLite)', () => {
|
||||
await put().expect(200);
|
||||
await seedMembership();
|
||||
const blocked = deferred<TbankPortfolio>();
|
||||
upstream.getPortfolio.mockReturnValueOnce(blocked.promise);
|
||||
await get('brokerage-accounts/account/portfolio').expect(200);
|
||||
const started = deferred<void>();
|
||||
upstream.getPortfolio.mockImplementationOnce(() => {
|
||||
started.resolve();
|
||||
return blocked.promise;
|
||||
});
|
||||
// Starts the real portfolio producer, the same path the hourly walk uses.
|
||||
const late = await portfolioService.request(
|
||||
'owner',
|
||||
(await prisma.tbankConnection.findUniqueOrThrow({ where: { userId: 'owner' } })).revision,
|
||||
'account',
|
||||
true,
|
||||
);
|
||||
assert(late);
|
||||
// The late upstream call must have begun before the connection is replaced.
|
||||
await started.promise;
|
||||
await put().expect(200);
|
||||
await seedMembership();
|
||||
blocked.resolve({ positions: [] });
|
||||
await delay(20);
|
||||
// Barrier: the fenced producer's completion, not a timer.
|
||||
await late.done;
|
||||
expect(await prisma.tbankPortfolio.count()).toBe(0);
|
||||
const replaced = await get(
|
||||
'brokerage-accounts/account/portfolio?sync=observe',
|
||||
).expect(200);
|
||||
expect((replaced.body as PortfolioResourceDto).data).toBeNull();
|
||||
expect(await prisma.tbankPortfolio.count()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user