<commit_before>/*
 * Copyright (c) 2012 <NAME>
 * Licensed under the MIT license.
 */

var assert = require('assert');
var events = require('events');
var sinon = require('sinon');
var util = require('util');

var FunctionCall = require('../lib/function_call');

function MockBackoff() {
    events.EventEmitter.call(this);

    this.reset = sinon.spy();
    this.backoff = sinon.spy();
    this.failAfter = sinon.spy();
}
util.inherits(MockBackoff, events.EventEmitter);

exports["FunctionCall"] = {
    setUp: function(callback) {
        this.wrappedFn = sinon.stub();
        this.callback = sinon.stub();
        this.backoff = new MockBackoff();
        this.backoffFactory = sinon.stub();
        this.backoffFactory.returns(this.backoff);
        callback();
    },

    tearDown: function(callback) {
        callback();
    },

    "constructor's first argument should be a function": function(test) {
        test.throws(function() {
            new FunctionCall(1, [], function() {});
        }, /should be a function/);
        test.done();
    },

    "constructor's last argument should be a function": function(test) {
        test.throws(function() {
            new FunctionCall(function() {}, [], 3);
        }, /should be a function/);
        test.done();
    },

    "setStrategy should overwrite the default strategy": function(test) {
        var replacementStrategy = {};
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.setStrategy(replacementStrategy);
        call.start(this.backoffFactory);
        test.ok(this.backoffFactory.calledWith(replacementStrategy),
            'User defined strategy should be used to instantiate ' +
            'the backoff instance.');
        test.done();
    },

    "setStrategy should throw if the call is in progress": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.start(this.backoffFactory);
        test.throws(function() {
            call.setStrategy({});
        }, /in progress/);
        test.done();
    },

    "failAfter should not be set by default": function(test) {<commit_msg>Add tests for isPending, isRunning, isCompleted and isAborted.<commit_after>/*
 * Copyright (c) 2012 <NAME>
 * Licensed under the MIT license.
 */

var assert = require('assert');
var events = require('events');
var sinon = require('sinon');
var util = require('util');

var FunctionCall = require('../lib/function_call');

function MockBackoff() {
    events.EventEmitter.call(this);

    this.reset = sinon.spy();
    this.backoff = sinon.spy();
    this.failAfter = sinon.spy();
}
util.inherits(MockBackoff, events.EventEmitter);

exports["FunctionCall"] = {
    setUp: function(callback) {
        this.wrappedFn = sinon.stub();
        this.callback = sinon.stub();
        this.backoff = new MockBackoff();
        this.backoffFactory = sinon.stub();
        this.backoffFactory.returns(this.backoff);
        callback();
    },

    tearDown: function(callback) {
        callback();
    },

    "constructor's first argument should be a function": function(test) {
        test.throws(function() {
            new FunctionCall(1, [], function() {});
        }, /should be a function/);
        test.done();
    },

    "constructor's last argument should be a function": function(test) {
        test.throws(function() {
            new FunctionCall(function() {}, [], 3);
        }, /should be a function/);
        test.done();
    },

    "isPending should return false once the call is started": function(test) {
        this.wrappedFn.yields(new Error()).yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);

        test.ok(call.isPending());

        call.start(this.backoffFactory);
        test.ok(!call.isPending());

        this.backoff.emit('ready');
        test.ok(!call.isPending());

        test.done();
    },

    "isRunning should return true when call is in progress": function(test) {
        this.wrappedFn.yields(new Error()).yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);

        test.ok(!call.isRunning());

        call.start(this.backoffFactory);
        test.ok(call.isRunning());

        this.backoff.emit('ready');
        test.ok(!call.isRunning());

        test.done();
    },

    "isCompleted should return true once the call completes": function(test) {
        this.wrappedFn.yields(new Error()).yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);

        test.ok(!call.isCompleted());

        call.start(this.backoffFactory);
        test.ok(!call.isCompleted());

        this.backoff.emit('ready');
        test.ok(call.isCompleted());

        test.done();
    },

    "isAborted should return true once the call is aborted": function(test) {
        this.wrappedFn.yields(new Error()).yields(null, 'Success!');
        var call = new FunctionCall(this.wrappedFn, [], this.callback);

        test.ok(!call.isAborted());
        call.abort();
        test.ok(call.isAborted());

        test.done();
    },

    "setStrategy should overwrite the default strategy": function(test) {
        var replacementStrategy = {};
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.setStrategy(replacementStrategy);
        call.start(this.backoffFactory);
        test.ok(this.backoffFactory.calledWith(replacementStrategy),
            'User defined strategy should be used to instantiate ' +
            'the backoff instance.');
        test.done();
    },

    "setStrategy should throw if the call is in progress": function(test) {
        var call = new FunctionCall(this.wrappedFn, [], this.callback);
        call.start(this.backoffFactory);
        test.throws(function() {
            call.setStrategy({});
        }, /in progress/);
        test.done();
    },

    "failAfter should not be set by default": function(test) {
{'fraction_non_alphanumeric': 0.14640239840106595, 'fraction_numerical': 0.001998667554963358, 'mean_word_length': 2.6817903126916, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3063555', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1703, 'n_words': 390}