Skip to content

Commit

Permalink
fix: rename variable
Browse files Browse the repository at this point in the history
  • Loading branch information
junha-ahn committed Oct 24, 2023
1 parent 35e6d5d commit 4d42535
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 67 deletions.
41 changes: 8 additions & 33 deletions src/services/ticketStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ module.exports = class TicketStore {
userId,
}
}
async shiftFromWaiting(eventId, userId) {
const results = await this._shift(
this.getWaitingKeyByEventId(eventId),
userId.toString(),
)
async shiftFromWaiting(eventId, moveCount) {
const results = await this._shift(this.getWaitingKeyByEventId(eventId), moveCount)
return results.map((e) => ({
timestamp: e.score,
eventId: Number(e.value),
Expand All @@ -90,16 +87,10 @@ module.exports = class TicketStore {
return this._getOffset(this.getEventListKey(), eventId.toString())
}
async getOffsetFromWaiting(eventId, userId) {
return this._getOffset(
this.getWaitingKeyByEventId(eventId),
userId.toString(),
)
return this._getOffset(this.getWaitingKeyByEventId(eventId), userId.toString())
}
async getOffsetFromRunning(eventId, userId) {
return this._getOffset(
this.getRunningKeyByEventId(eventId),
userId.toString(),
)
return this._getOffset(this.getRunningKeyByEventId(eventId), userId.toString())
}
async getLengthOfWaiting(eventId) {
return await this._length(this.getWaitingKeyByEventId(eventId))
Expand All @@ -109,18 +100,10 @@ module.exports = class TicketStore {
}

async removeWaitingByTimestamp(eventId, minTimestamp, maxTimestamp) {
return this._removeByTimestamp(
this.getWaitingKeyByEventId(eventId),
minTimestamp,
maxTimestamp,
)
return this._removeByTimestamp(this.getWaitingKeyByEventId(eventId), minTimestamp, maxTimestamp)
}
async removeRunningByTimestamp(eventId, minTimestamp, maxTimestamp) {
return this._removeByTimestamp(
this.getRunningKeyByEventId(eventId),
minTimestamp,
maxTimestamp,
)
return this._removeByTimestamp(this.getRunningKeyByEventId(eventId), minTimestamp, maxTimestamp)
}
async removeAllOfWaiting(eventId) {
return this.redis.del(this.getWaitingKeyByEventId(eventId))
Expand All @@ -130,18 +113,10 @@ module.exports = class TicketStore {
}

async removeEventIdByTimestamp(minTimestamp, maxTimestamp) {
return this._removeByTimestamp(
this.getEventListKey(),
minTimestamp,
maxTimestamp,
)
return this._removeByTimestamp(this.getEventListKey(), minTimestamp, maxTimestamp)
}
async getEventIdByTimestamp(minTimestamp, maxTimestamp) {
const results = await this._getByTimestamp(
this.getEventListKey(),
minTimestamp,
maxTimestamp,
)
const results = await this._getByTimestamp(this.getEventListKey(), minTimestamp, maxTimestamp)
return results.map((e) => Number(e))
}
}
42 changes: 8 additions & 34 deletions test/integrationTest/api/ticket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,8 @@ describe('Ticket', () => {
const testEventId = 1
const testUserId = 1

async function queueMovingJob(eventId, count) {
const tickets = await ticketStoreService.shiftFromWaiting(eventId, count)
for (const { value, score } of tickets) {
await ticketStoreService.pushIntoRunning(eventId, value, score)
}
}

beforeAll(async () => {
container = await new GenericContainer('redis')
.withExposedPorts(6379)
.start()
container = await new GenericContainer('redis').withExposedPorts(6379).start()
process.env = {
NODE_ENV: 'test',
REDIS_HOST: container.getHost(),
Expand All @@ -51,26 +42,18 @@ describe('Ticket', () => {
eventId: testEventId,
userId: testUserId,
})
const res = await redis.zRank(
ticketStoreService.getEventListKey(),
testEventId.toString(),
)
const res = await redis.zRank(ticketStoreService.getEventListKey(), testEventId.toString())
expect(res).to.deep.equal(testEventId - 1)
}
})
it('전체 Queue를 관리하는 Sorted Set에 항상 최신값이 갱신되어야 한다', async () => {
const isRange = (score) =>
score <= new Date().valueOf() + 100 &&
score >= new Date().valueOf() - 100
const isRange = (score) => score <= new Date().valueOf() + 100 && score >= new Date().valueOf() - 100

await chai.request(server).post('/ticket').send({
eventId: 1,
userId: 1,
})
const score1 = await redis.zScore(
ticketStoreService.getEventListKey(),
'1',
)
const score1 = await redis.zScore(ticketStoreService.getEventListKey(), '1')

expect(isRange(score1)).to.deep.equal(true)
await sleep(2000)
Expand All @@ -80,10 +63,7 @@ describe('Ticket', () => {
eventId: 1,
userId: 1,
})
const score2 = await redis.zScore(
ticketStoreService.getEventListKey(),
'1',
)
const score2 = await redis.zScore(ticketStoreService.getEventListKey(), '1')
expect(isRange(score2)).to.deep.equal(true)
})

Expand Down Expand Up @@ -126,9 +106,7 @@ describe('Ticket', () => {
userId: testUserId,
})

const res = await chai
.request(server)
.get(`/ticket/${testEventId}/${testUserId}`)
const res = await chai.request(server).get(`/ticket/${testEventId}/${testUserId}`)
expect(res).to.have.status(200)
expect(res.body.status).to.deep.equal(true)
})
Expand All @@ -139,9 +117,7 @@ describe('Ticket', () => {
userId: testUserId,
})

const res = await chai
.request(server)
.get(`/ticket/${testEventId}/${testUserId}`)
const res = await chai.request(server).get(`/ticket/${testEventId}/${testUserId}`)
expect(res.body.data).to.be.an('object')
expect(res.body.data).to.have.property('eventId')
expect(res.body.data).to.have.property('userId')
Expand All @@ -163,9 +139,7 @@ describe('Ticket', () => {
})
describe('실패시', () => {
it('대기열 티켓이 없을 경우 404 Not Found를 리턴한다', async () => {
const res = await chai
.request(server)
.get(`/ticket/${testEventId}/${testUserId}`)
const res = await chai.request(server).get(`/ticket/${testEventId}/${testUserId}`)
expect(res).to.have.status(404)
expect(res.body.status).to.deep.equal(false)
})
Expand Down

0 comments on commit 4d42535

Please sign in to comment.