NineSec Team Shell
Server IP : 92.205.26.207  /  Your IP : 216.73.216.16
Web Server : Apache
System : Linux 207.26.205.92.host.secureserver.net 4.18.0-553.60.1.el8_10.x86_64 #1 SMP Thu Jul 10 04:01:16 EDT 2025 x86_64
User : zikryat ( 1002)
PHP Version : 8.3.23
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/zikryat/public_html/node_modules/lru-memoizer/test/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/zikryat/public_html/node_modules/lru-memoizer/test/lru-memoizer.itemmaxage.test.js
var memoizer = require('./..');
var assert = require('chai').assert;

describe('lru-memoizer (itemMaxAge)', function () {
  var loadTimes = 0, memoized;

  beforeEach(function () {
    loadTimes = 0;
  });

  it('should use default behavior if not configured', function (done) {
    memoized = memoizer({
      load: function (a, b, callback) {
        loadTimes++;
        setTimeout(function () {
          callback(null, a + b);
        }, 100);
      },
      hash: function (a, b) {
        return a + '-' + b;
      },
      max: 10,
      maxAge: 500
    });

    memoized(1,2, function (err, result) {
      assert.isNull(err);
      assert.strictEqual(result, 3);
      assert.strictEqual(loadTimes, 1);

      // Not expired yet.
      setTimeout(function() {
        memoized(1,2, function (err, result) {
          assert.isNull(err);
          assert.strictEqual(result, 3);
          assert.strictEqual(loadTimes, 1);

          // Expired, load times will increase.
          setTimeout(function() {
            memoized(1,2, function (err, result) {
              assert.isNull(err);
              assert.strictEqual(result, 3);
              assert.strictEqual(loadTimes, 2);
              done();
            });
          }, 200);
        });
      }, 400);
    });
  });

  it('should return all args and the result in the itemMaxAge function', function (done) {
    var args;
    memoized = memoizer({
      load: function (a, b, callback) {
        loadTimes++;
        setTimeout(function () {
          callback(null, a + b);
        }, 100);
      },
      itemMaxAge: function (a, b, result) {
        args = arguments;
        return 1000;
      },
      hash: function (a, b) {
        return a + '-' + b;
      },
      max: 10,
      maxAge: 600
    });

    memoized(1,2, function (err, result) {
      assert.isNull(err);
      assert.strictEqual(args[0], 1);
      assert.strictEqual(args[1], 2);
      assert.strictEqual(args[2], 3);
      done();
    });
  });

  it('should overwrite the default behavior if configured', function (done) {
    var maxAge = 0;
    var lastKey = null;
    memoized = memoizer({
      load: function (a, b, callback) {
        loadTimes++;
        setTimeout(function () {
          callback(null, a + b);
        }, 100);
      },
      itemMaxAge: function (a, b, result) {
        lastKey = a + '-' + b;
        // In this test, we set the maxAge of the current item to (result*100).
        // If the result is 3, the max age of this item will be 300.
        maxAge = result * 100;
        return maxAge;
      },
      hash: function (a, b) {
        return a + '-' + b;
      },
      max: 10,
      maxAge: 600
    });

    memoized(1,2, function (err, result) {
      assert.isNull(err);
      assert.strictEqual(maxAge, 300);
      assert.strictEqual(lastKey, '1-2');
      assert.strictEqual(result, 3);
      assert.strictEqual(loadTimes, 1);

      // Not expired yet after 200 ms, because the expiration is 300
      setTimeout(function() {
        memoized(1,2, function (err, result) {
          assert.isNull(err);
          assert.strictEqual(maxAge, 300);
          assert.strictEqual(lastKey, '1-2');
          assert.strictEqual(result, 3);
          assert.strictEqual(loadTimes, 1);

          // Expired because now we are at 350 ms (even though gloabl expiration has been set to 600)
          setTimeout(function() {
            memoized(1,2, function (err, result) {
              assert.isNull(err);
              assert.strictEqual(maxAge, 300);
              assert.strictEqual(lastKey, '1-2');
              assert.strictEqual(result, 3);
              assert.strictEqual(loadTimes, 2);

              // Expired again, because 350ms have passed again.
              setTimeout(function() {
                memoized(1,2, function (err, result) {
                  assert.isNull(err);
                  assert.strictEqual(maxAge, 300);
                  assert.strictEqual(lastKey, '1-2');
                  assert.strictEqual(result, 3);
                  assert.strictEqual(loadTimes, 3);
                  done();
                });
              }, 350);
            });
          }, 150);
        });
      }, 200);
    });
  });

  it('should overwrite the default behavior if configured (sync)', function (done) {
    var maxAge = 0;
    var lastKey = null;
    memoized = memoizer.sync({
      load: function (a, b) {
        loadTimes++;
        return a + b;
      },
      itemMaxAge: function (a, b, result) {
        lastKey = a + '-' + b;
        // In this test, we set the maxAge of the current item to (result*100).
        // If the result is 3, the max age of this item will be 300.
        maxAge = result * 100;
        return maxAge;
      },
      hash: function (a, b) {
        return a + '-' + b;
      },
      max: 10,
      maxAge: 600
    });

    var result = memoized(1, 2);
    assert.strictEqual(maxAge, 300);
    assert.strictEqual(lastKey, '1-2');
    assert.strictEqual(result, 3);
    assert.strictEqual(loadTimes, 1);

    // Not expired yet after 200 ms, because the expiration is 300
    setTimeout(function() {
      result = memoized(1, 2);
      assert.strictEqual(maxAge, 300);
      assert.strictEqual(lastKey, '1-2');
      assert.strictEqual(result, 3);
      assert.strictEqual(loadTimes, 1);

      // Expired because now we are at 350 ms (even though gloabl expiration has been set to 600)
      setTimeout(function() {
        result = memoized(1,2);
        assert.strictEqual(maxAge, 300);
        assert.strictEqual(lastKey, '1-2');
        assert.strictEqual(result, 3);
        assert.strictEqual(loadTimes, 2);

          // Expired again, because 350ms have passed again.
          setTimeout(function() {
            result = memoized(1,2);
            assert.strictEqual(maxAge, 300);
            assert.strictEqual(lastKey, '1-2');
            assert.strictEqual(result, 3);
            assert.strictEqual(loadTimes, 3);
            done();
          }, 350);
      }, 150);
    }, 200);
  });
});

NineSec Team - 2022