2023-07-27 00:31:52 -05:00
|
|
|
/*
|
2024-02-13 09:59:27 -06:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 00:31:52 -05:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-07-08 05:34:19 -05:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import { jest } from '@jest/globals';
|
|
|
|
import { Test } from '@nestjs/testing';
|
2023-07-27 00:31:52 -05:00
|
|
|
import { Redis } from 'ioredis';
|
2023-07-08 05:34:19 -05:00
|
|
|
import { GlobalModule } from '@/GlobalModule.js';
|
|
|
|
import { FetchInstanceMetadataService } from '@/core/FetchInstanceMetadataService.js';
|
|
|
|
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import type { TestingModule } from '@nestjs/testing';
|
|
|
|
|
|
|
|
function mockRedis() {
|
2024-03-03 05:15:35 -06:00
|
|
|
const hash = {} as any;
|
|
|
|
const set = jest.fn((key: string, value) => {
|
2023-07-27 00:31:52 -05:00
|
|
|
const ret = hash[key];
|
|
|
|
hash[key] = value;
|
|
|
|
return ret;
|
|
|
|
});
|
2023-07-08 05:34:19 -05:00
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('FetchInstanceMetadataService', () => {
|
|
|
|
let app: TestingModule;
|
|
|
|
let fetchInstanceMetadataService: jest.Mocked<FetchInstanceMetadataService>;
|
|
|
|
let federatedInstanceService: jest.Mocked<FederatedInstanceService>;
|
|
|
|
let httpRequestService: jest.Mocked<HttpRequestService>;
|
2023-09-03 23:33:38 -05:00
|
|
|
let redisClient: jest.Mocked<Redis>;
|
2023-07-08 05:34:19 -05:00
|
|
|
|
2023-09-03 23:33:38 -05:00
|
|
|
beforeEach(async () => {
|
2023-07-08 05:34:19 -05:00
|
|
|
app = await Test
|
|
|
|
.createTestingModule({
|
|
|
|
imports: [
|
|
|
|
GlobalModule,
|
|
|
|
],
|
|
|
|
providers: [
|
|
|
|
FetchInstanceMetadataService,
|
|
|
|
LoggerService,
|
|
|
|
UtilityService,
|
|
|
|
IdService,
|
|
|
|
],
|
|
|
|
})
|
|
|
|
.useMocker((token) => {
|
|
|
|
if (token === HttpRequestService) {
|
2023-07-27 00:31:52 -05:00
|
|
|
return { getJson: jest.fn(), getHtml: jest.fn(), send: jest.fn() };
|
2023-07-08 05:34:19 -05:00
|
|
|
} else if (token === FederatedInstanceService) {
|
|
|
|
return { fetch: jest.fn() };
|
|
|
|
} else if (token === DI.redis) {
|
|
|
|
return mockRedis;
|
2024-01-08 02:43:52 -06:00
|
|
|
}
|
2024-03-03 17:45:47 -06:00
|
|
|
return null;
|
2024-01-08 02:43:52 -06:00
|
|
|
})
|
2023-07-08 05:34:19 -05:00
|
|
|
.compile();
|
|
|
|
|
|
|
|
app.enableShutdownHooks();
|
|
|
|
|
2024-03-03 05:15:35 -06:00
|
|
|
fetchInstanceMetadataService = app.get<FetchInstanceMetadataService>(FetchInstanceMetadataService) as jest.Mocked<FetchInstanceMetadataService>;
|
2023-07-08 05:34:19 -05:00
|
|
|
federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService) as jest.Mocked<FederatedInstanceService>;
|
2023-09-03 23:33:38 -05:00
|
|
|
redisClient = app.get<Redis>(DI.redis) as jest.Mocked<Redis>;
|
2023-07-08 05:34:19 -05:00
|
|
|
httpRequestService = app.get<HttpRequestService>(HttpRequestService) as jest.Mocked<HttpRequestService>;
|
|
|
|
});
|
|
|
|
|
2023-09-03 23:33:38 -05:00
|
|
|
afterEach(async () => {
|
2023-07-08 05:34:19 -05:00
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Lock and update', async () => {
|
|
|
|
redisClient.set = mockRedis();
|
|
|
|
const now = Date.now();
|
2024-03-05 10:44:11 -06:00
|
|
|
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date(now - 10 * 1000 * 60 * 60 * 24) } as any);
|
2023-07-08 05:34:19 -05:00
|
|
|
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
|
|
|
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
|
|
|
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
2024-03-03 17:45:47 -06:00
|
|
|
|
2024-03-03 05:15:35 -06:00
|
|
|
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
2024-03-05 10:44:11 -06:00
|
|
|
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(1);
|
2023-07-08 05:34:19 -05:00
|
|
|
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(unlockSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(httpRequestService.getJson).toHaveBeenCalled();
|
|
|
|
});
|
2023-09-03 23:33:38 -05:00
|
|
|
|
2024-03-05 10:44:32 -06:00
|
|
|
test('Don\'t lock and update if recently updated', async () => {
|
2023-07-08 05:34:19 -05:00
|
|
|
redisClient.set = mockRedis();
|
2024-03-05 10:44:11 -06:00
|
|
|
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date() } as any);
|
2023-07-08 05:34:19 -05:00
|
|
|
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
|
|
|
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
|
|
|
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
2024-03-03 17:45:47 -06:00
|
|
|
|
2024-03-03 05:15:35 -06:00
|
|
|
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
2023-07-08 05:34:19 -05:00
|
|
|
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(1);
|
2024-03-05 10:44:11 -06:00
|
|
|
expect(tryLockSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(unlockSpy).toHaveBeenCalledTimes(0);
|
2023-07-08 05:34:19 -05:00
|
|
|
expect(httpRequestService.getJson).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
2023-09-03 23:33:38 -05:00
|
|
|
|
2023-07-08 05:34:19 -05:00
|
|
|
test('Do nothing when lock not acquired', async () => {
|
|
|
|
redisClient.set = mockRedis();
|
2024-03-03 05:15:35 -06:00
|
|
|
const now = Date.now();
|
2024-03-05 10:44:11 -06:00
|
|
|
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date(now - 10 * 1000 * 60 * 60 * 24) } as any);
|
2023-07-08 05:34:19 -05:00
|
|
|
httpRequestService.getJson.mockImplementation(() => { throw Error(); });
|
2024-03-03 17:45:47 -06:00
|
|
|
await fetchInstanceMetadataService.tryLock('example.com');
|
2023-07-08 05:34:19 -05:00
|
|
|
const tryLockSpy = jest.spyOn(fetchInstanceMetadataService, 'tryLock');
|
|
|
|
const unlockSpy = jest.spyOn(fetchInstanceMetadataService, 'unlock');
|
2024-03-03 17:45:47 -06:00
|
|
|
|
2024-03-03 05:15:35 -06:00
|
|
|
await fetchInstanceMetadataService.fetchInstanceMetadata({ host: 'example.com' } as any);
|
2024-03-05 10:44:11 -06:00
|
|
|
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(1);
|
2024-03-03 17:45:47 -06:00
|
|
|
expect(tryLockSpy).toHaveBeenCalledTimes(1);
|
2023-07-08 05:34:19 -05:00
|
|
|
expect(unlockSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(httpRequestService.getJson).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
2024-03-03 17:45:47 -06:00
|
|
|
|
2024-03-05 10:44:11 -06:00
|
|
|
test('Do when forced', async () => {
|
2024-03-03 17:45:47 -06:00
|
|
|
redisClient.set = mockRedis();
|
|
|
|
const now = Date.now();
|
2024-03-05 10:44:11 -06:00
|
|
|
federatedInstanceService.fetch.mockResolvedValue({ infoUpdatedAt: new Date(now - 10 * 1000 * 60 * 60 * 24) } as any);
|
2024-03-03 17:45:47 -06:00
|
|
|
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);
|
2024-03-05 10:44:11 -06:00
|
|
|
expect(federatedInstanceService.fetch).toHaveBeenCalledTimes(0);
|
2024-03-03 17:45:47 -06:00
|
|
|
expect(tryLockSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(unlockSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(httpRequestService.getJson).toHaveBeenCalled();
|
|
|
|
});
|
2023-07-08 05:34:19 -05:00
|
|
|
});
|