1

I'm updating a library to work with the latest Hapi libraries, including Lab. With the old version of Lab, I was able to run two experiments, each with their own Hapi.server instance, and both using port 3000. The new version of Lab, however, either finishes too quickly, or run asynchronously, causing a "port in use" error on the second experiment.

var lab = exports.lab = Lab.script();
var routes = [{...}, {...}, {...}];

lab.before(async () => { ... });

lab.experiment('experiments', () => {

    lab.experiment('experiment 1', () => {

        var server;

        lab.before(() => {
          server = Hapi.server({ port: 3000 });
          return server.start();
        });

        lab.test('test 1.1', async () => {
            return server.register(Plugin)
            .then(() => server.route(routes))
            .catch((error) => console.log(error));
        });

        lab.experiment('experiment 1.1', () => {
            lab.test('test 1.1.1', async () => {
                const response = await server.inject({...});
                Code.expect(response.result)...;
            });

            lab.test('test 1.1.2', async () => {
                const response = await server.inject({...});
                Code.expect(response.result)...;
            });
        });
        
    });

    lab.experiment('experiment 2', () => {

        var server;

        lab.before(() => {
          server = Hapi.server({ port: 3000 });  
          return server.start();
        });

        lab.test('test 1.1', async () => {
            return server.register([{...}, {...}])
            .then(() => server.route(routes))
            .catch((error) => console.log(error));
        });

        lab.experiment('experiment 2.1', () => {
            lab.test('test 2.1.1', async () => {
                const response = await server.inject({...});
                Code.expect(response.result)...;
            });
            lab.test('test 2.1.2', async () => {
                const response = await server.inject({...});
                Code.expect(response.statusCode)...;
            });
        });
    });
});

The second experiment fails:

listen EADDRINUSE: address already in use 0.0.0.0:3000
      at Server.setupListenHandle [as _listen2] (net.js:1318:16)
      at listenInCluster (net.js:1366:12)
      at doListen (net.js:1503:7)
      at processTicksAndRejections (internal/process/task_queues.js:81:21)

There were 1 test script error(s).

Changing the port to 3001 solves the problem, but I'd like to know what changed with Lab, and what changes can I make so the same port can be used for both server instances.

John Manko
  • 1,828
  • 27
  • 51

0 Answers0