fix test
This commit is contained in:
parent
0127f89298
commit
01b8d2fdb1
3 changed files with 15 additions and 15 deletions
|
@ -81,7 +81,7 @@ export class FetchInstanceMetadataService {
|
|||
// キャッシュ有効チェックはロック取得前に行う
|
||||
const _instance = await this.federatedInstanceService.fetch(host);
|
||||
const now = Date.now();
|
||||
if (_instance && _instance.infoUpdatedAt && (now - _instance.infoUpdatedAt.getTime() < REMOTE_SERVER_CACHE_TTL)) {
|
||||
if (_instance && _instance.infoUpdatedAt != null && (now - _instance.infoUpdatedAt.getTime() < REMOTE_SERVER_CACHE_TTL)) {
|
||||
this.logger.debug(`Skip because updated recently ${_instance.infoUpdatedAt.toJSON()}`);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -91,9 +91,10 @@ export class ApRequestService {
|
|||
@bindThis
|
||||
public async signedPost(user: { id: MiUser['id'] }, url: string, object: unknown, level: string, digest?: string, key?: PrivateKeyWithPem): Promise<void> {
|
||||
const body = typeof object === 'string' ? object : JSON.stringify(object);
|
||||
const keyFetched = await this.userKeypairService.getLocalUserPrivateKey(key ?? user.id, level);
|
||||
const req = await createSignedPost({
|
||||
level,
|
||||
key: await this.userKeypairService.getLocalUserPrivateKey(key ?? user.id, level),
|
||||
key: keyFetched,
|
||||
url,
|
||||
body,
|
||||
additionalHeaders: {
|
||||
|
@ -106,7 +107,7 @@ export class ApRequestService {
|
|||
version: 'draft',
|
||||
level,
|
||||
url,
|
||||
keyId: key.keyId,
|
||||
keyId: keyFetched.keyId,
|
||||
});
|
||||
|
||||
await this.httpRequestService.send(url, {
|
||||
|
|
|
@ -75,62 +75,61 @@ describe('FetchInstanceMetadataService', () => {
|
|||
test('Lock and update', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: { getTime: () => { return now - 10 * 1000 * 60 * 60 * 24; } } } as any);
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date(now - 10 * 1000 * 60 * 60 * 24) } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
||||
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(1);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
||||
expect(unlockSpy).toHaveBeenCalledTimes(1);
|
||||
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(1);
|
||||
expect(httpRequestService.getJson).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('Lock and don\'t update', async () => {
|
||||
test('Don\'t lock and update when recently updated', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: { getTime: () => now } } as any);
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date() } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
||||
expect(unlockSpy).toHaveBeenCalledTimes(1);
|
||||
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(1);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(0);
|
||||
expect(unlockSpy).toHaveBeenCalledTimes(0);
|
||||
expect(httpRequestService.getJson).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
test('Do nothing when lock not acquired', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: { getTime: () => now - 10 * 1000 * 60 * 60 * 24 } } as any);
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date(now - 10 * 1000 * 60 * 60 * 24) } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
await fetchInstanceMetadataService.tryLock('example.com');
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
||||
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(1);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
||||
expect(unlockSpy).toHaveBeenCalledTimes(0);
|
||||
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(0);
|
||||
expect(httpRequestService.getJson).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
test('Do when lock not acquired but forced', async () => {
|
||||
test('Do when forced', async () => {
|
||||
redisClient.set = mockRedis();
|
||||
const now = Date.now();
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: { getTime: () => now - 10 * 1000 * 60 * 60 * 24 } } as any);
|
||||
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date(now - 10 * 1000 * 60 * 60 * 24) } as any);
|
||||
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
||||
await fetchInstanceMetadataService.tryLock('example.com');
|
||||
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
||||
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
||||
|
||||
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any, true);
|
||||
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(0);
|
||||
expect(tryLockSpy).toHaveBeenCalledTimes(0);
|
||||
expect(unlockSpy).toHaveBeenCalledTimes(1);
|
||||
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(0);
|
||||
expect(httpRequestService.getJson).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue