question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Random test success/failure on Windows

See original GitHub issue

I test this API:

function handleError(res){
    return function(data) {
        res.status(data.status).json(data.error);
    };
}

function reject(deferred, error, statusCode) {
    deferred.reject({
        status: statusCode || error.status || 500,
        error: error.payload
    });
}

function responseWithResult(res){
    return function(data) {
        if (data.entity) {
            return res.status(data.statusCode).json(data.entity);
        }
        res.status(500).json(error.UNKNOWN_ERROR.payload);
    };
}

function resolve(deferred, entity, statusCode) {
    deferred.resolve({
        statusCode: statusCode || 200,
        entity: entity
    });
}

function validate(deferred, filename, mimetype) {
    if (/.(?:jpe?g|png|gif)$/.test(filename.toLowerCase()) === false) {
    // Should reply here with status code 400 and body: { "code": 90, "message": "The file extension is not supported. Accepted extensions: jpg, png and gif." }
        reject(deferred, error.FILE_EXTENSION_NOT_SUPPORTED, 400); 
        return false;
    }
    if (/^image\/.+$/.test(mimetype) === false) {
        reject(deferred, error.WRONG_MIME_TYPE, 400);
        return false;
    }
    return true;
}

exports.create = function(req, res) {
    var deferredResponse = Q.defer();
    var image;

    deferredResponse.promise
        .then(responseWithResult(res))
        .catch(handleError(res));

    var form = new busboy({
        headers: req.headers,
        limits: {
            fields: 1,
            parts: 2,
            files: 1,
            fileSize: 5 * 1024 * 1024
        }
    })
    .on('file', function(fieldname, file, filename, encoding, mimetype) {
        if (validate(deferredResponse, filename, mimetype)) {
            image = uniqueFilename(path.extname(filename));
            file.pipe(fs.createWriteStream(path.join(IMAGES_FOLDER, image)));
        }
        file.on('data', function(data) {});
        file.on('end', function() {
            if (file.truncated) {
                reject(deferredResponse, error.FILE_SIZE_LIMIT_EXCEEDED);
            }
        });
    })
    .on('error', function() {
        reject(deferredResponse, error.BAD_PARSING);
    })
    .on('finish', function() {
        resolve(deferredResponse, { filename: image }, 201);
    });
    req.pipe(form);
};

With this test:

it('should respond with with 400 and the error code 90 on unsupported file extension', function(done) {
    request(app)
        .post('/api/images')
        .set('authorization', 'Bearer ' + token)
        .attach('file', path.join(TEST_FOLDER, 'test.tiff'))
        .expect(400)
        .expect('Content-Type', /json/)
        .end(function(err, res) {
            if (err) return done(err);
            expect(res.body.code).to.equal(90);
            done();
        });
});

It always success on MacOS and TravisCI but when I run it on Windows, it fails most of time (6/10 times) and returns the error:

  1) Image API: POST /api/images should respond with with 400 and the error code 90 on unsupported file extension:
     Uncaught TypeError: Cannot read property 'header' of undefined
      at Test.assert (<project_root>\node_modules\supertest\lib\test.js:188:17)
      at Server.assert (<projet_root>\node_modules\supertest\lib\test.js:131:12)
      at Server.g (events.js:199:16)
      at Server.emit (events.js:104:17)
      at net.js:1409:10
      at process._tickDomainCallback (node.js:381:11)

The API works as expected while uploading a tiff image in Chrome: image

Details:

  • Node v0.12.2
  • Windows 8.1 x64
  • grunt-mocha-test 0.12.7
  • supertest 1.0.1
  • chai-as-promised 5.0.0
  • chai-things 0.2.0
  • sinon-chai 2.7.0

I’ve never seen anything like that before. The random behavior with the same code is really weird.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
Shingazcommented, May 18, 2015

I figured it out. I just added the "Connection: keep-alive" header in the request. If someone has an explanation, I would be happy to know it!

it('should respond with with 400 and the error code 90 on unsupported file extension', function(done) {
    request(app)
        .post('/api/images')
        .set('authorization', 'Bearer ' + token)
        .set('Connection', 'keep-alive')
        .attach('file', path.join(TEST_FOLDER, 'test.tiff'))
        .expect(400)
        .expect('Content-Type', /json/)
        .end(function(err, res) {
            if (err) return done(err);
            expect(res.body.code).to.equal(90);
            done();
        });
});
2reactions
Overdrivrcommented, Apr 21, 2016

I had the same exact issue, but with a different root cause I believe. I was using malformed paths like for instance api/auth/signin/ instead of /api/auth/signin. As soon as I switched to the second format tests ran fine. Hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Random test success/failure on Windows · Issue #230 - GitHub
I have no idea why the crud test Random success/failure on docker Jenkins. The post and put request often fail. The error log...
Read more >
Troubleshooting random failures with Coded UI Tests
Coded UI Automation Tests runs fine locally but randomly fails when run on the server. The functionality which you are trying to verify...
Read more >
Random Crashing - Microsoft Community
I've had my computer hard crash twice now, no BSOD or anything, just hard reboot, so i went into event viewer and found...
Read more >
How to Fix Flaky Tests - Semaphore CI
Randomly failing tests are the hardest to debug. Here's a framework you can use to fix them and keep your test suite healthy....
Read more >
Troubleshooting Continuous Integration, or How to Debug ...
Random test failures can also happen if you use a random number generator somewhere in your code, which sometimes triggers a limit case....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found