<commit_before>var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;
var Config = require('../../.').Config;
var Controller = require('../RC');
var HazelcastClient = require('../../.').Client;
var DataRecord = require('../../lib/nearcache/NearCache').DataRecord;
var NearCacheImpl = require('../../lib/nearcache/NearCache').NearCacheImpl;
var EvictionPolicy = Config.EvictionPolicy;
var promiseLater = require('../Util').promiseLater;
var SerializationService = require('../../lib/serialization/SerializationService').SerializationServiceV1;
describe('NearCacheImpl', function() {
    var cluster;
    var client;

    var invalidateOnChange = [false, true];
    var ttls = [0, 1];
    var evictionPolicy = [EvictionPolicy.LFU, EvictionPolicy.LRU, EvictionPolicy.RANDOM, EvictionPolicy.NONE];

    var testConfigs = [];

    evictionPolicy.forEach(function (evictionPolicy) {
        invalidateOnChange.forEach(function (ioc) {
            ttls.forEach(function (ttl) {
                var ncc = new Config.NearCacheConfig();
                ncc.invalidateOnChange = ioc;
                ncc.timeToLiveSeconds = ttl;
                ncc.evictionMaxSize = 100;
                ncc.evictionPolicy = evictionPolicy;
                testConfigs.push(ncc);
            });
        });
    });

    function ds(str) {
        return {
            val: str,
            hashCode: function () {
                return str[0] - 'a';
            },
            equals(other) {
                return this.val === other.val;
            }
        }
    }

    before(function() {
        return Controller.createCluster(null, null).then(function(res) {
            cluster = res;
            return Controller.startMember(cluster.id);
        }).then(function () {
            return HazelcastClient.newHazelcastClient();
        }).then(function (cl) {
            client = cl;
        });
    });

    after(function() {
        client.shutdown();
        return Controller.shutdownCluster(cluster.id);
    });

    describe('CacheRecord', function () {

        it('does not expire if ttl is 0', function (done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 0);
            setTimeout(function () {
                if (rec.isExpired()) {
                    done(new Error('Unlimited ttl record expired'));
                } else {
                    done();
                }
            }, 1000);
        });

        it('expires after ttl', function (done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 1);
            setTimeout(function () {
                if (rec.isExpired()) {
                    done();
                } else {
                    done(new Error('Record did not expire after ttl'));
                }
            }, 1100);
        });

        it('does not expire before ttl', function (done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 10);
            setTimeout(function () {
                if (rec.isExpired()) {
                    done(new Error('Record expired before ttl'));
                } else {
                    done();
                }
            }, 100);
        });

        it('expires after maxIdleSeconds', function(done) {
            this.timeout(4000);
            var rec = new DataRecord(ds('key'), 'value', undefined, 100);
            setTimeout(function () {
                if (rec.isExpired(1)) {
                    done();
                } else {
                    done(new Error('did not expire after maxIdleSeconds'));
                }
            }, 2000);
        });

        it('does not expire while active', function(done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 100);
            setTimeout(function () {
                rec.setAccessTime();
                if (rec.isExpired(1)) {
                    done(new Error('expired'));
                } else {
                    done();
                }
            }, 100);
        });
    });

    testConfigs.forEach(function (testConfig) {

        describe(testConfig.toString(), function () {

            var nearCache;

            beforeEach(function () {
                this.timeout(50);
                nearCache = new NearCacheImpl(testConfig, createSerializationService());
            });


            it('simple put/get', function () {
                nearCache.put(ds('key'), 'val');
                return expect(nearCache.get(ds('key'))).to.equal('val');
            });

<commit_msg>remove unreasonable timeout from nearcache test beforeEach hook<commit_after>var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;
var Config = require('../../.').Config;
var Controller = require('../RC');
var HazelcastClient = require('../../.').Client;
var DataRecord = require('../../lib/nearcache/NearCache').DataRecord;
var NearCacheImpl = require('../../lib/nearcache/NearCache').NearCacheImpl;
var EvictionPolicy = Config.EvictionPolicy;
var promiseLater = require('../Util').promiseLater;
var SerializationService = require('../../lib/serialization/SerializationService').SerializationServiceV1;
describe('NearCacheImpl', function() {
    var cluster;
    var client;

    var invalidateOnChange = [false, true];
    var ttls = [0, 1];
    var evictionPolicy = [EvictionPolicy.LFU, EvictionPolicy.LRU, EvictionPolicy.RANDOM, EvictionPolicy.NONE];

    var testConfigs = [];

    evictionPolicy.forEach(function (evictionPolicy) {
        invalidateOnChange.forEach(function (ioc) {
            ttls.forEach(function (ttl) {
                var ncc = new Config.NearCacheConfig();
                ncc.invalidateOnChange = ioc;
                ncc.timeToLiveSeconds = ttl;
                ncc.evictionMaxSize = 100;
                ncc.evictionPolicy = evictionPolicy;
                testConfigs.push(ncc);
            });
        });
    });

    function ds(str) {
        return {
            val: str,
            hashCode: function () {
                return str[0] - 'a';
            },
            equals(other) {
                return this.val === other.val;
            }
        }
    }

    before(function() {
        return Controller.createCluster(null, null).then(function(res) {
            cluster = res;
            return Controller.startMember(cluster.id);
        }).then(function () {
            return HazelcastClient.newHazelcastClient();
        }).then(function (cl) {
            client = cl;
        });
    });

    after(function() {
        client.shutdown();
        return Controller.shutdownCluster(cluster.id);
    });

    describe('CacheRecord', function () {

        it('does not expire if ttl is 0', function (done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 0);
            setTimeout(function () {
                if (rec.isExpired()) {
                    done(new Error('Unlimited ttl record expired'));
                } else {
                    done();
                }
            }, 1000);
        });

        it('expires after ttl', function (done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 1);
            setTimeout(function () {
                if (rec.isExpired()) {
                    done();
                } else {
                    done(new Error('Record did not expire after ttl'));
                }
            }, 1100);
        });

        it('does not expire before ttl', function (done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 10);
            setTimeout(function () {
                if (rec.isExpired()) {
                    done(new Error('Record expired before ttl'));
                } else {
                    done();
                }
            }, 100);
        });

        it('expires after maxIdleSeconds', function(done) {
            this.timeout(4000);
            var rec = new DataRecord(ds('key'), 'value', undefined, 100);
            setTimeout(function () {
                if (rec.isExpired(1)) {
                    done();
                } else {
                    done(new Error('did not expire after maxIdleSeconds'));
                }
            }, 2000);
        });

        it('does not expire while active', function(done) {
            var rec = new DataRecord(ds('key'), 'value', undefined, 100);
            setTimeout(function () {
                rec.setAccessTime();
                if (rec.isExpired(1)) {
                    done(new Error('expired'));
                } else {
                    done();
                }
            }, 100);
        });
    });

    testConfigs.forEach(function (testConfig) {

        describe(testConfig.toString(), function () {

            var nearCache;

            beforeEach(function () {
                nearCache = new NearCacheImpl(testConfig, createSerializationService());
            });


            it('simple put/get', function () {
                nearCache.put(ds('key'), 'val');
                return expect(nearCache.get(ds('key'))).to.equal('val');
            });


{'fraction_non_alphanumeric': 0.13579175704989155, 'fraction_numerical': 0.009327548806941432, 'mean_word_length': 1.6451520367183017, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7035843', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2362, 'n_words': 510}