Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server stopping requires too much time #2

Closed
sylvainmetayer opened this issue Feb 1, 2018 · 7 comments
Closed

Server stopping requires too much time #2

sylvainmetayer opened this issue Feb 1, 2018 · 7 comments

Comments

@sylvainmetayer
Copy link

Hello,

I'm developping a Telegram Bot for a school project and need a way to test it.

But I've got an issue when running tests, the server doesn't seems to stop. All tests are correctly executed, but the close promise isn't resolved.

The last output I can have is the "Stopping server..." but I never enter here (https://github.com/jehy/telegram-test-api/blob/master/src/telegramServer.js#L159).

Is this a known bug or am I using your package the wrong way ?

You can find below a example of my test file.

Thank you in advance for your reply !

require("dotenv").config();
const expect = require('chai').expect;

const TelegramServer = require('telegram-test-api');
const TelegramBot = require('node-telegram-bot-api');
const Bot = require("./bot");

describe("Simple test", function () {

  var client, server, token;

  beforeEach(function (done) {
    token = process.env.TOKEN;
    let serverConfig = {
      "port": 9000,
      "host": "localhost",
      "storage": "RAM",
      "storeTimeout": 60
    };
    server = new TelegramServer(serverConfig);
    client = server.getClient(token);
    done();
  });
  
  afterEach(function (done) {
    server.stop().then(() => done());
  })

  it('should reply to hello', function () {

    this.slow(1000);
    this.timeout(3000);

    let message = client.makeMessage('hello');
    let telegramBot,
      testBot;
    return server.start()
      .then(() => client.sendMessage(message))
      .then(() => {
        let botOptions = { polling: true, baseApiUrl: server.ApiURL };
        telegramBot = new TelegramBot(token, botOptions);
        testBot = new Bot(telegramBot);
        return client.getUpdates();
      })
      .then((updates) => {
        if (updates.result.length !== 1) {
          throw new Error('updates queue should contain one message!');
        }

        var message = updates.result[0].message.text;

        if (message != "Bonjour à vous !") {
          throw new Error("Wrong expect message ! Got '" + message + "'");
        }

        return true;
      });

    });
});
@jehy
Copy link
Owner

jehy commented Mar 12, 2018

Sorry for late answer. Fixed it in version 1.00, but please beware that server needs much time (about 4 seconds) to stop (that seems like express's issue). You can check updated example for this.

@jehy jehy closed this as completed Mar 12, 2018
@jehy
Copy link
Owner

jehy commented Mar 12, 2018

@sylvainmetayer I investigated large timeout matter a bit further - it is not even express's problem, that's a nodejs problem - it does not stop http server if there are keep alive connections (and telegram bot uses keep alive connections): nodejs/node-v0.x-archive#9066

Pretty funny to see that bug from 0.x versions is still present in node 9.... I will try to find some workaround.

@jehy jehy changed the title Server doesn't seems to stop Server stopping requires too much time Mar 12, 2018
@jehy jehy reopened this Mar 12, 2018
jehy added a commit that referenced this issue Mar 12, 2018
@sylvainmetayer
Copy link
Author

@jehy Thanks for the explanation ! I'll give it a try tomorrow or wednesday and I'll come back to you

jehy added a commit that referenced this issue Mar 12, 2018
@jehy
Copy link
Owner

jehy commented Mar 12, 2018

Fixed it in version 1.0.1 : )

@jehy jehy closed this as completed Mar 12, 2018
@sylvainmetayer
Copy link
Author

I've updated to 1.0.1 but it seems that I'm still having the issue.

Here is how I tried to stop the server but I keep having an "EFATAL : Error: connect ECONNREFUSED 127.0.0.1:9000" polling error. Is it related to your last changes ?
EPSIBordeaux/Telegram_Bot@fde0974

@jehy
Copy link
Owner

jehy commented Mar 13, 2018

Telegram bot client continues polling and that causes errors. You should add telegramBot.stopPolling() to your after block like this:

  after(function (done) {
    telegramBot.stopPolling();
    // Because server.close() doesn't work and freeze CI
    if (process.env.CIRCLECI != undefined) {
      process.exit();
    }
    server.stop();
    done();
  })

By the way, server.stop(); returns promise, so it's better to use it like this (mocha becomes much prettier when using promises instead of callbacks):

  after(function (done) {
    telegramBot.stopPolling();
    // Because server.close() doesn't work and freeze CI
    if (process.env.CIRCLECI != undefined) {
      process.exit();
    }
    return server.stop();
  })

@sylvainmetayer
Copy link
Author

Great, it is now fixed ! Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants