@@ -11,10 +11,14 @@ describe('Ticket', () => {
11
11
let ticketStoreService = null
12
12
let jobService = null
13
13
14
+ const testEventId = 1
15
+
16
+ function addUserIntoQueue ( key , userId , timestamp ) {
17
+ return redis . zAdd ( key , [ { value : userId . toString ( ) , score : timestamp } ] )
18
+ }
19
+
14
20
beforeAll ( async ( ) => {
15
- container = await new GenericContainer ( 'redis' )
16
- . withExposedPorts ( 6379 )
17
- . start ( )
21
+ container = await new GenericContainer ( 'redis' ) . withExposedPorts ( 6379 ) . start ( )
18
22
process . env = {
19
23
NODE_ENV : 'test' ,
20
24
REDIS_HOST : container . getHost ( ) ,
@@ -45,84 +49,78 @@ describe('Ticket', () => {
45
49
it ( 'should return [{eventId, timestamp}] when no event in queue' , async ( ) => {
46
50
const { timestamp } = await ticketStoreService . updateEventInList ( 1 )
47
51
const result = await jobService . getEventList ( )
48
- expect ( result ) . to . deep . equal ( [ { eventId : 1 , timestamp } ] )
52
+ expect ( result ) . to . deep . equal ( [ { eventId : testEventId , timestamp } ] )
49
53
} )
50
54
} )
51
55
52
56
describe ( 'Job.moveEventInToRunning' , ( ) => {
53
57
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 )
58
61
59
- await jobService . moveEventInToRunning ( eventId )
62
+ // MOVE_PER_INTEVAL = 2
63
+ await jobService . moveEventInToRunning ( testEventId )
60
64
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 )
63
67
} )
64
68
} )
65
69
66
70
describe ( 'Job.removeExpiredTicket' , ( ) => {
67
71
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 )
83
85
} )
84
86
} )
85
87
86
88
describe ( 'Job.removeExpiredEvent' , ( ) => {
87
- const eventId = 1001
88
89
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 )
104
104
}
105
+
105
106
it ( 'should remove expired queue' , async ( ) => {
106
107
await setup1hoursAgo ( )
107
- expect ( await ticketStoreService . getOffsetFromEventList ( eventId ) ) . to . equal (
108
- 0 ,
109
- )
108
+ expect ( await ticketStoreService . getOffsetFromEventList ( testEventId ) ) . to . equal ( 0 )
110
109
111
110
await jobService . removeExpiredEvent ( )
112
- expect ( await ticketStoreService . getOffsetFromEventList ( eventId ) ) . to . equal (
113
- null ,
114
- )
111
+ expect ( await ticketStoreService . getOffsetFromEventList ( testEventId ) ) . to . equal ( null )
115
112
} )
113
+
116
114
it ( 'should remove all waiting/running items when remove expired queue' , async ( ) => {
117
115
await setup1hoursAgo ( )
118
116
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 )
121
119
122
120
await jobService . removeExpiredEvent ( )
123
121
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 )
126
124
} )
127
125
} )
128
126
} )
0 commit comments