Skip to content

Commit 35e6d5d

Browse files
committed
test: fix test for looking easy
1 parent fde7cb7 commit 35e6d5d

File tree

2 files changed

+52
-53
lines changed

2 files changed

+52
-53
lines changed

.prettierrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"tabWidth": 2,
33
"semi": false,
44
"singleQuote": true,
5-
"trailingComma": "all"
5+
"trailingComma": "es5",
6+
"printWidth": 120
67
}

test/integrationTest/job/jobService.spec.js

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ describe('Ticket', () => {
1111
let ticketStoreService = null
1212
let jobService = null
1313

14+
const testEventId = 1
15+
16+
function addUserIntoQueue(key, userId, timestamp) {
17+
return redis.zAdd(key, [{ value: userId.toString(), score: timestamp }])
18+
}
19+
1420
beforeAll(async () => {
15-
container = await new GenericContainer('redis')
16-
.withExposedPorts(6379)
17-
.start()
21+
container = await new GenericContainer('redis').withExposedPorts(6379).start()
1822
process.env = {
1923
NODE_ENV: 'test',
2024
REDIS_HOST: container.getHost(),
@@ -45,84 +49,78 @@ describe('Ticket', () => {
4549
it('should return [{eventId, timestamp}] when no event in queue', async () => {
4650
const { timestamp } = await ticketStoreService.updateEventInList(1)
4751
const result = await jobService.getEventList()
48-
expect(result).to.deep.equal([{ eventId: 1, timestamp }])
52+
expect(result).to.deep.equal([{ eventId: testEventId, timestamp }])
4953
})
5054
})
5155

5256
describe('Job.moveEventInToRunning', () => {
5357
it('should move event from waiting to running', async () => {
54-
const eventId = 1
55-
await ticketStoreService.pushIntoWaiting(eventId, 1)
56-
await ticketStoreService.pushIntoWaiting(eventId, 2)
57-
await ticketStoreService.pushIntoWaiting(eventId, 3)
58+
await ticketStoreService.pushIntoWaiting(testEventId, 1)
59+
await ticketStoreService.pushIntoWaiting(testEventId, 2)
60+
await ticketStoreService.pushIntoWaiting(testEventId, 3)
5861

59-
await jobService.moveEventInToRunning(eventId)
62+
// MOVE_PER_INTEVAL = 2
63+
await jobService.moveEventInToRunning(testEventId)
6064

61-
expect(await ticketStoreService.getLengthOfWaiting(eventId)).to.equal(1)
62-
expect(await ticketStoreService.getLengthOfRunning(eventId)).to.equal(2)
65+
expect(await ticketStoreService.getLengthOfWaiting(testEventId)).to.equal(1)
66+
expect(await ticketStoreService.getLengthOfRunning(testEventId)).to.equal(2)
6367
})
6468
})
6569

6670
describe('Job.removeExpiredTicket', () => {
6771
it('should remove expired ticket', async () => {
68-
const eventId = 100
69-
70-
await redis.zAdd(ticketStoreService.getWaitingKeyByEventId(eventId), [
71-
{ value: `1`, score: new Date().valueOf() - ONE_MINUTE * 4 },
72-
{ value: `2`, score: new Date().valueOf() - ONE_MINUTE * 4 },
73-
{ value: `3`, score: new Date().valueOf() - ONE_MINUTE * 1 },
74-
])
75-
await redis.zAdd(ticketStoreService.getRunningKeyByEventId(eventId), [
76-
{ value: `4`, score: new Date().valueOf() - ONE_MINUTE * 4 },
77-
{ value: `5`, score: new Date().valueOf() - ONE_MINUTE * 4 },
78-
])
79-
80-
await jobService.removeExpiredTicket(eventId)
81-
expect(await ticketStoreService.getLengthOfWaiting(eventId)).to.equal(1)
82-
expect(await ticketStoreService.getLengthOfRunning(eventId)).to.equal(0)
72+
const NOW = new Date().valueOf()
73+
const WAITING = ticketStoreService.getWaitingKeyByEventId(testEventId)
74+
await addUserIntoQueue(WAITING, 1, NOW - ONE_MINUTE * 4)
75+
await addUserIntoQueue(WAITING, 2, NOW - ONE_MINUTE * 4)
76+
await addUserIntoQueue(WAITING, 3, NOW)
77+
78+
const RUNNING = ticketStoreService.getRunningKeyByEventId(testEventId)
79+
await addUserIntoQueue(RUNNING, 4, NOW - ONE_MINUTE * 4)
80+
await addUserIntoQueue(RUNNING, 5, NOW - ONE_MINUTE * 4)
81+
82+
await jobService.removeExpiredTicket(testEventId)
83+
expect(await ticketStoreService.getLengthOfWaiting(testEventId)).to.equal(1)
84+
expect(await ticketStoreService.getLengthOfRunning(testEventId)).to.equal(0)
8385
})
8486
})
8587

8688
describe('Job.removeExpiredEvent', () => {
87-
const eventId = 1001
8889
async function setup1hoursAgo() {
89-
await ticketStoreService.updateEventInList(eventId)
90-
const ONE_HOUR_AGO = new Date().valueOf() - ONE_MINUTE * 60
91-
92-
await redis.zAdd(ticketStoreService.getEventListKey(), [
93-
{ value: eventId.toString(), score: ONE_HOUR_AGO },
94-
])
95-
await redis.zAdd(ticketStoreService.getWaitingKeyByEventId(eventId), [
96-
{ value: `1`, score: ONE_HOUR_AGO },
97-
{ value: `2`, score: ONE_HOUR_AGO },
98-
{ value: `3`, score: new Date().valueOf() - ONE_MINUTE * 1 },
99-
])
100-
await redis.zAdd(ticketStoreService.getRunningKeyByEventId(eventId), [
101-
{ value: `4`, score: ONE_HOUR_AGO },
102-
{ value: `5`, score: ONE_HOUR_AGO },
103-
])
90+
await ticketStoreService.updateEventInList(testEventId)
91+
const NOW = new Date().valueOf()
92+
const ONE_HOUR_AGO = NOW - ONE_MINUTE * 60
93+
94+
await redis.zAdd(ticketStoreService.getEventListKey(), [{ value: testEventId.toString(), score: ONE_HOUR_AGO }])
95+
96+
const WAITING = ticketStoreService.getWaitingKeyByEventId(testEventId)
97+
await addUserIntoQueue(WAITING, 1, ONE_HOUR_AGO)
98+
await addUserIntoQueue(WAITING, 2, ONE_HOUR_AGO)
99+
await addUserIntoQueue(WAITING, 3, ONE_HOUR_AGO)
100+
101+
const RUNNING = ticketStoreService.getRunningKeyByEventId(testEventId)
102+
await addUserIntoQueue(RUNNING, 4, ONE_HOUR_AGO)
103+
await addUserIntoQueue(RUNNING, 5, ONE_HOUR_AGO)
104104
}
105+
105106
it('should remove expired queue', async () => {
106107
await setup1hoursAgo()
107-
expect(await ticketStoreService.getOffsetFromEventList(eventId)).to.equal(
108-
0,
109-
)
108+
expect(await ticketStoreService.getOffsetFromEventList(testEventId)).to.equal(0)
110109

111110
await jobService.removeExpiredEvent()
112-
expect(await ticketStoreService.getOffsetFromEventList(eventId)).to.equal(
113-
null,
114-
)
111+
expect(await ticketStoreService.getOffsetFromEventList(testEventId)).to.equal(null)
115112
})
113+
116114
it('should remove all waiting/running items when remove expired queue', async () => {
117115
await setup1hoursAgo()
118116

119-
expect(await ticketStoreService.getLengthOfWaiting(eventId)).to.equal(3)
120-
expect(await ticketStoreService.getLengthOfRunning(eventId)).to.equal(2)
117+
expect(await ticketStoreService.getLengthOfWaiting(testEventId)).to.equal(3)
118+
expect(await ticketStoreService.getLengthOfRunning(testEventId)).to.equal(2)
121119

122120
await jobService.removeExpiredEvent()
123121

124-
expect(await ticketStoreService.getLengthOfWaiting(eventId)).to.equal(0)
125-
expect(await ticketStoreService.getLengthOfRunning(eventId)).to.equal(0)
122+
expect(await ticketStoreService.getLengthOfWaiting(testEventId)).to.equal(0)
123+
expect(await ticketStoreService.getLengthOfRunning(testEventId)).to.equal(0)
126124
})
127125
})
128126
})

0 commit comments

Comments
 (0)