0

I have a test where I am performing a docker stop operation and checking something and starting the docker container. I am using dockerode library. When I run that I test I keep getting the following error - Error: Timeout of 120000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. I am using async/await. Here's the sample of the code

describe('docker container test', () => {
    let docker

    before(() => {
        docker = new Docker()
    })

    after( () => {
        docker = null
    })

it('simple docker test', async () => {
        try {
            container = await getDockerContainerByName(docker, containerName)
            await stopDocker(docker, container)
            await startDocker(docker, container)
            await waitForContainerToBeHealthy(docker, container)
            console.log("container healthy")
        } catch(error) {
            console.log("Error occurred "+ JSON.stringify(error))
        }
    }).timeout(120000)
})

async waitForContainerToBeHealthy (docker, container) {
        await invokeUntil(async (retry, complete) => {
            return dockerTestHelper.inspectDockerContainer(docker, container).then( (data) => {
                if(// check if container is healthy) {
                    complete()
                } else {
                    retry()
                }
            })
        })
    }

I can see on the console that the container is healthy. But, the test doesn't end. It hangs and after some time I see the error posted above. Can someone let me know what am I doing wrong

chan
  • 274
  • 1
  • 5
  • 24

2 Answers2

0

first of all, you should throw an error after you print it

} catch(error) {
  console.log("Error occurred "+ JSON.stringify(error));
  throw error;
}

Now looking at your code, it actually looks good and should resolve, I would suggest you try to debug like this

it('simple docker test', (done) => {
  getDockerContainerByName(docker, containerName).then((container) => {
    console.log("reached 1");
    stopDocker(docker, container).then(() => 
    {
      console.log("reached 2");
      startDocker(docker, container).then(() => {
        console.log("reached 3");
        waitForContainerToBeHealthy(docker, container).then(() => {
          console.log("reached 4");
          done()
        })
      })
    })
  })
})

If it reached the done() call, the test will be resolved. so you should check if it reaches there before the timeout is done. if you are absolutely sure it reached the done() on time, then you probably got a problem that is not shown in your post.

Amit Kahlon
  • 108
  • 1
  • 1
  • 10
  • it is resolving but somehow test is not ending. It keeps running even after last function resolves. – chan Oct 26 '22 at 21:18
  • if you implement what I added here, you will just need to figure out if it reached the `done()` method, if it did, the test will be resolved, if it didn't, you should debug why – Amit Kahlon Oct 27 '22 at 08:20
0

I think you should not return dockerTestHelper here you don't use the variable the approach you used to ditch the done is a really smart thing to do but I think Mocha thinks that still you are returning a promise. my suggestion is to change your code to this and see if it work or not

const data = await dockerTestHelper.inspectDockerContainer(docker, container);
if(// check if container is healthy) {
    complete()
  } else {
    retry()
}

I hope it help

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
Farbod Shabani
  • 422
  • 4
  • 9