<commit_before>var should = require("should");
var Sifaka = require("../../index.js").Sifaka;

module.exports = function (DEBUG) {
    return {
        'should pass through a get to a work function': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);

            var returnValue = "12345asdf";
            cache.get("abc", function (callback) {
                callback(null, returnValue);
            }, {}, function (err, data, meta) {
                should.exist(meta);
                should.not.exist(err);
                should.exist(data);
                data.should.equal("12345asdf");
                done();
            });
        }, 'should correctly return a positive exists call': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);

            var returnValue = "12345asdf";
            cache.get("abc", function (callback) {
                callback(null, returnValue);
            }, {}, function (err, data, meta) {
                should.exist(meta);
                should.not.exist(err);
                should.exist(data);
                data.should.equal("12345asdf");
                cache.exists("abc", {}, function (err, exists, meta) {
                    should.exist(meta)
                    meta.hit.should.equal(true);

                    exists.should.equal(true);
                    done();
                })
            });
        },

        'should correctly return a negative exists call': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            cache.exists("abc", {}, function (err, exists, meta) {
                should.exist(meta);
                meta.hit.should.equal(false);
                exists.should.equal(false);
                done();
            });

        },

        'should only fire the work function once': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            var key = "abc";
            var returnValue = "12345asdf";
            var callCount = 0;
            var workFunction = function (callback) {
                callCount += 1;
                setTimeout(function () {
                    callback(null, returnValue);
                }, 1000);
            };
            var completionCount = 0;
            var complete = function (err, data, meta) {
                should.not.exist(err);
                should.exist(data);
                should.exist(meta);

                data.should.equal("12345asdf");
                completionCount += 1;

                if(completionCount == 2) {
                    callCount.should.equal(1);
                    if(backend.locks) { // In-memory only
                        backend.locks.should.have.property(key, null);
                    }
                    done();
                }
            }

            cache.get(key, workFunction, {}, complete);
            cache.get(key, workFunction, {}, complete);
        },

        'should return only meta when requested on hit': function (backend, done) {

            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            var key = "abc";
            var returnValue = "12345asdf";
            var callCount = 0;
            var workFunction = function (callback) {
                callCount += 1;
                setTimeout(function () {
                    cache.debug("", "Finished Work")
                    callback(null, returnValue, function (err, succeeded) {
                        should.not.exist(err);
                        succeeded.should.equal(true);
                        cache.get(key, workFunction, {metaOnly: "hit"}, complete);
                    });
                }, 100);
            };
            var completionCount = 0;
            var complete = function (err, data, meta) {
                should.not.exist(err);

                should.exist(meta);

                completionCount += 1;

                if(completionCount < 3) {
                    meta.hit.should.equal(false);
                    meta.should.have.property("pending", true);
                    should.exist(data);
                    data.should.equal(returnValue);
                }

                if(completionCount == 3) {
                    callCount.should.equal(1);
                    meta.should.not.have.property("pending");
                    meta.hit.should.equal(true);
                    should.not.exist(data);
                    done();
                }
            }

            cache.get(key, workFunction, {metaOnly: "hit"}, complete);
            cache.get(key, workFunction, {metaOnly: "hit"}, complete);
        },

        'should return only meta when requested on miss': function (backend, done) {

            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            var key = "abc";
            var returnValue = "12345asdf";
            var callCount = 0;
            var workFunction = function (callback) {
                callCount += 1;
                setTimeout(function () {
                    callback(null, returnValue);
                    // Trigger the third cache call once we know we will get a hit
                    setTimeout(function () {
                        cache.get(key, workFunction, {metaOnly: "miss"}, complete)
                    }, 100);
                }, 100);
            };
            var completionCount = 0;
            var complete = function (err, data, meta) {
                should.not.exist(err);

                should.exist(meta);

                completionCount += 1;

                if(completionCount < 3) {
                    // first two requests should be misses, and no work should happen
                    callCount.should.equal(0);
                    meta.hit.should.equal(false);
                    meta.should.not.have.property("pending"); // We should have bypassed the pending callbacks
                    should.not.exist(data);
                }
<commit_msg>Add a bit more leeway for remote testing<commit_after>var should = require("should");
var Sifaka = require("../../index.js").Sifaka;

module.exports = function (DEBUG) {
    return {
        'should pass through a get to a work function': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);

            var returnValue = "12345asdf";
            cache.get("abc", function (callback) {
                callback(null, returnValue);
            }, {}, function (err, data, meta) {
                should.exist(meta);
                should.not.exist(err);
                should.exist(data);
                data.should.equal("12345asdf");
                done();
            });
        }, 'should correctly return a positive exists call': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);

            var returnValue = "12345asdf";
            cache.get("abc", function (callback) {
                callback(null, returnValue);
            }, {}, function (err, data, meta) {
                should.exist(meta);
                should.not.exist(err);
                should.exist(data);
                data.should.equal("12345asdf");
                cache.exists("abc", {}, function (err, exists, meta) {
                    should.exist(meta)
                    meta.hit.should.equal(true);

                    exists.should.equal(true);
                    done();
                })
            });
        },

        'should correctly return a negative exists call': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            cache.exists("abc", {}, function (err, exists, meta) {
                should.exist(meta);
                meta.hit.should.equal(false);
                exists.should.equal(false);
                done();
            });

        },

        'should only fire the work function once': function (backend, done) {
            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            var key = "abc";
            var returnValue = "12345asdf";
            var callCount = 0;
            var workFunction = function (callback) {
                callCount += 1;
                setTimeout(function () {
                    callback(null, returnValue);
                }, 1000);
            };
            var completionCount = 0;
            var complete = function (err, data, meta) {
                should.not.exist(err);
                should.exist(data);
                should.exist(meta);

                data.should.equal("12345asdf");
                completionCount += 1;

                if(completionCount == 2) {
                    callCount.should.equal(1);
                    if(backend.locks) { // In-memory only
                        backend.locks.should.have.property(key, null);
                    }
                    done();
                }
            }

            cache.get(key, workFunction, {}, complete);
            cache.get(key, workFunction, {}, complete);
        },

        'should return only meta when requested on hit': function (backend, done) {

            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            var key = "abc";
            var returnValue = "12345asdf";
            var callCount = 0;
            var workFunction = function (callback) {
                callCount += 1;
                setTimeout(function () {
                    cache.debug("", "Finished Work")
                    callback(null, returnValue, function (err, succeeded) {
                        should.not.exist(err);
                        succeeded.should.equal(true);
                        cache.get(key, workFunction, {metaOnly: "hit"}, complete);
                    });
                }, 300);
            };
            var completionCount = 0;
            var complete = function (err, data, meta) {
                should.not.exist(err);

                should.exist(meta);

                completionCount += 1;

                if(completionCount < 3) {
                    meta.hit.should.equal(false, "Completion " + completionCount + " should have been a miss, but registered as a hit.");
                    meta.should.have.property("pending", true);
                    should.exist(data);
                    data.should.equal(returnValue);
                }

                if(completionCount == 3) {
                    callCount.should.equal(1);
                    meta.should.not.have.property("pending");
                    meta.hit.should.equal(true);
                    should.not.exist(data);
                    done();
                }
            }

            cache.get(key, workFunction, {metaOnly: "hit"}, complete);
            cache.get(key, workFunction, {metaOnly: "hit"}, complete);
        },

        'should return only meta when requested on miss': function (backend, done) {

            should.exist(Sifaka);
            var options = {debug: DEBUG};
            var cache = new Sifaka(backend, options)
            should.exist(cache);
            var key = "abc";
            var returnValue = "12345asdf";
            var callCount = 0;
            var workFunction = function (callback) {
                callCount += 1;
                setTimeout(function () {
                    callback(null, returnValue);
                    // Trigger the third cache call once we know we will get a hit
                    setTimeout(function () {
                        cache.get(key, workFunction, {metaOnly: "miss"}, complete)
                    }, 200);
                }, 200);
            };
            var completionCount = 0;
            var complete = function (err, data, meta) {
                should.not.exist(err);

                should.exist(meta);

                completionCount += 1;

                if(completionCount < 3) {
                    // first two requests should be misses, and no work should happen
                    callCount.should.equal(0);
                    meta.hit.should.equal(false);
                    meta.should.not.have.property("pending"); // We should have bypassed the pending callbacks
                    should.not.exist(data);
                }

{'fraction_non_alphanumeric': 0.11162547599492272, 'fraction_numerical': 0.010751885313223325, 'mean_word_length': 1.3017700635848084, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '900910', 'n_tokens_mistral': 3438, 'n_tokens_neox': 3117, 'n_words': 789}