Fixing issue with redis updates in tests.

This commit is contained in:
mike12345567 2021-05-24 17:05:46 +01:00
parent 4268ea8eb0
commit 8200f2a4e8
3 changed files with 30 additions and 14 deletions

View File

@ -8,16 +8,16 @@ const STARTUP_TIMEOUT_MS = 5000
const CLUSTERED = false const CLUSTERED = false
// for testing just generate the client once // for testing just generate the client once
let CONNECTED = false let CLOSED = false
let CLIENT = env.isTest() ? new Redis(getRedisOptions()) : null let CLIENT = env.isTest() ? new Redis(getRedisOptions()) : null
// if in test always connected
let CONNECTED = !!env.isTest()
/** function connectionError(timeout, err) {
* Inits the system, will error if unable to connect to redis cluster (may take up to 10 seconds) otherwise // manually shut down, ignore errors
* will return the ioredis client which will be ready to use. if (CLOSED) {
*/ return
function init() { }
let timeout
function errorOccurred(err) {
// always clear this on error // always clear this on error
clearTimeout(timeout) clearTimeout(timeout)
CONNECTED = false CONNECTED = false
@ -25,7 +25,15 @@ function init() {
setTimeout(() => { setTimeout(() => {
init() init()
}, RETRY_PERIOD_MS) }, RETRY_PERIOD_MS)
} }
/**
* Inits the system, will error if unable to connect to redis cluster (may take up to 10 seconds) otherwise
* will return the ioredis client which will be ready to use.
*/
function init() {
let timeout
CLOSED = false
// testing uses a single in memory client // testing uses a single in memory client
if (env.isTest() || (CLIENT && CONNECTED)) { if (env.isTest() || (CLIENT && CONNECTED)) {
return return
@ -33,7 +41,7 @@ function init() {
// start the timer - only allowed 5 seconds to connect // start the timer - only allowed 5 seconds to connect
timeout = setTimeout(() => { timeout = setTimeout(() => {
if (!CONNECTED) { if (!CONNECTED) {
errorOccurred() connectionError(timeout)
} }
}, STARTUP_TIMEOUT_MS) }, STARTUP_TIMEOUT_MS)
@ -49,10 +57,10 @@ function init() {
} }
// attach handlers // attach handlers
CLIENT.on("end", err => { CLIENT.on("end", err => {
errorOccurred(err) connectionError(timeout, err)
}) })
CLIENT.on("error", err => { CLIENT.on("error", err => {
errorOccurred(err) connectionError(timeout, err)
}) })
CLIENT.on("connect", () => { CLIENT.on("connect", () => {
clearTimeout(timeout) clearTimeout(timeout)
@ -122,12 +130,14 @@ class RedisWrapper {
} }
async init() { async init() {
CLOSED = false
init() init()
await waitForConnection() await waitForConnection()
return this return this
} }
async finish() { async finish() {
CLOSED = true
CLIENT.disconnect() CLIENT.disconnect()
} }

View File

@ -73,10 +73,11 @@ if (env.isProd()) {
const server = http.createServer(app.callback()) const server = http.createServer(app.callback())
destroyable(server) destroyable(server)
server.on("close", () => { server.on("close", async () => {
if (env.NODE_ENV !== "jest") { if (env.NODE_ENV !== "jest") {
console.log("Server Closed") console.log("Server Closed")
} }
await redis.shutdown()
}) })
module.exports = server.listen(env.PORT || 0, async () => { module.exports = server.listen(env.PORT || 0, async () => {

View File

@ -11,6 +11,11 @@ exports.init = async () => {
debounceClient = await new Client(utils.Databases.DEBOUNCE).init() debounceClient = await new Client(utils.Databases.DEBOUNCE).init()
} }
exports.shutdown = async () => {
await devAppClient.finish()
await debounceClient.finish()
}
exports.doesUserHaveLock = async (devAppId, user) => { exports.doesUserHaveLock = async (devAppId, user) => {
const value = await devAppClient.get(devAppId) const value = await devAppClient.get(devAppId)
if (!value) { if (!value) {