{"size": 1448, "ext": "js", "max_stars_repo_path": "test/consume.js", "max_stars_repo_name": "StrictlySkyler/cockroach", "max_stars_repo_stars_event_min_datetime": "2021-04-22T04:41:27.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-22T04:41:27.000Z", "max_issues_repo_path": "test/consume.js", "max_issues_repo_name": "StrictlySkyler/cockroach", "max_issues_repo_issues_event_min_datetime": "2017-10-19T22:58:15.000Z", "max_issues_repo_issues_event_max_datetime": "2019-05-22T11:10:33.000Z", "max_forks_repo_path": "test/consume.js", "max_forks_repo_name": "StrictlySkyler/cockroach", "max_forks_repo_forks_event_min_datetime": "2017-10-28T04:57:51.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-06T22:52:24.000Z"}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": 2, "avg_line_length": 24.9655172414, "max_line_length": 77, "alphanum_fraction": 0.570441989}
import assert from 'assert';
import { consume, starve } from '../src';
import { decorate_producer } from '../src/produce';
import { decorate_consumer } from '../src/consume';

let run = () => {
  return '';
};
let spawn = () => ({
  on: () => spawn(),
  stdout: { pipe: () => spawn(), on: () => spawn(), destroy: () => spawn() },
  stderr: { pipe: () => spawn(), on: () => spawn(), destroy: () => spawn() },
  kill: () => {},
});
const topic = 'news';

describe('consuming', () => {
  beforeEach(() => {
    decorate_producer(run);
    decorate_consumer(run, spawn);
  });

  afterEach(() => {
    starve(topic);
  });

  it('needs at least one topic and some work', () => {
    assert(consume(topic, run));
    assert(consume([topic, topic], run));
    assert.throws(consume);
    assert.throws(() => consume(topic));
  });

  it('registers a consumer', () => {
    const registry = consume(topic, run);
    assert(Object.keys(registry).length == 1);
  });

});

describe('starving', () => {

  it('needs a consumed topic', () => {
    assert(() => starve(topic));
    assert.throws(starve);
    consume(topic, run);
    assert(starve(topic));
  });

  it('can stop a specific consumer', () => {
    const consumer_id = Object.keys(consume(topic, run))[0];
    const another_id = Object.keys(consume(topic, run))[1];
    const registry = starve(topic, consumer_id);
    assert(registry[another_id]);
    assert(! registry[consumer_id]);
  });
});
