<commit_before>var assert = require('assert');
var _ = require('underscore');
var testClient = require('../../lib/test-redis-client').client;
var rateLimiter = require('../../lib/rate-limiter');

describe('Test the functions that are adding values to redis', function() {
    describe('Test ensureAppAndKey', function () {
        it('Test basic setup', function(){
            testClient.innerDict = {};
            rateLimiter.ensureAppAndKey("testApp", "key1", 60, testClient, function(){})
            assert.equal('60', testClient.innerDict['testApp:key1_rl']);
            assert.equal('60', testClient.innerDict['testApp:key1_tkns']);
            assert.equal('testApp:key1_epoch_ms' in testClient.innerDict, true);
        });
        it('Test more complicated setup', function(){
            testClient.innerDict = {};
            rateLimiter.ensureAppAndKey("testApp", "key1", 60, testClient, function(){})
            rateLimiter.ensureAppAndKey("testApp", "key2", 55, testClient, function(){})
            rateLimiter.ensureAppAndKey("testApp", "key3", 50, testClient, function(){})
            rateLimiter.ensureAppAndKey("testApp", "key1", 45, testClient, function(){})
            assert.equal(9, _.keys(testClient.innerDict).length)
            assert.equal('60', testClient.innerDict['testApp:key1_rl']);
            assert.equal('60', testClient.innerDict['testApp:key1_tkns']);
            assert.equal('testApp:key1_epoch_ms' in testClient.innerDict, true);
            assert.equal('55', testClient.innerDict['testApp:key2_rl']);
            assert.equal('55', testClient.innerDict['testApp:key2_tkns']);
            assert.equal('testApp:key2_epoch_ms' in testClient.innerDict, true);
            assert.equal('50', testClient.innerDict['testApp:key3_rl']);
            assert.equal('50', testClient.innerDict['testApp:key3_tkns']);
            assert.equal('testApp:key3_epoch_ms' in testClient.innerDict, true);
        });
    });
    describe('Test keyToken', function(){
        it('Test Basic Setup', function(){
            testClient.innerDict = {};
            testClient.innerDict["testApp"] = ["key1", "key2", "key3"];
            testClient.innerDict["testApp:key1_tkns"] = "60";
            testClient.innerDict["testApp:key2_tkns"] = "55";
            testClient.innerDict["testApp:key3_tkns"] = "50";
            rateLimiter.keyToken("testApp", "key1", testClient, function(err, response){
                assert.equal(null, err);
                assert.equal(60, response);
            });
            rateLimiter.keyToken("testApp", "key2", testClient, function(err, response){
                assert.equal(null, err);
                assert.equal(55, response);
            });
            rateLimiter.keyToken("testApp", "key3", testClient, function(err, response){
                assert.equal(null, err);
                assert.equal(50, response);
            });
        });
        it('Empty dict', function(){
            testClient.innerDict = {};
            rateLimiter.keyToken("testApp", "key1", testClient, function(err, response){
                assert.equal(null, err);
                assert.equal(null, response);
            });
        });
    });
});
<commit_msg>make tests use redis and run asyncrhonously<commit_after>var assert = require('assert');
var _ = require('underscore');
var async = require('async');
var testClient = require('../../lib/redis-client').client;
var rateLimiter = require('../../lib/rate-limiter');

describe('Test the functions that are adding values to redis', function() {
    beforeEach(function(done){
        testClient.keys('*', function(err, keys){
            array = [];
            for (i=0; i < keys.length; i++){
                var val = keys[i]
                addToArray = function(val){
                    array.push(function(cb){
                            testClient.del(val, function(err){
                                cb();
                            });
                    });
                }
                addToArray(val);
            }
            async.waterfall(array, function(err){done();})
        });
    });
    describe('Test ensureAppAndKey', function () {
        it('Test basic setup', function(done){
            rateLimiter.ensureAppAndKey("testApp", "key", 60, testClient, function(){
                testClient.get('testApp:key_rl', function(err, res){
                    assert.equal('60', res);
                });
                testClient.get('testApp:key_tkns', function(err, res){
                    assert.equal('60', res);
                });
                testClient.get('testApp:key_epoch_ms', function(err, res){
                    assert.equal(false, null == res);
                    done();
                });
            });
        });
        it('Test more complicated setup', function(done){
            rateLimiter.ensureAppAndKey("testApp", "key1", 60, testClient, function(){
                rateLimiter.ensureAppAndKey("testApp", "key2", 55, testClient, function(){
                    rateLimiter.ensureAppAndKey("testApp", "key3", 50, testClient, function(){
                        rateLimiter.ensureAppAndKey("testApp", "key1", 45, testClient, function(){
                            async.waterfall([
                                function(cb){
                                    testClient.keys('*', function(err, res){
                                        assert.equal(9, res.length);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key1_rl', function(err, res){
                                        assert.equal('60', res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key1_tkns', function(err, res){
                                        assert.equal('60', res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key1_epoch_ms', function(err, res){
                                        assert.equal(false, null == res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key2_rl', function(err, res){
                                        assert.equal('55', res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key2_tkns', function(err, res){
                                        assert.equal('55', res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key2_epoch_ms', function(err, res){
                                        assert.equal(false, null == res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key3_rl', function(err, res){
                                        assert.equal('50', res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key3_tkns', function(err, res){
                                        assert.equal('50', res);
                                        cb();
                                    });
                                },
                                function(cb){
                                    testClient.get('testApp:key3_epoch_ms', function(err, res){
                                        assert.equal(false, null == res);
                                        cb();
                                    });
                                },
                            ],
                            function(err){done();});

                        });
                    });
                });
            });
        });
    });
    describe('Test keyToken', function(){
        it('Test Basic Setup', function(done){
            testClient.set("testApp:key1_tkns", "60", function(err){
                testClient.set("testApp:key2_tkns", "55", function(err){
                    testClient.set("testApp:key3_tkns", "50", function(err){
                        async.waterfall([
                            function(cb){
                                testClient.keys('*', function(err, keys){
                                    cb();
                                });
                            },
                            function(cb){
                                rateLimiter.keyToken("testApp", "key1", testClient, function(err, response){
                                    assert.equal(null, err);
                                    assert.equal(60, response);
                                    cb();
                                });
                            },
                            function(cb){
                                rateLimiter.keyToken("testApp", "key2", testClient, function(err, response){
                                    assert.equal(null, err);
                                    assert.equal(55, response);
                                    cb();
                                });
                            },
                            function(cb){
                                rateLimiter.keyToken("testApp", "key3", testClient, function(err, response){
                                    assert.equal(null, err);
                                    assert.equal(50, response);
                                    cb();
                                });
                            },
                            ],
                            function(err){console.log("DONE");done();});
                    });
                });
            });
        });


        it('Empty dict', function(done){
            testClient.innerDict = {};
            rateLimiter.keyToken("testApp", "key1", testClient, function(err, response){
                assert.equal(null, err);
                assert.equal(null, response);
                done();
            });
        });
    });
});

{'fraction_non_alphanumeric': 0.12460063897763578, 'fraction_numerical': 0.011501597444089457, 'mean_word_length': 1.0640542577241898, 'pattern_counts': {'":': 0, '<': 4, '<?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': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6789596', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2411, 'n_words': 403}