HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/tana/frontend/node_modules/fork-stream/test/tests.js
var assert = require("chai").assert;

var ForkStream = require("../");

describe("fork-stream", function() {
  it("should split objects into their correct streams", function(done) {
    var fork = new ForkStream({
      classifier: function classify(e, done) {
        return done(null, e >= 5);
      },
    });

    var expectedA = [5, 7, 9],
        expectedB = [1, 4, 3, 1];

    var actualA = [],
        actualB = [];

    fork.a.on("data", function(e) {
      actualA.push(e);
    });

    fork.b.on("data", function(e) {
      actualB.push(e);
    });

    fork.on("finish", function() {
      assert.deepEqual(expectedA, actualA);
      assert.deepEqual(expectedB, actualB);

      return done();
    });

    [1, 5, 7, 4, 9, 3, 1].forEach(function(n) {
      fork.write(n);
    });

    fork.end();
  });

  it("should respect backpressure", function(done) {
    var fork = new ForkStream({
      highWaterMark: 2,
      classifier: function classify(e, done) {
        return done(null, e >= 5);
      },
    });

    var expected = [5, 7],
        actual = [];

    fork.a.on("data", function(e) {
      actual.push(e);
    });

    var timeout = setTimeout(function() {
      assert.deepEqual(expected, actual);

      return done();
    }, 10);

    fork.on("finish", function() {
      clearTimeout(timeout);

      return done(Error("should not finish"));
    });

    [1, 5, 7, 4, 9, 3, 1].forEach(function(n) {
      fork.write(n);
    });

    fork.end();
  });

  it("should end the outputs when the input finishes", function(done) {
    var fork = new ForkStream();

    var count = 0;
    var onEnd = function onEnd() {
      if (++count === 2) {
        return done();
      }
    };

    fork.a.on("end", onEnd)
    fork.b.on("end", onEnd);

    // start "flowing" mode
    fork.a.resume();
    fork.b.resume();

    fork.end();
  });
});