سؤال

I'm attempting to use tape to test an API built with restify. The issue I'm having is that the tests don't complete until some "timeout" or something occurs. The test suite just hangs. Here's a simple test I'm using:

var test = require('tape');
var restify = require('restify');
var client = restify.createJsonClient({url: 'http://localhost:9000'});

test('GET /events/foo is 401 w/o auth', function(t) {
  client.get('/events/foo', function(err, req, res, obj) {
    t.equal(res.statusCode, 401);
    t.end();
  });
});

What am I missing or doing wrong?

هل كانت مفيدة؟

المحلول

Turns out the problem is because restify clients use keepalive by default. It can be disabled by setting agent: false upon construction or calling client.close when finished.

نصائح أخرى

I have found that tape tests are easier to comprehend and reason about if I use t.plan rather than t.end. In this case t.end is called only if/when client.get calls back, which may be never. For example:

test('GET /events/foo is 401 w/o auth', function(t) {
  t.plan(1);
  client.get('/events/foo', function(err, req, res, obj) {
    t.equal(res.statusCode, 401);
  });
});

This slightly modified test will timeout much sooner and fail even if client.get never calls back.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top