Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ test: SSE 최초 접속 테스트 코드 작성 #273

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/cookie-parser": "^1.4.7",
"@types/eventsource": "^1.1.15",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.17.13",
Expand All @@ -67,6 +68,7 @@
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"eventsource": "^2.0.2",
"ioredis-mock": "^8.9.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
Expand Down
102 changes: 102 additions & 0 deletions server/test/feed/trend.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { INestApplication } from '@nestjs/common';
import { EventEmitter } from 'stream';
import { RedisService } from '../../src/common/redis/redis.service';
import { redisKeys } from '../../src/common/redis/redis.constant';
import { RssAcceptFixture } from '../fixture/rssAccept.fixture';
import { FeedRepository } from '../../src/feed/feed.repository';
import { RssAcceptRepository } from '../../src/rss/rss.repository';
import { Feed } from '../../src/feed/feed.entity';
import { FeedFixture } from '../fixture/feed.fixture';
import * as EventSource from 'eventsource';

describe('SSE /api/trend/sse E2E Test', () => {
let app: INestApplication;
let feedRepository: FeedRepository;

beforeAll(async () => {
app = global.testApp;
feedRepository = app.get(FeedRepository);
const rssAcceptRepository = app.get(RssAcceptRepository);
const redisService = app.get(RedisService);
const [blog] = await Promise.all([
rssAcceptRepository.save(RssAcceptFixture.createRssAcceptFixture()),
redisService.redisClient.rpush(redisKeys.FEED_ORIGIN_TREND_KEY, 1, 2),
]);
const feeds: Feed[] = [];
for (let i = 1; i <= 2; i++) {
feeds.push(FeedFixture.createFeedFixture(blog, {}, i));
}
await Promise.all([feedRepository.save(feeds), app.listen(7000)]);
});

it('최초 연결이 되면 트랜드 데이터를 최대 4개 받을 수 있다.', async () => {
// given
const es = new EventSource('http://localhost:7000/api/feed/trend/sse');
const timeout = new Promise((_, reject) =>
setTimeout(
() => reject(new Error('Timeout occurred before receiving data')),
1000,
),
);
const eventResult = new Promise((resolve, reject) => {
es.onmessage = (event) => {
try {
const response = JSON.parse(event.data);
const data = response.data;
const idList = data.map((item) => item.id);
es.close();
resolve(idList);
} catch (error) {
es.close();
reject(error);
}
};
es.onerror = (error) => {
es.close();
reject(error);
};
});

// when
const idList = await Promise.race([eventResult, timeout]);

// then
expect(idList).toStrictEqual([1, 2]);
});

it('서버로부터 데이터를 받을 때, 게시글이 지워진 상황이라면 게시글을 받지 않는다.', async () => {
// given
await feedRepository.delete({ id: 2 });
const es = new EventSource('http://localhost:7000/api/feed/trend/sse');
const timeout = new Promise((_, reject) =>
setTimeout(
() => reject(new Error('Timeout occurred before receiving data')),
1000,
),
);
const eventResult = new Promise((resolve, reject) => {
es.onmessage = (event) => {
try {
const response = JSON.parse(event.data);
const data = response.data;
const idList = data.map((item) => item.id);
es.close();
resolve(idList);
} catch (error) {
es.close();
reject(error);
}
};
es.onerror = (error) => {
es.close();
reject(error);
};
});

// when
const idList = await Promise.race([eventResult, timeout]);

// then
expect(idList).toStrictEqual([1]);
});
});
Loading