jehy / telegram-test-api

Simple implimentation of telegram API which can be used for testing telegram bots
MIT License
98 stars 24 forks source link

Server stopping requires too much time #2

Closed sylvainmetayer closed 6 years ago

sylvainmetayer commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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): https://github.com/nodejs/node-v0.x-archive/issues/9066

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

sylvainmetayer commented 6 years ago

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

jehy commented 6 years ago

Fixed it in version 1.0.1 : )

sylvainmetayer commented 6 years ago

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 ? sylvainmetayer/Telegram_Bot@fde0974fed2400cba0c93bbac1fdc135d0299e64

jehy commented 6 years ago

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 commented 6 years ago

Great, it is now fixed ! Thanks !