<commit_before>/* global describe, it, beforeEach, afterEach */
'use strict';
const join = require('path').join;
const readFileSync = require('fs').readFileSync;
const InstanceManager = require('../InstanceManager.js');
const arangojs = require('arangojs');
const expect = require('chai').expect;

const noop = () => {};
const service1 = readFileSync(
  join(__dirname, '..', 'fixtures', 'service1.zip')
);
const service2 = readFileSync(
  join(__dirname, '..', 'fixtures', 'service2.zip')
);

describe('Foxx service', function() {
  const im = new InstanceManager();
  const MOUNT = '/resiliencetestservice';

  beforeEach(() => im.startCluster(1, 2, 2));
  afterEach(() => im.cleanup().catch(noop));

  describe('when already installed', function() {
    beforeEach(function() {
      const coord = im.coordinators()[1];
      const db = arangojs(im.getEndpointUrl(coord));
      return db
        .installService(MOUNT, service1)
        .then(() => db.route(MOUNT).get())
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it('should survive a single coordinator being rebooted', function() {
      const coord = im.coordinators()[0];
      const db = arangojs(im.getEndpointUrl(coord));
      return im
        .kill(coord)
        .then(() => im.restart(coord))
        .then(() => db.route(MOUNT).get())
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it('should survive a single coordinator being replaced', function() {
      const coord1 = im.coordinators()[0];
      return im
        .destroy(coord1)
        .then(() => im.replace(coord1))
        .then(coord2 => {
          const db = arangojs(im.getEndpointUrl(coord2));
          return db.route(MOUNT).get();
        })
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it('should survive a single coordinator being added', function() {
      return im
        .startCoordinator('coordinator-new')
        .then(instance => im.waitForInstance(instance))
        .then(instance => {
          im.instances = [...im.instances, instance];
          const db = arangojs(im.getEndpointUrl(instance));
          return db.route(MOUNT).get();
        })
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it.skip('should survive all coordinators being replaced', function() {
      const instances = im.coordinators();
      return Promise.all(instances.map(instance => im.destroy(instance)))
        .then(() =>
          Promise.all(instances.map(instance => im.replace(instance)))
        )
        .then(instances => {
          const coord = instances[0];
          const db = arangojs(im.getEndpointUrl(coord));
          return db.route(MOUNT).get();
        })<commit_msg>Finally :tada:<commit_after>/* global describe, it, beforeEach, afterEach */
'use strict';
const join = require('path').join;
const readFileSync = require('fs').readFileSync;
const InstanceManager = require('../InstanceManager.js');
const arangojs = require('arangojs');
const expect = require('chai').expect;

const noop = () => {};
const service1 = readFileSync(
  join(__dirname, '..', 'fixtures', 'service1.zip')
);
const service2 = readFileSync(
  join(__dirname, '..', 'fixtures', 'service2.zip')
);

describe('Foxx service', function() {
  const im = new InstanceManager();
  const MOUNT = '/resiliencetestservice';

  beforeEach(() => im.startCluster(1, 2, 2));
  afterEach(() => im.cleanup().catch(noop));

  describe('when already installed', function() {
    beforeEach(function() {
      const coord = im.coordinators()[1];
      const db = arangojs(im.getEndpointUrl(coord));
      return db
        .installService(MOUNT, service1)
        .then(() => db.route(MOUNT).get())
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it('should survive a single coordinator being rebooted', function() {
      const coord = im.coordinators()[0];
      const db = arangojs(im.getEndpointUrl(coord));
      return im
        .kill(coord)
        .then(() => im.restart(coord))
        .then(() => db.route(MOUNT).get())
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it('should survive a single coordinator being replaced', function() {
      const coord1 = im.coordinators()[0];
      return im
        .destroy(coord1)
        .then(() => im.replace(coord1))
        .then(coord2 => {
          const db = arangojs(im.getEndpointUrl(coord2));
          return db.route(MOUNT).get();
        })
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it('should survive a single coordinator being added', function() {
      return im
        .startCoordinator('coordinator-new')
        .then(instance => im.waitForInstance(instance))
        .then(instance => {
          im.instances = [...im.instances, instance];
          const db = arangojs(im.getEndpointUrl(instance));
          return db.route(MOUNT).get();
        })
        .then(response => {
          expect(response).to.have.property('body', 'service1');
        });
    });

    it('should survive all coordinators being replaced', function() {
      const instances = im.coordinators();
      return Promise.all(instances.map(instance => im.destroy(instance)))
        .then(() =>
          Promise.all(instances.map(instance => im.replace(instance)))
        )
        .then(instances => {
          const coord = instances[0];
          const db = arangojs(im.getEndpointUrl(coord));
          return db.route(MOUNT).get();
        })
{'fraction_non_alphanumeric': 0.16746822397801442, 'fraction_numerical': 0.007214015802129852, 'mean_word_length': 2.7303010890454837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5723724', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1682, 'n_words': 345}