Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
How to concurrently run blocking and non blocking code?

I need to be able to run some code that is going to be blocking and some other code that will then, when blocked, start some other actions.

The use-case is the follows:

I have a file, called index.ts, running an express and socket server

I have a testfile, called test.spec.ts, that needs to be able to start the express server and then initiate some commands for running tests either via HTTP request or socket message(I would prefer HTTP)

The only way I found to keep the webserver alive is instantiating it with

import { spawnSync } from 'child_process';

spawnSync('ts-node', ['path/to/index.ts"], { cwd: "path/to/workdir"});

which will block until the child process is killed ( could be up to 30min later ).

Is there a way to split into two processes, one that gets blocked when starting it and one continuing to work that exposes some functions for interactions with the test file?

My target would look like this:

// index.ts

import * as express from "express";
const app = express();

const port = 3000;

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`);
});
// test.spec.ts

import { spawnSync } from 'child_process';

describe("Test",()=>{

  it("Test", async ()=>{

    // create somehow a child process that should block
    const childProcess = ...
    childProcess.do(spawnSync('ts-node', ['path/to/index.ts'], {cwd: 'path/to/workdir'}) //should block now

    // the following code should run in parallel
    await new Promise(r => setTimeout(r, 5000)); //wait some time until the webserver is ready

    fetch('http://localhost:3000').then((ret)=>{
      expect(ret,'to be Hello World').to.contain('Hello World!");
    })
    ... // more tests
  });
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think I found the reason and a possible solution myself.

Node always runs in a single process therefore it can't work. Luckily I was able to discover a totally different approach. I installed the npm-run-all package and created a new script in my package.json file calling run-p script1 script2. This way I can run both in parallel and the process with the webserver stays alive until all tests are run

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda