<commit_before>require('./test-helper');
var rewire = require("rewire");
var NodeRedisCacheStore = rewire('../../src/node-redis-cache-store');

describe('NodeRedisCacheStore', function() {
  var cache,
      entryName,
      entryValue,
      client,
      restore;

  var fakeredis = require('fakeredis');
  var redis = require('redis');

  beforeEach(function(done) {
    client = fakeredis.createClient();
    client.flushdb(function(err) { done() });

    sinon.stub(redis, 'createClient', function() { return client });
    restore = NodeRedisCacheStore.__set__('redis', redis);

    entryName = 'test-redis';
    entryValue = 'test-redis';

    cache = new NodeRedisCacheStore();
    cache.logger = false;
  });

  afterEach(function() {
    redis.createClient.restore();
    restore();
  });

  describe('constructor', function() {
    var opts, logger, onErrorCallback;

    beforeEach(function() {
      logger = 'my-logger';
      opts = {logger: logger, client: [6379, '127.0.0.1', {max_attempts: 5}]};
      cache = new NodeRedisCacheStore({redis: opts});
    });

    it('holds a reference of redis options at "redisOptions" attribute', function() {
      expect(cache.redisOptions).to.deep.equal(opts);
    });

    it('holds a reference of redis client at "storage"', function() {
      expect(cache.storage).to.eql(client)
    });

    it('accepts a custom logger at "redis.logger" attribute', function() {
      expect(cache.logger).to.equal(logger);
    });

    it('calls method "auth" if attribute "redis.password" is defined', function() {
      var password = '<PASSWORD>';
      sinon.spy(client, 'auth');
      cache = new NodeRedisCacheStore({redis: {password: password}});
      expect(client.auth).to.have.been.calledWith(password);
      client.auth.restore();
    });
  });

  describe('#read', function() {
    describe('without entry', function() {
      beforeEach(function() {
        entryName = 'invalid';
      });

      it('calls the callback with null', function(done) {
        cache.read(entryName, function(data) {
          expect(data).to.be.null;
          done();
        });
      });
    });

    describe('with a valid entry', function() {
      beforeEach(function(done) {
        cache.write(entryName, entryValue);
        client.get(cache.cacheKey(entryName), function(err, value) {
          expect(value).to.not.be.null;
          done();
        });
      });

      it('returns the persisted value', function(done) {
        cache.read(entryName, function(data) {
          expect(data).to.equal(entryValue);
          done();
        });
      });
    });

    describe('with expired entry', function() {
      beforeEach(function(done) {
        cache.write(entryName, entryValue);
        client.expire(cache.cacheKey(entryName), 0);

        client.get(cache.cacheKey(entryName), function(err, value) {
          expect(value).to.not.be.null;
          done();
        });
      });

      it('calls the callback with null', function(done) {
        cache.read(entryName, function(data) {
          expect(data).to.be.null;
          done();
        });
      });<commit_msg>Improve node-redis-cache-store-test to use DEL not EXPIRE removing race condition<commit_after>require('./test-helper');
var rewire = require("rewire");
var NodeRedisCacheStore = rewire('../../src/node-redis-cache-store');

describe('NodeRedisCacheStore', function() {
  var cache,
      entryName,
      entryValue,
      client,
      restore;

  var fakeredis = require('fakeredis');
  var redis = require('redis');

  beforeEach(function(done) {
    client = fakeredis.createClient();
    client.flushdb(function(err) { done() });

    sinon.stub(redis, 'createClient', function() { return client });
    restore = NodeRedisCacheStore.__set__('redis', redis);

    entryName = 'test-redis';
    entryValue = 'test-redis';

    cache = new NodeRedisCacheStore();
    cache.logger = false;
  });

  afterEach(function() {
    redis.createClient.restore();
    restore();
  });

  describe('constructor', function() {
    var opts, logger, onErrorCallback;

    beforeEach(function() {
      logger = 'my-logger';
      opts = {logger: logger, client: [6379, '127.0.0.1', {max_attempts: 5}]};
      cache = new NodeRedisCacheStore({redis: opts});
    });

    it('holds a reference of redis options at "redisOptions" attribute', function() {
      expect(cache.redisOptions).to.deep.equal(opts);
    });

    it('holds a reference of redis client at "storage"', function() {
      expect(cache.storage).to.eql(client)
    });

    it('accepts a custom logger at "redis.logger" attribute', function() {
      expect(cache.logger).to.equal(logger);
    });

    it('calls method "auth" if attribute "redis.password" is defined', function() {
      var password = '<PASSWORD>';
      sinon.spy(client, 'auth');
      cache = new NodeRedisCacheStore({redis: {password: password}});
      expect(client.auth).to.have.been.calledWith(password);
      client.auth.restore();
    });
  });

  describe('#read', function() {
    describe('without entry', function() {
      beforeEach(function() {
        entryName = 'invalid';
      });

      it('calls the callback with null', function(done) {
        cache.read(entryName, function(data) {
          expect(data).to.be.null;
          done();
        });
      });
    });

    describe('with a valid entry', function() {
      beforeEach(function(done) {
        cache.write(entryName, entryValue);
        client.get(cache.cacheKey(entryName), function(err, value) {
          expect(value).to.not.be.null;
          done();
        });
      });

      it('returns the persisted value', function(done) {
        cache.read(entryName, function(data) {
          expect(data).to.equal(entryValue);
          done();
        });
      });
    });

    describe('with expired entry', function() {
      beforeEach(function(done) {
        cache.write(entryName, entryValue);
        client.del(cache.cacheKey(entryName));

        client.get(cache.cacheKey(entryName), function(err, value) {
          expect(value).to.be.null;
          done();
        });
      });

      it('calls the callback with null', function(done) {
        cache.read(entryName, function(data) {
          expect(data).to.be.null;
          done();
        });
      });
{'fraction_non_alphanumeric': 0.1598736176935229, 'fraction_numerical': 0.0036334913112164298, 'mean_word_length': 2.7550415183867143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3093269', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1878, 'n_words': 372}