Fixing issue with redis updates in tests.

This commit is contained in:
mike12345567 2021-05-24 17:05:46 +01:00
parent 55aba6b824
commit ab4e880807
3 changed files with 30 additions and 14 deletions

View File

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

View File

@ -73,10 +73,11 @@ if (env.isProd()) {
const server = http.createServer(app.callback())
destroyable(server)
server.on("close", () => {
server.on("close", async () => {
if (env.NODE_ENV !== "jest") {
console.log("Server Closed")
}
await redis.shutdown()
})
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()
}
exports.shutdown = async () => {
await devAppClient.finish()
await debounceClient.finish()
}
exports.doesUserHaveLock = async (devAppId, user) => {
const value = await devAppClient.get(devAppId)
if (!value) {