Skip to content

Commit

Permalink
test: add Running Queue test
Browse files Browse the repository at this point in the history
  • Loading branch information
junha-ahn committed Oct 18, 2023
1 parent ca8277c commit d1153ae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
12 changes: 9 additions & 3 deletions src/api/routes/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,22 @@ module.exports = (app) => {
const { eventId, userId } = req.params

const ticketStoreService = new TicketStoreService(redis)
const offset = await ticketStoreService.getOffsetFromWaiting(
let isWaiting = true
let offset = await ticketStoreService.getOffsetFromWaiting(
eventId,
userId,
)
if (!offset) return CustomResponse(404, `Ticket Not Found!`)
if (offset == null) {
isWaiting = false
offset = await ticketStoreService.getOffsetFromRunning(eventId, userId)
}

if (offset == null) return CustomResponse(404, `Ticket Not Found!`)

return CustomResponse(200, `Success!`, {
eventId,
userId,
isWaiting: true,
isWaiting,
offset,
})
}),
Expand Down
14 changes: 4 additions & 10 deletions src/services/ticketStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ module.exports = class TicketStore {
return tickets
}
async _getOffset(key, value) {
return await this.redis.zRank(key, value)
return this.redis.zRank(key, value)
}
async _length(key) {
return await this.redis.zCard(key)
return this.redis.zCard(key)
}

async pushIntoWaiting(eventId, userId) {
Expand Down Expand Up @@ -72,16 +72,10 @@ module.exports = class TicketStore {
}

async getOffsetFromWaiting(eventId, userId) {
return await this.redis.zRank(
this.generateWaitingKey(eventId),
userId.toString(),
)
return this._getOffset(this.generateWaitingKey(eventId), userId.toString())
}
async getOffsetFromRunning(eventId, userId) {
return await this.redis.zRank(
this.generateRunningKey(eventId),
userId.toString(),
)
return this._getOffset(this.generateRunningKey(eventId), userId.toString())
}
async getLengthOfWaiting(eventId) {
return await this._length(this.generateWaitingKey(eventId))
Expand Down
41 changes: 39 additions & 2 deletions test/integrationTest/ticket.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ const { GenericContainer } = require('testcontainers')
const expect = chai.expect
chai.use(chaiHttp)

describe('Health', () => {
describe('Ticket', () => {
let server = null
let redis = null
let ticketStoreService = null

beforeAll(async () => {
container = await new GenericContainer('redis')
.withExposedPorts(6379)
Expand All @@ -17,8 +20,22 @@ describe('Health', () => {
}

server = require('../../src/api')
redis = $require('loaders/redis')
const TicketStoreService = $require('services/ticketStore')
ticketStoreService = new TicketStoreService(redis)
})

beforeEach(async () => {
await redis.flushAll()
})

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

describe('POST /ticket 은', () => {
describe('성공시', () => {
it('201을 리턴한다', async () => {
Expand Down Expand Up @@ -46,7 +63,7 @@ describe('Health', () => {
eventId: 1,
userId: i,
})
expect(res.body.data.offset).to.deep.equal(i)
expect(res.body.data.offset).to.deep.equal(i - 1)
}
})
})
Expand All @@ -64,6 +81,7 @@ describe('Health', () => {
expect(res).to.have.status(200)
expect(res.body.status).to.deep.equal(true)
})

it('{eventId, userId, isWaiting, timestamp, offset}을 반환한다', async () => {
await chai.request(server).post('/ticket').send({
eventId: 1,
Expand All @@ -77,6 +95,25 @@ describe('Health', () => {
expect(res.body.data).to.have.property('isWaiting')
expect(res.body.data).to.have.property('offset')
})

it('Running Queue Ticket은 IsWaiting = True를 반환한다.', async () => {
for (let i = 1; i < 10; i++) {
await chai.request(server).post('/ticket').send({
eventId: 1,
userId: i,
})
}

await queueMovingJob(1, 5)

const res1 = await chai.request(server).get(`/ticket/1/1`)
expect(res1.body.data.isWaiting).to.deep.equal(false)
expect(res1.body.data.offset).to.deep.equal(0)

const res2 = await chai.request(server).get(`/ticket/1/6`)
expect(res2.body.data.isWaiting).to.deep.equal(true)
expect(res2.body.data.offset).to.deep.equal(0)
})
})
describe('실패시', () => {
it('대기열 티켓이 없을 경우 404 Not Found를 리턴한다', async () => {
Expand Down

0 comments on commit d1153ae

Please sign in to comment.