1
  • 测试在本地环境中运行(并且 Redis 工作)非常好
  • 即使部署到生产环境,Redis 也能正常工作
  • 然而,当在 Heroku 的 CI 测试环境中运行时,Redis 连接会立即关闭

使用的技术:

  • Node.js、Express、Mocha、Chai、Supertest
  • Heroku, Heroku CI, (Heroku) Redis

我正在通过管道功能运行测试。然后我得到以下错误输出:

-----> Running test command `make test-ci`...
NODE_ENV=test ./node_modules/.bin/db-migrate up --log-level=error --env=test --config=config/database.json --migrations-dir ./db/migrations
NODE_ENV=test ./node_modules/.bin/mocha --exit --ui bdd --reporter spec --timeout 15000 --recursive ./tests/setup.js ./tests/
Redis successfully connected
  POST: API /account/phone
Redis set error The client is closed
    1) accepts a phone number and generates a PIN
  0 passing (15s)
  1 failing
  1) POST: API /account/phone
       accepts a phone number and generates a PIN:
     Error: Timeout of 15000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/app/tests/api/account.js)
  
make: *** [Makefile:66: test-ci] Error 1
-----> test command `make test-ci` failed with exit status 2

我的 app.json 文件看起来像这样

{
  "environments": {
    "test": {
      "scripts": {
        "test": "make test-ci"
      },
      "addons": [
        "heroku-redis:in-dyno",
        "heroku-postgresql:in-dyno"
      ]
    }
  }
}

我的 Redis 连接文件如下所示:

let redis = require('redis')
let client = redis.createClient({
  url: process.env.REDIS_URL
})
client.on('error', err => console.log('Redis Client Error', err))
client.on('connect', () => {
  console.log('Redis successfully connected')
})

正在测试的路线如下所示:

redis.set('phone:keys:' + phone, code, { EX: 60 * 5 }).then(val => {
  console.log('did it get set in redis?')
  twilio.send(phone, message).then(response => {
    res.status(201).json({
      ok: true,
      data: { body: response.body },
    })
  })
})
.catch((err) => {
  console.log('Redis set error', err.message)
})

在本地环境(和生产)中,一切正常。即使在本地运行测试。但只有在 Heroku CI 中它失败了,我看到"Redis set error The client is closed "

4

0 回答 0