<commit_before>const chai = require('chai');
const chaiImmutable = require('chai-immutable');

chai.use(chaiImmutable);

import { dummyStore } from '../../utils';

import { ActionsObservable } from 'redux-observable';

const Immutable = require('immutable');

const fromJS = Immutable.fromJS;

const expect = chai.expect;

const sinon = require('sinon');

const Rx = require('rxjs/Rx');

const Observable = Rx.Observable;
import { EXECUTE_CELL } from '../../../src/notebook/constants';
import { executeCell } from '../../../src/notebook/actions';
import {
  reduceOutputs,
  executeCellObservable,
  executeCellEpic,
  createExecuteRequest,
  msgSpecToNotebookFormat,
  createPagerActions,
  createSourceUpdateAction,
  createCellAfterAction,
  createCellStatusAction,
  createExecuteCellObservable,
  updateCellNumberingAction,
  handleFormattableMessages,
} from '../../../src/notebook/epics/execute';

describe('executeCell', () => {
  it('returns an executeCell action', () => {
    expect(executeCell('0-0-0-0', 'import random; random.random()'))
      .to.deep.equal({
        type: EXECUTE_CELL,
        id: '0-0-0-0',
        source: 'import random; random.random()',
      });
  });
});

describe('reduceOutputs', () => {
  it('empties outputs when clear_output passed', () => {
    const outputs = Immutable.List([1,2,3]);
    const newOutputs = reduceOutputs(outputs, {output_type: 'clear_output'});
    expect(newOutputs.size).to.equal(0);
  })

  it('puts new outputs at the end by default', () => {
    const outputs = Immutable.List([1,2]);
    const newOutputs = reduceOutputs(outputs, 3)

    expect(newOutputs).to.equal(Immutable.List([1, 2, 3]));
  })

  it('merges streams of text', () => {
    const outputs = Immutable.fromJS([{name: 'stdout', text: 'hello', output_type: 'stream'}])
    const newOutputs = reduceOutputs(outputs, {name: 'stdout', text: ' world', output_type: 'stream' });

    expect(newOutputs).to.equal(Immutable.fromJS([{name: 'stdout', text: 'hello world', output_type: 'stream'}]));
  })

  it('keeps respective streams together', () => {
    const outputs = Immutable.fromJS([
      {name: 'stdout', text: 'hello', output_type: 'stream'},
      {name: 'stderr', text: 'errors are', output_type: 'stream'},
    ])
    const newOutputs = reduceOutputs(outputs, {name: 'stdout', text: ' world', output_type: 'stream' });

    expect(newOutputs).to.equal(Immutable.fromJS([
      {name: 'stdout', text: 'hello world', output_type: 'stream'},
      {name: 'stderr', text: 'errors are', output_type: 'stream'},
    ]));

    const evenNewerOutputs = reduceOutputs(newOutputs, {name: 'stderr', text: ' informative', output_type: 'stream' });
    expect(evenNewerOutputs).to.equal(Immutable.fromJS([
      {name: 'stdout', text: 'hello world', output_type: 'stream'},
      {name: 'stderr', text: 'errors are informative', output_type: 'stream'},
    ]));

  })
})

describe('executeCellObservable', () => {
  // TODO: Refactor executeCellObservable into separate testable observables
  it('is entirely too insane for me to test this well right this second', (done) => {
    const frontendToShell = new Rx.Subject();
    const shellToFrontend = new Rx.Subject();
    const mockShell = Rx.Subject.create(frontendToShell, shellToFrontend);
    const mockIOPub = new Rx.Subject();

    const channels = {
      shell: mockShell,
      iopub: mockIOPub,
    };

    // Expect message to have been sent
    frontendToShell
      .subscribe(msg => {
        expect(msg.header.msg_type).to.equal('execute_request');
        expect(msg.content.code).to.equal('import this');
      })

    const action$ = executeCellObservable(channels, '0', 'import this');

    action$
      .bufferCount(3)
      .subscribe(messages => {
        expect(messages).to.deep.equal([
          // TODO: Order doesn't actually matter here
          { type: 'UPDATE_CELL_STATUS', id: '0', status: 'busy' },
          { type: 'UPDATE_CELL_PAGERS', id: '0', pagers: Immutable.List() },
          { type: 'UPDATE_CELL_OUTPUTS', id: '0', outputs: Immutable.List() },
        ]);
        done(); // TODO: Make sure message check above is called
      })

  })

  it('outright rejects a lack of channels.shell and iopub', (done) => {
    const obs = executeCellObservable({}, '0', 'woo')
    obs.subscribe(null, (err) => {
        expect(err.message).to.equal('kernel not connected');
        done();
    })

  })
});

describe('createExecuteRequest', () => {
  it('creates an execute_request message', () => {
    const code = 'print("test")';
    const executeRequest = createExecuteRequest(code);

    expect(executeRequest.content.code).to.equal(code);
    expect(executeRequest.header.msg_type).to.equal('execute_request');
  });
});

describe('msgSpecToNotebookFormat', () => {
  it('converts a message to the notebook format', () => {
    const msg = {content: {data: 'test'}, header: {msg_type: 'test_header'}};
    const notebookSpecMsg = msgSpecToNotebookFormat(msg);

    expect(notebookSpecMsg).to.have.property('output_type');
    expect(notebookSpecMsg).to.have.property('data');
    expect(notebookSpecMsg.output_type).to.equal('test_header');
  });
});

describe('createPagerActions', () => {
  it('emits actions to set pagers', (done) => {
    const msgObs = Rx.Observable.from([{
      source: 'page',
      data: {'text/html': 'this is a test'},
    }]);

    const pagerAction$ = createPagerActions('1', msgObs);

    pagerAction$.subscribe((action) => {
      const expected = [{ source: 'page', data: { 'text/html': 'this is a test' } } ];
      expect(action.id).to.equal('1');
      expect(action.pagers.toJS()).to.deep.equal(expected);
      done();
    });
  });
});

describe('createCellAfterAction', () => {
  it('emits a createCellAfter action', (done) => {
    const msgObs = Rx.Observable.from([{
      source: 'set_next_input',
      text: 'This is some test text.',
      replace: false,
    }]);

    const cellAction$ = createCellAfterAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.id).to.equal('1');
      done();
    });
  });
});

describe('createCellStatusAction', () => {
  it('emits an updateCellStatus action', (done) => {
    const msgObs = Rx.Observable.from([{
      header: {
        msg_id: '123',
        msg_type: 'status',
      },
      parent_header: {},
      content: {
        'execution_state': 'idle',
      },
      metadata: {},
    }]);

    const cellAction$ = createCellStatusAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.id).to.equal('1');
      expect(action.status).to.equal('idle');
      done();
    });
  });
});

describe('updateCellNumberingAction', () => {
  it('emits updateCellExecutionCount action', (done) => {
    const msgObs = Rx.Observable.from([{
      header: {
        msg_id: '123',
        msg_type: 'execute_input',
      },
      parent_header: {},
      content: {
        'execution_count': 3,
      },
      metadata: {},
    }]);

    const cellAction$ = updateCellNumberingAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.id).to.equal('1');
      expect(action.count).to.equal(3);
      done();
    });
  });
});

describe('createSourceUpdateAction', () => {
  it('emits updateCellSource action', (done) => {
    const msgObs = Rx.Observable.from([{
      source: 'set_next_input',
      text: 'This is some test text.',
      replace: true,
    }]);

    const cellAction$ = createSourceUpdateAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.source).to.equal('This is some test text.');
      done();
    });
  });
});
describe('createExecuteCellObservable', () => {
  let store = { getState: function() { return this.state; },
            state: {
              app: {
                executionState: 'starting',
                channels: 'channelInfo',
                notificationSystem: {
                  addNotification: sinon.spy(),
                },
              }
            },
          };
  const action$ = new ActionsObservable();
  it('notifies the user if kernel is not connected', () => {
    const testFunction = createExecuteCellObservable(action$, store, 'source', 'id');
    const notification = store.getState().app.notificationSystem.addNotification;
    expect(notification).to.be.calledWith({
      title: 'Could not execute cell',
      message: 'The cell could not be executed because the kernel is not connected.',
      level: 'error',
    });
    expect(testFunction.subscribe).to.not.be.null;
  });
  it('emits returns an observable when kernel connected', () => {
    store.state.app.executionState = 'started'
    const executeCellObservable = createExecuteCellObservable(action$, store, 'source', 'id');
    expect(executeCellObservable.subscribe).to.not.be.null;
  });
})

describe('executeCellEpic', () => {
  const store = { getState: function() { return this.state; },
            state: {
              app: {
                executionState: 'starting',
                channels: 'channelInfo',
                notificationSystem: {
                  addNotification: sinon.spy(),
                },
                token: 'blah'
              }
            },
          };
  it('Errors on a bad action', (done) => {
    const badInput$ = Observable.of({ type: EXECUTE_CELL });
    const badAction$ = new ActionsObservable(badInput$);
    const actionBuffer = [];
    const responseActions = executeCellEpic(badAction$, store).catch(error => {
      expect(error.message).to.equal('execute cell needs an id');
    });
    const subscription = responseActions.subscribe(
      actionBuffer.push, // Every action that goes through should get stuck on an array
      (err) => expect.fail(err, null), // It should not error in the stream
      () => {
        expect(actionBuffer).to.deep.equal([]);
        done();
      },
    );
  });
  it('Errors on an action where source not a string', (done) => {
    const badInput$ = Observable.of(executeCell('id', 2));
    const badAction$ = new ActionsObservable(badInput$);
    const actionBuffer = [];
    const responseActions = executeCellEpic(badAction$, store).catch(error=> {
      expect(error.message).to.equal('execute cell needs source string');
    });
    const subscription = responseActions.subscribe(
      actionBuffer.push, // Every action that goes through should get stuck on an array
      (err) => expect.fail(err, null), // It should not error in the stream
      () => {
        expect(actionBuffer).to.deep.equal([]);
        done();
      }
    );
  });
  it('Runs an epic with the approriate flow with good action', (done) => {
    const input$ = Observable.of(executeCell('id', 'source'));
    const action$ = new ActionsObservable(input$);
    const actionBuffer = [];
    const responseActions = executeCellEpic(action$, store);
    const subscription = responseActions.subscribe(
      actionBuffer.push, // Every action that goes through should get stuck on an array
      (err) => expect.fail(err, null), // It should not error in the stream
      () => {
        expect(actionBuffer).to.deep.equal([]);
        done();
      },
    );
  });
})
<commit_msg>chore(Epics): Fix broken epics tests.<commit_after>const chai = require('chai');
const chaiImmutable = require('chai-immutable');

chai.use(chaiImmutable);

import { dummyStore } from '../../utils';

import { ActionsObservable } from 'redux-observable';

const Immutable = require('immutable');

const fromJS = Immutable.fromJS;

const expect = chai.expect;

const sinon = require('sinon');

const Rx = require('rxjs/Rx');

const Observable = Rx.Observable;
import {
  EXECUTE_CELL,
  UPDATE_CELL_EXECUTION_COUNT,
  ERROR_EXECUTING,
 } from '../../../src/notebook/constants';

import { executeCell } from '../../../src/notebook/actions';
import {
  reduceOutputs,
  executeCellObservable,
  executeCellEpic,
  createExecuteRequest,
  msgSpecToNotebookFormat,
  createPagerActions,
  createSourceUpdateAction,
  createCellAfterAction,
  createCellStatusAction,
  createExecuteCellObservable,
  updateCellNumberingAction,
  handleFormattableMessages,
} from '../../../src/notebook/epics/execute';

describe('executeCell', () => {
  it('returns an executeCell action', () => {
    expect(executeCell('0-0-0-0', 'import random; random.random()'))
      .to.deep.equal({
        type: EXECUTE_CELL,
        id: '0-0-0-0',
        source: 'import random; random.random()',
      });
  });
});

describe('reduceOutputs', () => {
  it('empties outputs when clear_output passed', () => {
    const outputs = Immutable.List([1,2,3]);
    const newOutputs = reduceOutputs(outputs, {output_type: 'clear_output'});
    expect(newOutputs.size).to.equal(0);
  })

  it('puts new outputs at the end by default', () => {
    const outputs = Immutable.List([1,2]);
    const newOutputs = reduceOutputs(outputs, 3)

    expect(newOutputs).to.equal(Immutable.List([1, 2, 3]));
  })

  it('merges streams of text', () => {
    const outputs = Immutable.fromJS([{name: 'stdout', text: 'hello', output_type: 'stream'}])
    const newOutputs = reduceOutputs(outputs, {name: 'stdout', text: ' world', output_type: 'stream' });

    expect(newOutputs).to.equal(Immutable.fromJS([{name: 'stdout', text: 'hello world', output_type: 'stream'}]));
  })

  it('keeps respective streams together', () => {
    const outputs = Immutable.fromJS([
      {name: 'stdout', text: 'hello', output_type: 'stream'},
      {name: 'stderr', text: 'errors are', output_type: 'stream'},
    ])
    const newOutputs = reduceOutputs(outputs, {name: 'stdout', text: ' world', output_type: 'stream' });

    expect(newOutputs).to.equal(Immutable.fromJS([
      {name: 'stdout', text: 'hello world', output_type: 'stream'},
      {name: 'stderr', text: 'errors are', output_type: 'stream'},
    ]));

    const evenNewerOutputs = reduceOutputs(newOutputs, {name: 'stderr', text: ' informative', output_type: 'stream' });
    expect(evenNewerOutputs).to.equal(Immutable.fromJS([
      {name: 'stdout', text: 'hello world', output_type: 'stream'},
      {name: 'stderr', text: 'errors are informative', output_type: 'stream'},
    ]));

  })
})

describe('executeCellObservable', () => {
  // TODO: Refactor executeCellObservable into separate testable observables
  it('is entirely too insane for me to test this well right this second', (done) => {
    const frontendToShell = new Rx.Subject();
    const shellToFrontend = new Rx.Subject();
    const mockShell = Rx.Subject.create(frontendToShell, shellToFrontend);
    const mockIOPub = new Rx.Subject();

    const channels = {
      shell: mockShell,
      iopub: mockIOPub,
    };

    // Expect message to have been sent
    frontendToShell
      .subscribe(msg => {
        expect(msg.header.msg_type).to.equal('execute_request');
        expect(msg.content.code).to.equal('import this');
      })

    const action$ = executeCellObservable(channels, '0', 'import this');

    action$
      .bufferCount(3)
      .subscribe(messages => {
        expect(messages).to.deep.equal([
          // TODO: Order doesn't actually matter here
          { type: 'UPDATE_CELL_STATUS', id: '0', status: 'busy' },
          { type: 'UPDATE_CELL_PAGERS', id: '0', pagers: Immutable.List() },
          { type: 'UPDATE_CELL_OUTPUTS', id: '0', outputs: Immutable.List() },
        ]);
        done(); // TODO: Make sure message check above is called
      })

  })

  it('outright rejects a lack of channels.shell and iopub', (done) => {
    const obs = executeCellObservable({}, '0', 'woo')
    obs.subscribe(null, (err) => {
        expect(err.message).to.equal('kernel not connected');
        done();
    })

  })
});

describe('createExecuteRequest', () => {
  it('creates an execute_request message', () => {
    const code = 'print("test")';
    const executeRequest = createExecuteRequest(code);

    expect(executeRequest.content.code).to.equal(code);
    expect(executeRequest.header.msg_type).to.equal('execute_request');
  });
});

describe('msgSpecToNotebookFormat', () => {
  it('converts a message to the notebook format', () => {
    const msg = {content: {data: 'test'}, header: {msg_type: 'test_header'}};
    const notebookSpecMsg = msgSpecToNotebookFormat(msg);

    expect(notebookSpecMsg).to.have.property('output_type');
    expect(notebookSpecMsg).to.have.property('data');
    expect(notebookSpecMsg.output_type).to.equal('test_header');
  });
});

describe('createPagerActions', () => {
  it('emits actions to set pagers', (done) => {
    const msgObs = Rx.Observable.from([{
      source: 'page',
      data: {'text/html': 'this is a test'},
    }]);

    const pagerAction$ = createPagerActions('1', msgObs);

    pagerAction$.subscribe((action) => {
      const expected = [{ source: 'page', data: { 'text/html': 'this is a test' } } ];
      expect(action.id).to.equal('1');
      expect(action.pagers.toJS()).to.deep.equal(expected);
      done();
    });
  });
});

describe('createCellAfterAction', () => {
  it('emits a createCellAfter action', (done) => {
    const msgObs = Rx.Observable.from([{
      source: 'set_next_input',
      text: 'This is some test text.',
      replace: false,
    }]);

    const cellAction$ = createCellAfterAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.id).to.equal('1');
      done();
    });
  });
});

describe('createCellStatusAction', () => {
  it('emits an updateCellStatus action', (done) => {
    const msgObs = Rx.Observable.from([{
      header: {
        msg_id: '123',
        msg_type: 'status',
      },
      parent_header: {},
      content: {
        'execution_state': 'idle',
      },
      metadata: {},
    }]);

    const cellAction$ = createCellStatusAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.id).to.equal('1');
      expect(action.status).to.equal('idle');
      done();
    });
  });
});

describe('updateCellNumberingAction', () => {
  it('emits updateCellExecutionCount action', (done) => {
    const msgObs = Rx.Observable.from([{
      header: {
        msg_id: '123',
        msg_type: 'execute_input',
      },
      parent_header: {},
      content: {
        'execution_count': 3,
      },
      metadata: {},
    }]);

    const cellAction$ = updateCellNumberingAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.id).to.equal('1');
      expect(action.count).to.equal(3);
      done();
    });
  });
});

describe('createSourceUpdateAction', () => {
  it('emits updateCellSource action', (done) => {
    const msgObs = Rx.Observable.from([{
      source: 'set_next_input',
      text: 'This is some test text.',
      replace: true,
    }]);

    const cellAction$ = createSourceUpdateAction('1', msgObs);

    cellAction$.subscribe((action) => {
      expect(action.source).to.equal('This is some test text.');
      done();
    });
  });
});
describe('createExecuteCellObservable', () => {
  let store = { getState: function() { return this.state; },
            state: {
              app: {
                executionState: 'starting',
                channels: 'channelInfo',
                notificationSystem: {
                  addNotification: sinon.spy(),
                },
              }
            },
          };
  const action$ = new ActionsObservable();
  it('notifies the user if kernel is not connected', () => {
    const testFunction = createExecuteCellObservable(action$, store, 'source', 'id');
    const notification = store.getState().app.notificationSystem.addNotification;
    expect(notification).to.be.calledWith({
      title: 'Could not execute cell',
      message: 'The cell could not be executed because the kernel is not connected.',
      level: 'error',
    });
    expect(testFunction.subscribe).to.not.be.null;
  });
  it('emits returns an observable when kernel connected', () => {
    store.state.app.executionState = 'started'
    const executeCellObservable = createExecuteCellObservable(action$, store, 'source', 'id');
    expect(executeCellObservable.subscribe).to.not.be.null;
  });
})

describe('executeCellEpic', () => {
  const store = { getState: function() { return this.state; },
            state: {
              app: {
                executionState: 'starting',
                channels: 'channelInfo',
                notificationSystem: {
                  addNotification: sinon.spy(),
                },
                token: 'blah'
              }
            },
          };
  it('Errors on a bad action', (done) => {
    const badInput$ = Observable.of({ type: EXECUTE_CELL });
    const badAction$ = new ActionsObservable(badInput$);
    const actionBuffer = [];
    const responseActions = executeCellEpic(badAction$, store).catch(error => {
      expect(error.message).to.equal('execute cell needs an id');
    });
    const subscription = responseActions.subscribe(
      (x) => actionBuffer.push(x.type), // Every action that goes through should get stuck on an array
      (err) => expect.fail(err, null), // It should not error in the stream
      () => {
        expect(actionBuffer).to.deep.equal([ERROR_EXECUTING]);
        done();
      },
    );
  });
  it('Errors on an action where source not a string', (done) => {
    const badInput$ = Observable.of(executeCell('id', 2));
    const badAction$ = new ActionsObservable(badInput$);
    const actionBuffer = [];
    const responseActions = executeCellEpic(badAction$, store).catch(error=> {
      expect(error.message).to.equal('execute cell needs source string');
    });
    const subscription = responseActions.subscribe(
      (x) => actionBuffer.push(x.type), // Every action that goes through should get stuck on an array
      (err) => expect.fail(err, null), // It should not error in the stream
      () => {
        expect(actionBuffer).to.deep.equal([ERROR_EXECUTING]);
        done();
      },
    );
  });
  it('Runs an epic with the approriate flow with good action', (done) => {
    const input$ = Observable.of(executeCell('id', 'source'));
    const action$ = new ActionsObservable(input$);
    const actionBuffer = [];
    const responseActions = executeCellEpic(action$, store);
    const subscription = responseActions.subscribe(
      (x) => actionBuffer.push(x.type), // Every action that goes through should get stuck on an array
      (err) => expect.fail(err, null), // It should not error in the stream
      () => {
        expect(actionBuffer).to.deep.equal([UPDATE_CELL_EXECUTION_COUNT]); // ;
        done();
      },
    );
  });
})
<|endoftext|>


<commit_before>"use strict";

var element        = require("../valid-element")
  , mutationMethod = require("./_mutation-method");

module.exports = function (node/*, …nodes*/) {
	element(this).insertBefore(mutationMethod(this.ownerDocument, arguments),
		this.firstChild);
};
<commit_msg>style: prettier<commit_after>"use strict";

var element        = require("../valid-element")
  , mutationMethod = require("./_mutation-method");

module.exports = function (node/*, …nodes*/) {
	element(this).insertBefore(mutationMethod(this.ownerDocument, arguments), this.firstChild);
};
<|endoftext|>


<commit_before>'use strict';

const _ = require('lodash/fp');
const express = require('express');
const {expect} = require('chai');
const psTree = require('ps-tree');
const tp = require('./helpers/test-phases');
const fx = require('./helpers/fixtures');
const fetch = require('node-fetch');
const retryPromise = require('retry-promise').default;
const hooks = require('./helpers/hooks');
const {outsideTeamCity} = require('./helpers/env-variables');
const {readFileSync} = require('fs');

describe('Aggregator: start', () => {
  let test, child;

  beforeEach(() => {
    test = tp.create();
    child = null;
  });

  afterEach(done => {
    test.teardown();
    killSpawnProcessAndHidChildren(done);
  });

  describe('tests', function () {
    it.only('should run tests initially', () => {
      child = test
        .setup({
          'src/test.spec.js': '',
          'src/client.js': '',
          'entry.js': '',
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()
        })
        .spawn('start');

      return checkStdout('Testing with Mocha');
    });
  });

  describe('--entry-point', () => {
    it('should run the entry point provided', () => {
      child = test
        .setup({
          'src/client.js': '',
          'entry.js': `console.log('hello world!')`,
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()
        })
        .spawn('start', '--entry-point=entry');

      return checkServerLogCreated().then(() =>
        expect(test.content('target/server.log')).to.contains('hello world!')
      );
    });

    it('should run index.js by default', () => {
      child = test
        .setup({
          'src/client.js': '',
          'index.js': `console.log('hello world!')`,
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()
        })
        .spawn('start');

      return checkServerLogCreated().then(() =>
        expect(test.content('target/server.log')).to.contains('hello world!')
      );
    });
  });

  describe('--no-server', () => {
    it('should not start a server if --no-server is passed', () => {
      child = test
        .setup({
          'src/assets/image.png': '',
          'index.js': `console.log('should not run');`,
          'package.json': fx.packageJson({servers: {cdn: {port: 3005}}}),
          '.babelrc': '{}'
        })
        .spawn('start', ['--no-server']);

      return cdnIsServing('assets/image.png')
        .then(() => expect(test.stdout).not.to.contain('should not run'));
    });
  });

  describe('--hot', () => {
    it('should create bundle with enabled hot module replacement', () => {
      child = test
        .setup({
          'src/client.js': `console.log('client-content');`,
          'index.js': `console.log('should run');`,
          'package.json': fx.packageJson({servers: {cdn: {port: 3005}}})
        })
        .spawn('start', ['--hot']);

      return cdnIsServing('app.bundle.js')
        .then(file => {
          file = file.replace(/\s+/g, ' ');
          expect(file)
            .to.include(`if (false) { throw new Error("[HMR] Hot Module Replacement is disabled."); }`)
            .and.include(`console.log('client-content');`)
            .and.not.include('Cannot find module');
        });
    });
  });

  describe('CDN server', () => {
    it('should run cdn server with default dir', () => {
      child = test
        .setup({
          'src/assets/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({servers: {cdn: {port: 3005}}})
        })
        .spawn('start');

      return cdnIsServing('assets/test.json');
    });

    it('should run cdn server with configured dir', () => {
      child = test
        .setup({
          'src/assets/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({servers: {cdn: {port: 3005, dir: 'dist/statics'}}})
        })
        .spawn('start');

      return cdnIsServing('assets/test.json');
    });

    it('should run cdn server from node_modules, on n-build project, using default dir', () => {
      child = test
        .setup({
          'node_modules/my-client-project/dist/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({clientProjectName: 'my-client-project', servers: {cdn: {port: 3005}}})
        })
        .spawn('start');

      return cdnIsServing('test.json');
    });

    it('should run cdn server from node_modules, on n-build project, using configured dir', () => {
      child = test
        .setup({
          'node_modules/my-client-project/dist/statics/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({clientProjectName: 'my-client-project', servers: {cdn: {port: 3005, dir: 'dist/statics'}}})
        })
        .spawn('start');

      return cdnIsServing('test.json');
    });

    it('should support cross origin requests headers', () => {
      child = test
        .setup({
          'package.json': fx.packageJson()
        })
        .spawn('start');

      return fetchCDN().then(res => {
        expect(res.headers.get('Access-Control-Allow-Methods')).to.equal('GET, OPTIONS');
        expect(res.headers.get('Access-Control-Allow-Origin')).to.equal('*');
      });
    });
  });

  describe('when the default port is taken', () => {
    let server;

    beforeEach(() => server = takePort(3000));
    afterEach(() => server.close());

    it('it should use the next available port', () => {
      child = test
        .setup({
          'index.js': `console.log('port', process.env.PORT)`,
          'package.json': fx.packageJson()
        })
        .spawn('start');

      return checkServerLogCreated()
        .then(() => expect(test.content('target/server.log')).to.contains('port 3001'));
    });
  });

  describe('Watch', function () {
    this.timeout(30000);

    describe('when using typescript', () => {
      it(`should rebuild and restart server after a file has been changed with typescript files`, () => {
        child = test
          .setup({
            'tsconfig.json': fx.tsconfig(),
            'src/server.ts': `declare var require: any; ${fx.httpServer('hello')}`,
            'src/config.ts': '',
            'src/client.ts': '',
            'index.js': `require('./dist/src/server')`,
            'package.json': fx.packageJson(),
            'pom.xml': fx.pom()
          })
          .spawn('start');

        return checkServerIsUp({max: 100})
          .then(() => checkServerIsRespondingWith('hello'))
          .then(() => test.modify('src/server.ts', `declare var require: any; ${fx.httpServer('world')}`))
          .then(() => checkServerIsRespondingWith('world'));
      });
    });

    describe('when using es6', () => {
      it(`should rebuild and restart server after a file has been changed`, () => {
        child = test
          .setup({
            'src/server.js': fx.httpServer('hello'),
            'src/config.js': '',
            'src/client.js': '',
            'index.js': `require('./src/server')`,
            'package.json': fx.packageJson(),
            'pom.xml': fx.pom(),
            '.babelrc': '{}'
          })
          .spawn('start');

        return checkServerIsUp()
          .then(() => checkServerIsRespondingWith('hello'))
          .then(() => test.modify('src/server.js', fx.httpServer('world')))
          .then(() => checkServerIsRespondingWith('world'));
      });
    });

    describe('when using no transpile', () => {
      it(`should rebuild and restart server after a file has been changed`, () => {
        child = test
          .setup({
            'src/server.js': fx.httpServer('hello'),
            'src/config.js': '',
            'src/client.js': '',
            'index.js': `require('./src/server')`,
            'package.json': fx.packageJson(),
            'pom.xml': fx.pom()
          })
          .spawn('start');

        return checkServerIsUp()
          .then(() => checkServerIsRespondingWith('hello'))
          .then(() => test.modify('src/server.js', fx.httpServer('world')))
          .then(() => checkServerIsRespondingWith('world'));
      });
    });

    it.skip('should make a new bundle after the file has beend changed', () => {
      test.setup({
        'index.js': fx.httpServer(),
        'src/client.js': 'require(\'./menu\').create();',
        'src/client.spec.js': 'require(\'./menu\').create();',
        'src/menu.js': 'module.exports.create = function () {console.log(\'Initializing the menu!\')}',
        'package.json': fx.pkgJsonWithBuild()
      }, [hooks.linkWixNodeBuild]).execute('build', '--bundle');
      child = test.spawn('start', '-w');

      return checkServerIsUp()
        .then(() => test.modify('src/client.js', content => 'const menu = ' + content))
        .then(() => checkServerRestarted())
        .then(() => {
          expect(test.content('dist/statics/main.bundle.js')).to.contain('const menu =');
          expect(test.list('dist')).to.contain('specs.bundle.js');
        });
    });
  });

  it('should update .nvmrc to relevant version as shown in dockerfile', () => {
    const nodeVersion = readFileSync(require.resolve('../templates/.nvmrc'), {encoding: 'utf-8'});
    child = test
      .setup({
        'src/test.spec.js': '',
        'src/client.js': '',
        'entry.js': '',
        'package.json': fx.packageJson(),
        'pom.xml': fx.pom()
      })
      .spawn('start', [], outsideTeamCity);

    return checkServerLogCreated().then(() =>
      expect(test.content('.nvmrc')).to.equal(nodeVersion)
    );
  });

  describe('Clean', () => {
    ['dist', 'target'].forEach(folderName =>
      it(`should remove "${folderName}" folder before building`, () => {
        child = test
          .setup({
            [`${folderName}/src/old.js`]: `const hello = "world!";`,
            'src/new.js': 'const world = "hello!";',
            'package.json': fx.packageJson(),
            '.babelrc': '{}'
          })
          .spawn('start');

        return checkServerLogCreated().then(() => {
          expect(test.stdout).to.contains(`Cleaning up '${folderName}'...`);
          expect(test.list(folderName)).to.not.include('old.js');
          expect(test.list('dist/src')).to.include('new.js');
        });
      })
    );
  });

  describe('when there are runtime errors', () => {
    it('should display a warning message on the terminal', () => {
      child = test
        .setup({
          'index.js': `throw new Error('wix:error')`,
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()

        })
        .spawn('start');

      return checkServerLogCreated()
        .then(wait(1000))
        .then(() => expect(test.stdout).to.contains(`There are errors! Please check ./target/server.log`));
    });
  });

  function killSpawnProcessAndHidChildren(done) {
    if (!child) {
      return done();
    }

    const pid = child.pid;

    psTree(pid, (err /*eslint handle-callback-err: 0*/, children) => {
      [pid].concat(children.map(p => p.PID)).forEach(tpid => {
        try {
          process.kill(tpid, 'SIGKILL');
        } catch (e) {}
      });

      child = null;
      done();
    });
  }

  function checkServerLogCreated() {
    return retryPromise({backoff: 100}, () =>
      test.contains('target/server.log') ?
        Promise.resolve() :
        Promise.reject()
    );
  }

  function checkStdout(str) {
    return retryPromise({backoff: 100}, () =>
      test.stdout.indexOf(str) > -1 ?
        Promise.resolve() :
        Promise.reject()
    );
  }

  function takePort(port) {
    return express().listen(port);
  }

  function fetchCDN(port) {
    port = port || 3200;
    return retryPromise({backoff: 100}, () => fetch(`http://localhost:${port}/`));
  }

  function cdnIsServing(name) {
    return retryPromise({backoff: 100}, () =>
      fetch(`http://localhost:3005/${name}`)
        .then(res => {
          expect(res.status).to.equal(200);
          return res.text();
        })
    );
  }

  function checkServerIsRespondingWith(expected) {
    return retryPromise({backoff: 1000}, () =>
      fetch(`http://localhost:${fx.defaultServerPort()}/`)
        .then(res => res.text())
        .then(body => body === expected ? Promise.resolve() : Promise.reject())
    );
  }

  function checkServerIsUp(opts) {
    return retryPromise(_.merge({backoff: 100}, opts), () =>
      fetch(`http://localhost:${fx.defaultServerPort()}/`)
    );
  }

  function checkServerIsDown() {
    return retryPromise({backoff: 10}, () =>
      new Promise((resolve, reject) => {
        fetch(`http://localhost:${fx.defaultServerPort()}/`).then(reject, resolve);
      }));
  }

  function checkServerRestarted() {
    return checkServerIsDown().then(() => checkServerIsUp());
  }

  function wait(time) {
    return () => new Promise(resolve => setTimeout(resolve, time));
  }
});
<commit_msg>restore tests<commit_after>'use strict';

const _ = require('lodash/fp');
const express = require('express');
const {expect} = require('chai');
const psTree = require('ps-tree');
const tp = require('./helpers/test-phases');
const fx = require('./helpers/fixtures');
const fetch = require('node-fetch');
const retryPromise = require('retry-promise').default;
const hooks = require('./helpers/hooks');
const {outsideTeamCity} = require('./helpers/env-variables');
const {readFileSync} = require('fs');

describe('Aggregator: start', () => {
  let test, child;

  beforeEach(() => {
    test = tp.create();
    child = null;
  });

  afterEach(done => {
    test.teardown();
    killSpawnProcessAndHidChildren(done);
  });

  describe('tests', function () {
    it('should run tests initially', () => {
      child = test
        .setup({
          'src/test.spec.js': '',
          'src/client.js': '',
          'entry.js': '',
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()
        })
        .spawn('start');

      return checkStdout('Testing with Mocha');
    });
  });

  describe('--entry-point', () => {
    it('should run the entry point provided', () => {
      child = test
        .setup({
          'src/client.js': '',
          'entry.js': `console.log('hello world!')`,
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()
        })
        .spawn('start', '--entry-point=entry');

      return checkServerLogCreated().then(() =>
        expect(test.content('target/server.log')).to.contains('hello world!')
      );
    });

    it('should run index.js by default', () => {
      child = test
        .setup({
          'src/client.js': '',
          'index.js': `console.log('hello world!')`,
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()
        })
        .spawn('start');

      return checkServerLogCreated().then(() =>
        expect(test.content('target/server.log')).to.contains('hello world!')
      );
    });
  });

  describe('--no-server', () => {
    it('should not start a server if --no-server is passed', () => {
      child = test
        .setup({
          'src/assets/image.png': '',
          'index.js': `console.log('should not run');`,
          'package.json': fx.packageJson({servers: {cdn: {port: 3005}}}),
          '.babelrc': '{}'
        })
        .spawn('start', ['--no-server']);

      return cdnIsServing('assets/image.png')
        .then(() => expect(test.stdout).not.to.contain('should not run'));
    });
  });

  describe('--hot', () => {
    it('should create bundle with enabled hot module replacement', () => {
      child = test
        .setup({
          'src/client.js': `console.log('client-content');`,
          'index.js': `console.log('should run');`,
          'package.json': fx.packageJson({servers: {cdn: {port: 3005}}})
        })
        .spawn('start', ['--hot']);

      return cdnIsServing('app.bundle.js')
        .then(file => {
          file = file.replace(/\s+/g, ' ');
          expect(file)
            .to.include(`if (false) { throw new Error("[HMR] Hot Module Replacement is disabled."); }`)
            .and.include(`console.log('client-content');`)
            .and.not.include('Cannot find module');
        });
    });
  });

  describe('CDN server', () => {
    it('should run cdn server with default dir', () => {
      child = test
        .setup({
          'src/assets/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({servers: {cdn: {port: 3005}}})
        })
        .spawn('start');

      return cdnIsServing('assets/test.json');
    });

    it('should run cdn server with configured dir', () => {
      child = test
        .setup({
          'src/assets/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({servers: {cdn: {port: 3005, dir: 'dist/statics'}}})
        })
        .spawn('start');

      return cdnIsServing('assets/test.json');
    });

    it('should run cdn server from node_modules, on n-build project, using default dir', () => {
      child = test
        .setup({
          'node_modules/my-client-project/dist/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({clientProjectName: 'my-client-project', servers: {cdn: {port: 3005}}})
        })
        .spawn('start');

      return cdnIsServing('test.json');
    });

    it('should run cdn server from node_modules, on n-build project, using configured dir', () => {
      child = test
        .setup({
          'node_modules/my-client-project/dist/statics/test.json': '{a: 1}',
          'src/index.js': 'var a = 1;',
          'package.json': fx.packageJson({clientProjectName: 'my-client-project', servers: {cdn: {port: 3005, dir: 'dist/statics'}}})
        })
        .spawn('start');

      return cdnIsServing('test.json');
    });

    it('should support cross origin requests headers', () => {
      child = test
        .setup({
          'package.json': fx.packageJson()
        })
        .spawn('start');

      return fetchCDN().then(res => {
        expect(res.headers.get('Access-Control-Allow-Methods')).to.equal('GET, OPTIONS');
        expect(res.headers.get('Access-Control-Allow-Origin')).to.equal('*');
      });
    });
  });

  describe('when the default port is taken', () => {
    let server;

    beforeEach(() => server = takePort(3000));
    afterEach(() => server.close());

    it('it should use the next available port', () => {
      child = test
        .setup({
          'index.js': `console.log('port', process.env.PORT)`,
          'package.json': fx.packageJson()
        })
        .spawn('start');

      return checkServerLogCreated()
        .then(() => expect(test.content('target/server.log')).to.contains('port 3001'));
    });
  });

  describe('Watch', function () {
    this.timeout(30000);

    describe('when using typescript', () => {
      it(`should rebuild and restart server after a file has been changed with typescript files`, () => {
        child = test
          .setup({
            'tsconfig.json': fx.tsconfig(),
            'src/server.ts': `declare var require: any; ${fx.httpServer('hello')}`,
            'src/config.ts': '',
            'src/client.ts': '',
            'index.js': `require('./dist/src/server')`,
            'package.json': fx.packageJson(),
            'pom.xml': fx.pom()
          })
          .spawn('start');

        return checkServerIsUp({max: 100})
          .then(() => checkServerIsRespondingWith('hello'))
          .then(() => test.modify('src/server.ts', `declare var require: any; ${fx.httpServer('world')}`))
          .then(() => checkServerIsRespondingWith('world'));
      });
    });

    describe('when using es6', () => {
      it(`should rebuild and restart server after a file has been changed`, () => {
        child = test
          .setup({
            'src/server.js': fx.httpServer('hello'),
            'src/config.js': '',
            'src/client.js': '',
            'index.js': `require('./src/server')`,
            'package.json': fx.packageJson(),
            'pom.xml': fx.pom(),
            '.babelrc': '{}'
          })
          .spawn('start');

        return checkServerIsUp()
          .then(() => checkServerIsRespondingWith('hello'))
          .then(() => test.modify('src/server.js', fx.httpServer('world')))
          .then(() => checkServerIsRespondingWith('world'));
      });
    });

    describe('when using no transpile', () => {
      it(`should rebuild and restart server after a file has been changed`, () => {
        child = test
          .setup({
            'src/server.js': fx.httpServer('hello'),
            'src/config.js': '',
            'src/client.js': '',
            'index.js': `require('./src/server')`,
            'package.json': fx.packageJson(),
            'pom.xml': fx.pom()
          })
          .spawn('start');

        return checkServerIsUp()
          .then(() => checkServerIsRespondingWith('hello'))
          .then(() => test.modify('src/server.js', fx.httpServer('world')))
          .then(() => checkServerIsRespondingWith('world'));
      });
    });

    it.skip('should make a new bundle after the file has beend changed', () => {
      test.setup({
        'index.js': fx.httpServer(),
        'src/client.js': 'require(\'./menu\').create();',
        'src/client.spec.js': 'require(\'./menu\').create();',
        'src/menu.js': 'module.exports.create = function () {console.log(\'Initializing the menu!\')}',
        'package.json': fx.pkgJsonWithBuild()
      }, [hooks.linkWixNodeBuild]).execute('build', '--bundle');
      child = test.spawn('start', '-w');

      return checkServerIsUp()
        .then(() => test.modify('src/client.js', content => 'const menu = ' + content))
        .then(() => checkServerRestarted())
        .then(() => {
          expect(test.content('dist/statics/main.bundle.js')).to.contain('const menu =');
          expect(test.list('dist')).to.contain('specs.bundle.js');
        });
    });
  });

  it('should update .nvmrc to relevant version as shown in dockerfile', () => {
    const nodeVersion = readFileSync(require.resolve('../templates/.nvmrc'), {encoding: 'utf-8'});
    child = test
      .setup({
        'src/test.spec.js': '',
        'src/client.js': '',
        'entry.js': '',
        'package.json': fx.packageJson(),
        'pom.xml': fx.pom()
      })
      .spawn('start', [], outsideTeamCity);

    return checkServerLogCreated().then(() =>
      expect(test.content('.nvmrc')).to.equal(nodeVersion)
    );
  });

  describe('Clean', () => {
    ['dist', 'target'].forEach(folderName =>
      it(`should remove "${folderName}" folder before building`, () => {
        child = test
          .setup({
            [`${folderName}/src/old.js`]: `const hello = "world!";`,
            'src/new.js': 'const world = "hello!";',
            'package.json': fx.packageJson(),
            '.babelrc': '{}'
          })
          .spawn('start');

        return checkServerLogCreated().then(() => {
          expect(test.stdout).to.contains(`Cleaning up '${folderName}'...`);
          expect(test.list(folderName)).to.not.include('old.js');
          expect(test.list('dist/src')).to.include('new.js');
        });
      })
    );
  });

  describe('when there are runtime errors', () => {
    it('should display a warning message on the terminal', () => {
      child = test
        .setup({
          'index.js': `throw new Error('wix:error')`,
          'package.json': fx.packageJson(),
          'pom.xml': fx.pom()

        })
        .spawn('start');

      return checkServerLogCreated()
        .then(wait(1000))
        .then(() => expect(test.stdout).to.contains(`There are errors! Please check ./target/server.log`));
    });
  });

  function killSpawnProcessAndHidChildren(done) {
    if (!child) {
      return done();
    }

    const pid = child.pid;

    psTree(pid, (err /*eslint handle-callback-err: 0*/, children) => {
      [pid].concat(children.map(p => p.PID)).forEach(tpid => {
        try {
          process.kill(tpid, 'SIGKILL');
        } catch (e) {}
      });

      child = null;
      done();
    });
  }

  function checkServerLogCreated() {
    return retryPromise({backoff: 100}, () =>
      test.contains('target/server.log') ?
        Promise.resolve() :
        Promise.reject()
    );
  }

  function checkStdout(str) {
    return retryPromise({backoff: 100}, () =>
      test.stdout.indexOf(str) > -1 ?
        Promise.resolve() :
        Promise.reject()
    );
  }

  function takePort(port) {
    return express().listen(port);
  }

  function fetchCDN(port) {
    port = port || 3200;
    return retryPromise({backoff: 100}, () => fetch(`http://localhost:${port}/`));
  }

  function cdnIsServing(name) {
    return retryPromise({backoff: 100}, () =>
      fetch(`http://localhost:3005/${name}`)
        .then(res => {
          expect(res.status).to.equal(200);
          return res.text();
        })
    );
  }

  function checkServerIsRespondingWith(expected) {
    return retryPromise({backoff: 1000}, () =>
      fetch(`http://localhost:${fx.defaultServerPort()}/`)
        .then(res => res.text())
        .then(body => body === expected ? Promise.resolve() : Promise.reject())
    );
  }

  function checkServerIsUp(opts) {
    return retryPromise(_.merge({backoff: 100}, opts), () =>
      fetch(`http://localhost:${fx.defaultServerPort()}/`)
    );
  }

  function checkServerIsDown() {
    return retryPromise({backoff: 10}, () =>
      new Promise((resolve, reject) => {
        fetch(`http://localhost:${fx.defaultServerPort()}/`).then(reject, resolve);
      }));
  }

  function checkServerRestarted() {
    return checkServerIsDown().then(() => checkServerIsUp());
  }

  function wait(time) {
    return () => new Promise(resolve => setTimeout(resolve, time));
  }
});
<|endoftext|>


<commit_before>import { expect } from 'chai';

import {
  dummyCommutable,
  dummy,
} from '../dummy-nb';

import {
  load,
  newNotebook,
  notebookLoaded,
  extractNewKernel,
  convertRawNotebook,
  LOAD,
  loadEpic,
} from '../../../src/notebook/epics/loading';
import { ActionsObservable } from 'redux-observable';
import Immutable from 'immutable';

const path = require('path');
const Rx = require('rxjs/Rx');
const Observable = Rx.Observable;

describe('load', () => {
  it('loads a notebook', () => {
    expect(load('mytest.ipynb'))
      .to.deep.equal({ type: 'LOAD', filename: 'mytest.ipynb' })
  })
})

describe('newNotebook', () => {
  it('creates a new notebook', () => {
    expect(newNotebook('python3', '/tmp'))
      .to.deep.equal({
        type: 'NEW_NOTEBOOK',
        kernelSpecName: 'python3',
        cwd: '/tmp',
      })
  })
})

describe('notebookLoaded', () => {
  it('sets a notebook', () => {
    expect(notebookLoaded('test', dummyCommutable))
      .to.deep.equal({
        type: 'SET_NOTEBOOK',
        filename: 'test',
        notebook: dummyCommutable,
      })
  })
})

describe('extractNewKernel', () => {
  it('extracts and launches the kernel from a notebook', () => {
    expect(extractNewKernel('/tmp/test.ipynb', dummyCommutable)).to.deep.equal({
      type: 'LAUNCH_KERNEL',
      kernelSpecName: 'python3',
      cwd: path.resolve('/tmp'),
    })
  })
})

describe('convertRawNotebook', () => {
  it('converts a raw notebook', () => {
    const converted = convertRawNotebook('/tmp/test.ipynb', dummy);
    expect(converted.filename).to.equal('/tmp/test.ipynb');

    const notebook = converted.notebook;
    expect(dummyCommutable.get('metadata').equals(notebook.get('metadata')))
      .to.be.true;
  })
});

describe('loadingEpic', () => {
  it('errors without a filename', (done) => {
    const input$ = Observable.of({ type: LOAD });
    const action$ = new ActionsObservable(input$);
    const actionBuffer = [];
    const responseActions = loadEpic(action$);
    responseActions.subscribe(
      (x) => actionBuffer.push(x.type),
      (err) => {
        expect(err.message).to.equal('load needs a filename')
        done();
      },
      () => {
        expect.fail();
      },
    )
  });
  it('errors when file cant be read', (done) => {
    const input$ = Observable.of({ type: LOAD , filename: 'file'});
    const action$ = new ActionsObservable(input$);
    const actionBuffer = [];
    const responseActions = loadEpic(action$);
    responseActions.subscribe(
      (x) => actionBuffer.push(x.type),
      (err) => expect.fail(),
      () => {
        expect(actionBuffer).to.deep.equal(['ERROR']);
        done();
      },
    )
  });
})
<commit_msg>chore(loadingEpic): Test newNotebookEpic.<commit_after>import { expect } from 'chai';

import {
  dummyCommutable,
  dummy,
} from '../dummy-nb';

import {
  load,
  newNotebook,
  notebookLoaded,
  extractNewKernel,
  convertRawNotebook,
  LOAD,
  loadEpic,
  NEW_NOTEBOOK,
  newNotebookEpic,
} from '../../../src/notebook/epics/loading';
import { ActionsObservable } from 'redux-observable';
import Immutable from 'immutable';

const path = require('path');
const Rx = require('rxjs/Rx');
const Observable = Rx.Observable;

describe('load', () => {
  it('loads a notebook', () => {
    expect(load('mytest.ipynb'))
      .to.deep.equal({ type: 'LOAD', filename: 'mytest.ipynb' })
  })
})

describe('newNotebook', () => {
  it('creates a new notebook', () => {
    expect(newNotebook('python3', '/tmp'))
      .to.deep.equal({
        type: 'NEW_NOTEBOOK',
        kernelSpecName: 'python3',
        cwd: '/tmp',
      })
  })
})

describe('notebookLoaded', () => {
  it('sets a notebook', () => {
    expect(notebookLoaded('test', dummyCommutable))
      .to.deep.equal({
        type: 'SET_NOTEBOOK',
        filename: 'test',
        notebook: dummyCommutable,
      })
  })
})

describe('extractNewKernel', () => {
  it('extracts and launches the kernel from a notebook', () => {
    expect(extractNewKernel('/tmp/test.ipynb', dummyCommutable)).to.deep.equal({
      type: 'LAUNCH_KERNEL',
      kernelSpecName: 'python3',
      cwd: path.resolve('/tmp'),
    })
  })
})

describe('convertRawNotebook', () => {
  it('converts a raw notebook', () => {
    const converted = convertRawNotebook('/tmp/test.ipynb', dummy);
    expect(converted.filename).to.equal('/tmp/test.ipynb');

    const notebook = converted.notebook;
    expect(dummyCommutable.get('metadata').equals(notebook.get('metadata')))
      .to.be.true;
  })
});

describe('loadingEpic', () => {
  it('errors without a filename', (done) => {
    const input$ = Observable.of({ type: LOAD });
    const action$ = new ActionsObservable(input$);
    const actionBuffer = [];
    const responseActions = loadEpic(action$);
    responseActions.subscribe(
      (x) => actionBuffer.push(x.type),
      (err) => {
        expect(err.message).to.equal('load needs a filename')
        done();
      },
      () => {
        expect.fail();
      },
    )
  });
  it('errors when file cant be read', (done) => {
    const input$ = Observable.of({ type: LOAD , filename: 'file'});
    const action$ = new ActionsObservable(input$);
    const actionBuffer = [];
    const responseActions = loadEpic(action$);
    responseActions.subscribe(
      (x) => actionBuffer.push(x.type),
      (err) => expect.fail(),
      () => {
        expect(actionBuffer).to.deep.equal(['ERROR']);
        done();
      },
    )
  });
});

describe('newNotebookEpic', () => {
  it('calls new Kernel after creating a new notebook', (done) => {
    const input$ = Observable.of({ type: NEW_NOTEBOOK });
    const action$ = new ActionsObservable(input$);
    const actionBuffer = [];
    const responseActions = newNotebookEpic(action$);
    responseActions.subscribe(
      (x) => actionBuffer.push(x.type),
      (err) => expect.fail(),
      () => {
        expect(actionBuffer).to.deep.equal(['SET_NOTEBOOK', 'LAUNCH_KERNEL']);
        done();
      },
    )
  })
})
<|endoftext|>


<commit_before>// To run, please use jrunscript (jdk tool)
// example: jrunscript start-server.js

withNashorn = true
try {
    load("nashorn:mozilla_compat.js");
    this.println = print;
} catch (e) {
    withNashorn = false
}

importPackage(java.lang)
importPackage(java.io)
importPackage(java.util)
importPackage(java.util.concurrent)
importPackage(java.util.zip)

// This script support the following configurations :
// PNP_PAMR : main protocol : PNP , additional protocol : PAMR (default)
// PNP : PNP protocol only
// PAMR : PAMR protocol only
var CONFIGS = {
    PNP_PAMR : 1,
    PNP : 2,
    PAMR : 3
}

// Change this variable in order to switch configurations
var config = CONFIGS.PNP

// Change these ports if required
var ROUTER_PORT = 64737 // port used by the PAMR router
var SCHEDULER_PORT = 64739 // port used by the Scheduler in case of PNP
var JETTY_PORT = 8080  // port used by the Web Server

var MAIN_PROTOCOL
var ADDITIONAL_PROTOCOL
var PORT_PROTOCOL

var SCHEDULER_URL

// Please always use this variale instead of os-dependant separator
var fs = File.separator;

// The name of the current script used for checking current location
var SCRIPT_NAME = 'start-server.js'

// Get current directory (must be SCHEDULER_HOME/bin)
var currDir = new File(getCheckedCurrDir())
var homeDir = new File(currDir.getParent())
var logsDir = new File(homeDir, 'logs')
var configDir = new File(homeDir, 'config')
var distDir = new File(homeDir, 'dist')
var paConfigFile = new File(configDir, 'proactive'+fs+'ProActiveConfiguration.xml')

// Load all jars from dist/lib
loadClasspath();

// OS switch and independent absolute path to Java executable
var javaExe = System.getProperty('java.home')+fs+'bin'+fs+(com.sun.jna.Platform.isWindows() ? 'java.exe' : 'java')

// Logs locations
var routerOutputFile = new File(logsDir,'Router-stdout.log')
var schedulerOutputFile = new File(logsDir, 'Scheduler-stdout.log')

// Processes
var routerProcess, schedulerProcess

startEverything()

function startEverything() {
	println('---------------------------------')
	println('    Starting server processes    ')
	println('---------------------------------')

	println('\nSetting up config and checking ports ...')
    setupConfigAndCheckPorts()
    setupURLs()

    println('\nDumping configuration into ' + paConfigFile)
    dumpProActiveConfiguration(paConfigFile)

	// Add shutdownhook to terminate all processes if the current process is killed
	Runtime.getRuntime().addShutdownHook(new Thread(function () {
		if (schedulerProcess != null) schedulerProcess.destroy()
		if (routerProcess != null) routerProcess.destroy()
	}))

	var executor = Executors.newFixedThreadPool(3)
	var service = new ExecutorCompletionService(executor)

	if (!logsDir.exists()) {
		logsDir.mkdir()
	}

    if (config == CONFIGS.PNP_PAMR || config == CONFIGS.PAMR) {
        println('\nRunning PAMR Router process ...')
        routerProcess = startRouter()

        if (routerProcess != null) {
            var routerWaiter = new Callable({
                call: function () {
                    var exitValue = routerProcess.waitFor()
                    println('!! Router HAS EXITED !! Please consult ' + routerOutputFile)
                    return exitValue
                }})
            service.submit(routerWaiter)
        }
    }

	println('\nRunning Scheduler process ...')
	schedulerProcess = startScheduler()
	if (schedulerProcess != null) {
		var schedulerWaiter = new Callable({
			call: function () {
				var exitValue = schedulerProcess.waitFor()
				println('!! Scheduler HAS EXITED !! Please consult ' + schedulerOutputFile)
				return exitValue
		}})
		service.submit(schedulerWaiter)
	}

	var exitListener = new Callable({
    call: function () {
            try {
                var stream = new InputStreamReader(System['in'])
                var reader = new BufferedReader(stream)
                while (!(reader.readLine().equals('exit')));
            } catch (e) {
                println('Unable to get input due to ' + e)
            }
            return 'exit by user'
    }})
    service.submit(exitListener)

	// For each process a waiter thread is used
	println('Preparing to wait for processes to exit ...')
	// no more tasks are going to be submitted, this will let the executor clean up its threads
	executor.shutdown()

	if (!executor.isTerminated()) {
	    println('Hit CTRL+C or enter \'exit\' to terminate all server processes and exit')
		var finishedFuture = service.take()
		println('Finishing process returned ' + finishedFuture.get())
		// Exit current process ... if under agent it will restart it
		System.exit(-1)
	}
}

// Configure protocol and heck that given ports are free
function setupConfigAndCheckPorts() {
    switch (config) {
        case CONFIGS.PNP_PAMR:
            MAIN_PROTOCOL = 'pnp'
            ADDITIONAL_PROTOCOL = 'pamr'
            PORT_PROTOCOL = 'pnp'
            CHECK_PORTS = {
                'ROUTER':ROUTER_PORT,
                'SCHEDULER':SCHEDULER_PORT,
                'JETTY':JETTY_PORT
            }
            break
        case CONFIGS.PNP:
            MAIN_PROTOCOL = 'pnp'
            ADDITIONAL_PROTOCOL = null
            PORT_PROTOCOL = 'pnp'
            CHECK_PORTS = {
                'SCHEDULER':SCHEDULER_PORT,
                'JETTY':JETTY_PORT
            }
            break
        case CONFIGS.PAMR:
            MAIN_PROTOCOL = 'pamr'
            ADDITIONAL_PROTOCOL = null
            PORT_PROTOCOL = null
            CHECK_PORTS = {
                'ROUTER':ROUTER_PORT,
                'JETTY':JETTY_PORT
            }
            break
        default:
            MAIN_PROTOCOL = 'pnp'
            ADDITIONAL_PROTOCOL = 'pamr'
            PORT_PROTOCOL = 'pnp'
            CHECK_PORTS = {
                'ROUTER':ROUTER_PORT,
                'SCHEDULER':SCHEDULER_PORT,
                'JETTY':JETTY_PORT
            }
    }
    checkPorts(CHECK_PORTS)
}

function setupURLs() {
    if (config == CONFIGS.PAMR) {
        SCHEDULER_URL = 'pamr://0'
    } else {
        SCHEDULER_URL = PORT_PROTOCOL+'://localhost:'+SCHEDULER_PORT
    }
}

function startRouter() {
	var cmd = initCmd()
	cmd.push('-server')
	cmd.push('-XX:+UseParNewGC')
	cmd.push('-XX:+UseConcMarkSweepGC')
	cmd.push('-XX:CMSInitiatingOccupancyFraction=50')
	cmd.push('-XX:NewRatio=2')
	cmd.push('-Xms512m')
	cmd.push('-Xmx512m')
	cmd.push('-Dlog4j.configuration=file:'+configDir+fs+'log4j' + fs+'log4j-router')
	cmd.push('org.objectweb.proactive.extensions.pamr.router.Main')
	cmd.push('--configFile')
	cmd.push(configDir+fs+'router'+fs+'router.ini')
	cmd.push('-v')
    cmd.push('-i', '0.0.0.0')   // ip to bind
    cmd.push('-p', ROUTER_PORT) // port to listen
	cmd.push('-t', '180000')    // heartbeat timeout
	cmd.push('-e', '86400000')  // disconnected clients timeout
	var proc = execCmdAsync(cmd, homeDir, routerOutputFile, 'router listening on')
	println('PAMR Router stdout/stderr redirected into ' + routerOutputFile)
	return proc
}

function startScheduler() {
	var cmd = initCmd()
    if (PORT_PROTOCOL != null) {
	    cmd.push('-Dproactive.'+PORT_PROTOCOL+'.port='+SCHEDULER_PORT)
    }
	cmd.push('-Dlog4j.configuration=file:'+configDir+fs+'log4j' + fs+'scheduler-log4j-server')
    if (config == CONFIGS.PNP_PAMR || config == CONFIGS.PAMR) {
	    cmd.push('-Dproactive.pamr.agent.id=0')
	    cmd.push('-Dproactive.pamr.agent.magic_cookie=scheduler')
    }
    cmd.push('org.ow2.proactive.scheduler.util.SchedulerStarter')

	var proc = execCmdAsync(cmd, homeDir, schedulerOutputFile, 'The scheduler created on')
	println('Scheduler stdout/stderr redirected into ' + schedulerOutputFile)
	return proc
}

function initCmd() {
	var cmd = [ javaExe ]
	cmd.push('-Dproactive.home='+homeDir)
	cmd.push('-Dpa.rm.home='+homeDir)
	cmd.push('-Dpa.scheduler.home='+homeDir)
	cmd.push('-Djava.security.manager')
	cmd.push('-Djava.security.policy=file:'+configDir+fs+'security.java.policy-server')
	cmd.push('-Dproactive.configuration='+configDir+fs+'proactive'+fs+'ProActiveConfiguration.xml')
	cmd.push('-Dderby.stream.error.file='+logsDir+fs+'derby.log')
	cmd.push('-Xms128m','-Xmx1048m')
	cmd.push('-Dproactive.communication.protocol='+MAIN_PROTOCOL)
    if (ADDITIONAL_PROTOCOL != null) {
        cmd.push('-Dproactive.communication.additional_protocols='+ADDITIONAL_PROTOCOL)
    }
	return cmd
}

function dumpProActiveConfiguration(/*File*/ targetFile) {
    var pconf = new PrintWriter(targetFile)
    pconf.println('<?xml version=\'1.0\' encoding=\'UTF-8\'?>')
    pconf.println('<ProActiveUserProperties>')
    pconf.println(' <properties>')
    pconf.println('     <prop key=\'proactive.communication.protocol\' value=\''+MAIN_PROTOCOL+'\'/>')
    // normally we don't put additional protocols in standard proactive configuration
    if (config == CONFIGS.PNP_PAMR || config == CONFIGS.PAMR) {
        pconf.println('     <prop key=\'proactive.pamr.router.address\' value=\''+java.net.InetAddress.getLocalHost().getHostName()+'\'/>')
        pconf.println('     <prop key=\'proactive.pamr.router.port\' value=\''+ROUTER_PORT+'\'/>')
    }

    // prevent proactive from binding on loopback adresses
    pconf.println('     <prop key=\'proactive.net.nolocal\' value=\'true\'/>')

    // the following properties are often used, uncomment them if you need :
    // pconf.println('     <prop key=\'proactive.useIPaddress\' value=\'true\'/>')

    pconf.println('  </properties>')
    pconf.println('  <javaProperties>')
    pconf.println('  </javaProperties>')
    pconf.println('</ProActiveUserProperties>')
    pconf.flush()
    pconf.close()
}

function fillClasspath() {
	var libDir = new File(distDir, 'lib')
	var allJars = []
	for (var i = 0; i < arguments.length; i++){
		var array = arguments[i]
		for (x in array) {
			allJars.push(new File(libDir, array[x]))
		}
	}
	// Add jars in addons directory
	new File(homeDir, 'addons').listFiles(new FileFilter({
		accept: function (file) {
			if (file.getName().endsWith('.jar')) {
				allJars.push(file)
			}
			return false
	}}))

	var classpath = new StringBuilder('.')
	for (x in allJars) {
		classpath.append(File.pathSeparator).append(allJars[x])
	}
	return classpath.toString()
}

function execCmdAsync(cmdarray, wdir, outputFile, stringToWait) {
	// The classpath should be built from addons/* and dist/lib/*
	var env = Collections.singletonMap('CLASSPATH', fillClasspath(['*']))

	// Force to string an all elements of the cmdarray, start the process and redirect output to a file
	var cmd = new ArrayList(cmdarray.length)
	for (x in cmdarray) {
		cmd.add(cmdarray[x].toString())
	}
	var pb = new ProcessBuilder(cmd)
	pb.redirectErrorStream(true)
	pb.directory(wdir)
	pb.environment().putAll(env)

	var fos = new FileOutputStream(outputFile)
	var pw = new PrintWriter(fos)

	// Start the process and wait until it prints successfully
	var process = pb.start()
	var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))
	var line = null
	if (stringToWait != null) {
		while ((line = reader.readLine()) != null) {
			println('> ' +line)
			pw.println(line)
			pw.flush()
			if (line.contains(stringToWait)) {
				break
			}
		}
	}
	// Create the stream gobbler
	var obj = { run: function() {
		try {
	        while ( (line = reader.readLine()) != null) {
	            if (pw != null) {
	                pw.println(line)
	                pw.flush()
	            }
	        }
	    } catch (e) {
	        println('Unable to gobble the stream: ' + e + 'e.javaException')
		}
	}}

	// Start the gobbler as in a separate thread
	var r = new Runnable(obj)
	var th = new Thread(r)
	th.setDaemon(true)
	th.start()
	return process
}

function getCheckedCurrDir() {
	var currentDir = new File(System.getProperty('user.dir'))
	try {
		var errmsg = 'Please run this script from SCHEDULER_HOME'+fs+'bin'
		assertExists(currentDir + fs + SCRIPT_NAME, errmsg)
		return currentDir
	} catch (e) {
		println('Problem found: ' + e)
		System.exit(-1)
	}
}

function assertExists(file, err) { // throws IllegalStateException
	var f = new File(file)
	if (!f.exists()) {
		throw IllegalStateException('File ' + file + ' not found. ' + err)
	}
	return
}

function isTcpPortAvailable(port) { // throws IOException
	var sock = null
	try {
		sock = new java.net.ServerSocket(port)
	} catch (ee) {
		return false
	} finally {
		if (sock!=null) {
			try {
			sock.close()
			} catch (e){}
		}
	}
	return true
}

function checkPorts(portmap){
	for (var serv in portmap) {
		var avail = isTcpPortAvailable(portmap[serv])
		if (!avail) {
			println('TCP port ' + portmap[serv] + ' (used by ' + serv + ') is busy...')
			println('This program will exit...')
			System.exit(-1)
		}
	}
}

// Add all jars from dist/lib to current classpath
function loadClasspath() {
	if (System.getProperty('un') == null) {
		System.setProperty('un','un')
	} else {
		return;
	}

    var urlsList = new ArrayList()
    var jars = new File(distDir, 'lib').listFiles()
    for (x in jars) {
        urlsList.add(jars[x].toURL())
    }
    if (withNashorn) {
        urlClass = Java.type("java.net.URL")['class']
    } else {
        urlClass = java.net.URL
    }
	var urls = urlsList.toArray(java.lang.reflect.Array.newInstance(urlClass, urlsList.size()))
	var currentThreadClassLoader = Thread.currentThread().getContextClassLoader()
	var urlClassLoader = new java.net.URLClassLoader(urls, currentThreadClassLoader)
	Thread.currentThread().setContextClassLoader(urlClassLoader)
	var seManager = new javax.script.ScriptEngineManager(urlClassLoader);
    var engine = seManager.getEngineByName('javascript');
	engine.eval(new java.io.FileReader(new File(currDir,SCRIPT_NAME)));
}
<commit_msg>SCHEDULING-2016: start-server.js should kill all deployed local nodes on exit<commit_after>// To run, please use jrunscript (jdk tool)
// example: jrunscript start-server.js

withNashorn = true
try {
    load("nashorn:mozilla_compat.js");
    this.println = print;
} catch (e) {
    withNashorn = false
}

importPackage(java.lang)
importPackage(java.io)
importPackage(java.util)
importPackage(java.util.concurrent)
importPackage(java.util.zip)

// This script support the following configurations :
// PNP_PAMR : main protocol : PNP , additional protocol : PAMR (default)
// PNP : PNP protocol only
// PAMR : PAMR protocol only
var CONFIGS = {
    PNP_PAMR : 1,
    PNP : 2,
    PAMR : 3
}

// Change this variable in order to switch configurations
var config = CONFIGS.PNP

// Change these ports if required
var ROUTER_PORT = 64737 // port used by the PAMR router
var SCHEDULER_PORT = 64739 // port used by the Scheduler in case of PNP
var JETTY_PORT = 8080  // port used by the Web Server

var MAIN_PROTOCOL
var ADDITIONAL_PROTOCOL
var PORT_PROTOCOL

var SCHEDULER_URL

// Please always use this variale instead of os-dependant separator
var fs = File.separator;

// The name of the current script used for checking current location
var SCRIPT_NAME = 'start-server.js'

// Get current directory (must be SCHEDULER_HOME/bin)
var currDir = new File(getCheckedCurrDir())
var homeDir = new File(currDir.getParent())
var logsDir = new File(homeDir, 'logs')
var configDir = new File(homeDir, 'config')
var distDir = new File(homeDir, 'dist')
var paConfigFile = new File(configDir, 'proactive'+fs+'ProActiveConfiguration.xml')

// Load all jars from dist/lib
loadClasspath();

// OS switch and independent absolute path to Java executable
var javaExe = System.getProperty('java.home')+fs+'bin'+fs+(com.sun.jna.Platform.isWindows() ? 'java.exe' : 'java')

// Logs locations
var routerOutputFile = new File(logsDir,'Router-stdout.log')
var schedulerOutputFile = new File(logsDir, 'Scheduler-stdout.log')

// Processes
var routerProcess, schedulerProcess

startEverything()

function startEverything() {
	println('---------------------------------')
	println('    Starting server processes    ')
	println('---------------------------------')

	println('\nSetting up config and checking ports ...')
    setupConfigAndCheckPorts()
    setupURLs()

    println('\nDumping configuration into ' + paConfigFile)
    dumpProActiveConfiguration(paConfigFile)

    killChildrenOnShutdown()

	var executor = Executors.newFixedThreadPool(3)
	var service = new ExecutorCompletionService(executor)

	if (!logsDir.exists()) {
		logsDir.mkdir()
	}

    if (config == CONFIGS.PNP_PAMR || config == CONFIGS.PAMR) {
        println('\nRunning PAMR Router process ...')
        routerProcess = startRouter()

        if (routerProcess != null) {
            var routerWaiter = new Callable({
                call: function () {
                    var exitValue = routerProcess.waitFor()
                    println('!! Router HAS EXITED !! Please consult ' + routerOutputFile)
                    return exitValue
                }})
            service.submit(routerWaiter)
        }
    }

	println('\nRunning Scheduler process ...')
	schedulerProcess = startScheduler()
	if (schedulerProcess != null) {
		var schedulerWaiter = new Callable({
			call: function () {
				var exitValue = schedulerProcess.waitFor()
				println('!! Scheduler HAS EXITED !! Please consult ' + schedulerOutputFile)
				return exitValue
		}})
		service.submit(schedulerWaiter)
	}

	var exitListener = new Callable({
    call: function () {
            try {
                var stream = new InputStreamReader(System['in'])
                var reader = new BufferedReader(stream)
                while (!(reader.readLine().equals('exit')));
            } catch (e) {
                println('Unable to get input due to ' + e)
            }
            return 'exit by user'
    }})
    service.submit(exitListener)

	// For each process a waiter thread is used
	println('Preparing to wait for processes to exit ...')
	// no more tasks are going to be submitted, this will let the executor clean up its threads
	executor.shutdown()

	if (!executor.isTerminated()) {
	    println('Hit CTRL+C or enter \'exit\' to terminate all server processes and exit')
		var finishedFuture = service.take()
		println('Finishing process returned ' + finishedFuture.get())
		// Exit current process ... if under agent it will restart it
		System.exit(-1)
	}
}

// Configure protocol and heck that given ports are free
function setupConfigAndCheckPorts() {
    switch (config) {
        case CONFIGS.PNP_PAMR:
            MAIN_PROTOCOL = 'pnp'
            ADDITIONAL_PROTOCOL = 'pamr'
            PORT_PROTOCOL = 'pnp'
            CHECK_PORTS = {
                'ROUTER':ROUTER_PORT,
                'SCHEDULER':SCHEDULER_PORT,
                'JETTY':JETTY_PORT
            }
            break
        case CONFIGS.PNP:
            MAIN_PROTOCOL = 'pnp'
            ADDITIONAL_PROTOCOL = null
            PORT_PROTOCOL = 'pnp'
            CHECK_PORTS = {
                'SCHEDULER':SCHEDULER_PORT,
                'JETTY':JETTY_PORT
            }
            break
        case CONFIGS.PAMR:
            MAIN_PROTOCOL = 'pamr'
            ADDITIONAL_PROTOCOL = null
            PORT_PROTOCOL = null
            CHECK_PORTS = {
                'ROUTER':ROUTER_PORT,
                'JETTY':JETTY_PORT
            }
            break
        default:
            MAIN_PROTOCOL = 'pnp'
            ADDITIONAL_PROTOCOL = 'pamr'
            PORT_PROTOCOL = 'pnp'
            CHECK_PORTS = {
                'ROUTER':ROUTER_PORT,
                'SCHEDULER':SCHEDULER_PORT,
                'JETTY':JETTY_PORT
            }
    }
    checkPorts(CHECK_PORTS)
}

function setupURLs() {
    if (config == CONFIGS.PAMR) {
        SCHEDULER_URL = 'pamr://0'
    } else {
        SCHEDULER_URL = PORT_PROTOCOL+'://localhost:'+SCHEDULER_PORT
    }
}

function startRouter() {
	var cmd = initCmd()
	cmd.push('-server')
	cmd.push('-XX:+UseParNewGC')
	cmd.push('-XX:+UseConcMarkSweepGC')
	cmd.push('-XX:CMSInitiatingOccupancyFraction=50')
	cmd.push('-XX:NewRatio=2')
	cmd.push('-Xms512m')
	cmd.push('-Xmx512m')
	cmd.push('-Dlog4j.configuration=file:'+configDir+fs+'log4j' + fs+'log4j-router')
	cmd.push('org.objectweb.proactive.extensions.pamr.router.Main')
	cmd.push('--configFile')
	cmd.push(configDir+fs+'router'+fs+'router.ini')
	cmd.push('-v')
    cmd.push('-i', '0.0.0.0')   // ip to bind
    cmd.push('-p', ROUTER_PORT) // port to listen
	cmd.push('-t', '180000')    // heartbeat timeout
	cmd.push('-e', '86400000')  // disconnected clients timeout
	var proc = execCmdAsync(cmd, homeDir, routerOutputFile, 'router listening on')
	println('PAMR Router stdout/stderr redirected into ' + routerOutputFile)
	return proc
}

function startScheduler() {
	var cmd = initCmd()
    if (PORT_PROTOCOL != null) {
	    cmd.push('-Dproactive.'+PORT_PROTOCOL+'.port='+SCHEDULER_PORT)
    }
	cmd.push('-Dlog4j.configuration=file:'+configDir+fs+'log4j' + fs+'scheduler-log4j-server')
    if (config == CONFIGS.PNP_PAMR || config == CONFIGS.PAMR) {
	    cmd.push('-Dproactive.pamr.agent.id=0')
	    cmd.push('-Dproactive.pamr.agent.magic_cookie=scheduler')
    }
    cmd.push('org.ow2.proactive.scheduler.util.SchedulerStarter')

	var proc = execCmdAsync(cmd, homeDir, schedulerOutputFile, 'The scheduler created on')
	println('Scheduler stdout/stderr redirected into ' + schedulerOutputFile)
	return proc
}

function initCmd() {
	var cmd = [ javaExe ]
	cmd.push('-Dproactive.home='+homeDir)
	cmd.push('-Dpa.rm.home='+homeDir)
	cmd.push('-Dpa.scheduler.home='+homeDir)
	cmd.push('-Djava.security.manager')
	cmd.push('-Djava.security.policy=file:'+configDir+fs+'security.java.policy-server')
	cmd.push('-Dproactive.configuration='+configDir+fs+'proactive'+fs+'ProActiveConfiguration.xml')
	cmd.push('-Dderby.stream.error.file='+logsDir+fs+'derby.log')
	cmd.push('-Xms128m','-Xmx1048m')
	cmd.push('-Dproactive.communication.protocol='+MAIN_PROTOCOL)
    if (ADDITIONAL_PROTOCOL != null) {
        cmd.push('-Dproactive.communication.additional_protocols='+ADDITIONAL_PROTOCOL)
    }
	return cmd
}

function dumpProActiveConfiguration(/*File*/ targetFile) {
    var pconf = new PrintWriter(targetFile)
    pconf.println('<?xml version=\'1.0\' encoding=\'UTF-8\'?>')
    pconf.println('<ProActiveUserProperties>')
    pconf.println(' <properties>')
    pconf.println('     <prop key=\'proactive.communication.protocol\' value=\''+MAIN_PROTOCOL+'\'/>')
    // normally we don't put additional protocols in standard proactive configuration
    if (config == CONFIGS.PNP_PAMR || config == CONFIGS.PAMR) {
        pconf.println('     <prop key=\'proactive.pamr.router.address\' value=\''+java.net.InetAddress.getLocalHost().getHostName()+'\'/>')
        pconf.println('     <prop key=\'proactive.pamr.router.port\' value=\''+ROUTER_PORT+'\'/>')
    }

    // prevent proactive from binding on loopback adresses
    pconf.println('     <prop key=\'proactive.net.nolocal\' value=\'true\'/>')

    // the following properties are often used, uncomment them if you need :
    // pconf.println('     <prop key=\'proactive.useIPaddress\' value=\'true\'/>')

    pconf.println('  </properties>')
    pconf.println('  <javaProperties>')
    pconf.println('  </javaProperties>')
    pconf.println('</ProActiveUserProperties>')
    pconf.flush()
    pconf.close()
}

function fillClasspath() {
	var libDir = new File(distDir, 'lib')
	var allJars = []
	for (var i = 0; i < arguments.length; i++){
		var array = arguments[i]
		for (x in array) {
			allJars.push(new File(libDir, array[x]))
		}
	}
	// Add jars in addons directory
	new File(homeDir, 'addons').listFiles(new FileFilter({
		accept: function (file) {
			if (file.getName().endsWith('.jar')) {
				allJars.push(file)
			}
			return false
	}}))

	var classpath = new StringBuilder('.')
	for (x in allJars) {
		classpath.append(File.pathSeparator).append(allJars[x])
	}
	return classpath.toString()
}

function execCmdAsync(cmdarray, wdir, outputFile, stringToWait) {
	// The classpath should be built from addons/* and dist/lib/*
	var env = Collections.singletonMap('CLASSPATH', fillClasspath(['*']))

	// Force to string an all elements of the cmdarray, start the process and redirect output to a file
	var cmd = new ArrayList(cmdarray.length)
	for (x in cmdarray) {
		cmd.add(cmdarray[x].toString())
	}
	var pb = new ProcessBuilder(cmd)
	pb.redirectErrorStream(true)
	pb.directory(wdir)
	pb.environment().putAll(env)

	var fos = new FileOutputStream(outputFile)
	var pw = new PrintWriter(fos)

	// Start the process and wait until it prints successfully
	var process = pb.start()
	var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))
	var line = null
	if (stringToWait != null) {
		while ((line = reader.readLine()) != null) {
			println('> ' +line)
			pw.println(line)
			pw.flush()
			if (line.contains(stringToWait)) {
				break
			}
		}
	}
	// Create the stream gobbler
	var obj = { run: function() {
		try {
	        while ( (line = reader.readLine()) != null) {
	            if (pw != null) {
	                pw.println(line)
	                pw.flush()
	            }
	        }
	    } catch (e) {
	        println('Unable to gobble the stream: ' + e + 'e.javaException')
		}
	}}

	// Start the gobbler as in a separate thread
	var r = new Runnable(obj)
	var th = new Thread(r)
	th.setDaemon(true)
	th.start()
	return process
}

function getCheckedCurrDir() {
	var currentDir = new File(System.getProperty('user.dir'))
	try {
		var errmsg = 'Please run this script from SCHEDULER_HOME'+fs+'bin'
		assertExists(currentDir + fs + SCRIPT_NAME, errmsg)
		return currentDir
	} catch (e) {
		println('Problem found: ' + e)
		System.exit(-1)
	}
}

function assertExists(file, err) { // throws IllegalStateException
	var f = new File(file)
	if (!f.exists()) {
		throw IllegalStateException('File ' + file + ' not found. ' + err)
	}
	return
}

function isTcpPortAvailable(port) { // throws IOException
	var sock = null
	try {
		sock = new java.net.ServerSocket(port)
	} catch (ee) {
		return false
	} finally {
		if (sock!=null) {
			try {
			sock.close()
			} catch (e){}
		}
	}
	return true
}

function checkPorts(portmap){
	for (var serv in portmap) {
		var avail = isTcpPortAvailable(portmap[serv])
		if (!avail) {
			println('TCP port ' + portmap[serv] + ' (used by ' + serv + ') is busy...')
			println('This program will exit...')
			System.exit(-1)
		}
	}
}

// Add all jars from dist/lib to current classpath
function loadClasspath() {
	if (System.getProperty('un') == null) {
		System.setProperty('un','un')
	} else {
		return;
	}

    var urlsList = new ArrayList()
    var jars = new File(distDir, 'lib').listFiles()
    for (x in jars) {
        urlsList.add(jars[x].toURL())
    }
    if (withNashorn) {
        urlClass = Java.type("java.net.URL")['class']
    } else {
        urlClass = java.net.URL
    }
	var urls = urlsList.toArray(java.lang.reflect.Array.newInstance(urlClass, urlsList.size()))
	var currentThreadClassLoader = Thread.currentThread().getContextClassLoader()
	var urlClassLoader = new java.net.URLClassLoader(urls, currentThreadClassLoader)
	Thread.currentThread().setContextClassLoader(urlClassLoader)
	var seManager = new javax.script.ScriptEngineManager(urlClassLoader);
    var engine = seManager.getEngineByName('javascript');
	engine.eval(new java.io.FileReader(new File(currDir,SCRIPT_NAME)));
}

function killChildrenOnShutdown() {
	// Add shutdownhook to terminate all processes if the current process is killed
	Runtime.getRuntime().addShutdownHook(new Thread(function () {
		if (schedulerProcess != null) schedulerProcess.destroy()
		if (routerProcess != null) routerProcess.destroy()
	}))
	// Kill all children processes on exit
	org.apache.log4j.BasicConfigurator.configure(new org.apache.log4j.varia.NullAppender())
	org.ow2.proactive.rm.util.process.EnvironmentCookieBasedChildProcessKiller.setCookie('killme')
    org.ow2.proactive.rm.util.process.EnvironmentCookieBasedChildProcessKiller.registerKillChildProcessesOnShutdown()
}<|endoftext|>


<commit_before>////
// Tests the driver cursor API
/////

process.on('uncaughtException', function(err) {
    console.log(err);
    console.log(err.toString() + err.stack.toString());
});

var r = require('../../../drivers/javascript/build/rethinkdb');

var assertNoError = function(err) {
    if (err) {
        throw "Error "+err+" not expected"
    }
};

var assertArgError = function(expected, found, callback) {
    var errFound = null;
    try {
        callback();
    } catch (err) {
        errFound = err;
    }

    if (!errFound) {
        throw new Error("No error thrown");
    } else if (!(errFound.msg !== "Expected "+expected+" argument(s) but found "+found)) {
        throw new Error("Wrong error message: "+errFound.msg);
    }
};

var assert = function(predicate) {
    if (!predicate) {
        throw new Error("Assert failed");
    }
};

var port = parseInt(process.argv[2], 10)

assertArgError(2, 0, function() { r.connect(); });

r.connect({port:port}, function(err, c) {
    assertNoError(err);

    var tbl = r.table('test');
    var num_rows = parseInt(process.argv[3], 10);
    console.log("Testing for "+num_rows);

    tbl.run(c, function(err, cur) {
        assertNoError(err);

        // This is getting unruley but we want to make sure that array results
        // support the connection api too
        r([1,2,3]).run(c, function(err, res) {
            assertNoError(err);

            // yes, res is an array that supports array ops
            res.push(4, 5);
            res.toArray(function(err, res) {
                assertNoError(err);
                assert(res.length == 5);
                for (var i = 1; i <= 5; ++i) {
                    assert(res[i] = i);
                }
            });
        });

        // These simply test that we appropriately check arg numbers for
        // cursor api methods
        assertArgError(1, 0, function() { cur.each(); });
        assertArgError(1, 0, function() { cur.toArray(); });
        assertArgError(1, 0, function() { cur.toArray(); });
        assertArgError(1, 0, function() { cur.next(); });
        assertArgError(0, 1, function() { cur.hasNext(1); });
        assertArgError(0, 1, function() { cur.close(1); });
        assertArgError(0, 1, function() { cur.toString(1); });

        var i = 0;
        cur.each(function(err, row) {
            assertNoError(err);
            i++;

            // This is the only way to know if the `each` has ended
            if (!cur.hasNext()) {
                if (i === num_rows) {
                    console.log("Test passed!");
                } else {
                    console.log("Test failed: expected "+num_rows+" rows but found "+i+".");
                    process.exit(1)
                }
                c.close();
            }
        });
    });
});
<commit_msg>fix for review 439<commit_after>////
// Tests the driver cursor API
/////

process.on('uncaughtException', function(err) {
    console.log(err);
    console.log(err.toString() + err.stack.toString());
});

var r = require('../../../drivers/javascript/build/rethinkdb');

var assertNoError = function(err) {
    if (err) {
        throw "Error "+err+" not expected"
    }
};

var assertArgError = function(expected, found, callback) {
    var errFound = null;
    try {
        callback();
    } catch (err) {
        errFound = err;
    }

    if (!errFound) {
        throw new Error("No error thrown");
    } else if (!(errFound.msg !== "Expected "+expected+" argument(s) but found "+found)) {
        throw new Error("Wrong error message: "+errFound.msg);
    }
};

var assert = function(predicate) {
    if (!predicate) {
        throw new Error("Assert failed");
    }
};

var port = parseInt(process.argv[2], 10)

assertArgError(2, 0, function() { r.connect(); });

r.connect({port:port}, function(err, c) {
    assertNoError(err);

    var tbl = r.table('test');
    var num_rows = parseInt(process.argv[3], 10);
    console.log("Testing for "+num_rows);

    tbl.run(c, function(err, cur) {
        assertNoError(err);

        // This is getting unruley but we want to make sure that array results
        // support the connection api too
        r([1,2,3]).run(c, function(err, res) {
            assertNoError(err);

            // yes, res is an array that supports array ops
            res.push(4, 5);
            res.toArray(function(err, res) {
                assertNoError(err);
                assert(res.length == 5);
                for (var i = 1; i <= 5; ++i) {
                    assert(res[i - 1] == i);
                }
            });
        });

        // These simply test that we appropriately check arg numbers for
        // cursor api methods
        assertArgError(1, 0, function() { cur.each(); });
        assertArgError(1, 0, function() { cur.toArray(); });
        assertArgError(1, 0, function() { cur.toArray(); });
        assertArgError(1, 0, function() { cur.next(); });
        assertArgError(0, 1, function() { cur.hasNext(1); });
        assertArgError(0, 1, function() { cur.close(1); });
        assertArgError(0, 1, function() { cur.toString(1); });

        var i = 0;
        cur.each(function(err, row) {
            assertNoError(err);
            i++;

            // This is the only way to know if the `each` has ended
            if (!cur.hasNext()) {
                if (i === num_rows) {
                    console.log("Test passed!");
                } else {
                    console.log("Test failed: expected "+num_rows+" rows but found "+i+".");
                    process.exit(1)
                }
                c.close();
            }
        });
    });
});
<|endoftext|>


<commit_before>var pull = require('pull-stream')
var mux = require('../')
var tape = require('tape')

function delay(fun) {
  return function (a, b) {
    setImmediate(function () {
      fun(a, b)
    })
  }
}

var client = {
  hello  : 'async',
  goodbye: 'async',
  stuff  : 'source',
  bstuff : 'source',
  things : 'sink',
  echo   : 'duplex',
  suchstreamwow: 'duplex'
}

module.exports = function (codec) {

tape('outer stream ends after close', function (t) {

  t.plan(4)

  var A = mux(client, null, codec) ()
  var B = mux(null, client, codec) ({
    hello: function (a, cb) {
      delay(cb)(null, 'hello, '+a)
    },
    goodbye: function(b, cb) {
      delay(cb)(null, b)
    }
  })

  A.hello('jim', function (err, value) {
    if(err) throw err
    console.log(value)
    t.equal(value, 'hello, jim')
  })

  A.goodbye('bbb', function (err, value) {
    if(err) throw err
    console.log(value)
    t.equal(value, 'bbb')
  })

  var bs = B.createStream()

  var as = A.createStream()
  pull(as, bs, as)

  A.on('closed', function () {
    t.ok(true)
  })

  A.close(function (err) {
    t.notOk(err)
  })

})

tape('close after uniplex streams end', function (t) {
  t.plan(6)

  var A = mux(client, null, codec) ()
  var B = mux(null, client, codec) ({
    stuff: function () {
      t.ok(true)
      return pull.values([1, 2, 3, 4, 5])
    }
  })

  pull(A.stuff(), pull.collect(function (err, ary) {
    t.deepEqual(ary, [1, 2, 3, 4, 5])
  }))

  var bs = B.createStream()
  var as = A.createStream()
  pull(as, bs, as)

  B.on('closed', function () {
    console.log('B emits "closed"')
    t.ok(true)
  })

  A.on('closed', function () {
    console.log('A emits "closed"')
    t.ok(true)
  })

  B.close(function (err) {
    console.log('B CLOSE')
    t.notOk(err, 'bs is closed')
  })

  A.close(function (err) {
    console.log('A CLOSE')
    t.notOk(err, 'as is closed')
  })
})

tape('close after uniplex streams end 2', function (t) {
  t.plan(4)

  var A = mux(client, null, codec) ()
  var B = mux(null, client, codec) ({
    things: function () {
      t.ok(true)
      return pull.collect(function (err, ary) {
        t.deepEqual(ary, [1, 2, 3, 4, 5])
      })
    }
  })

  pull(pull.values([1, 2, 3, 4, 5]), A.things())

  var bs = B.createStream()
  var as = A.createStream()

  pull(as, bs, as)

  B.close(function (err) {
    console.log('B CLOSE')
    t.notOk(err, 'bs is closed')
  })

  A.close(function (err) {
    console.log('A CLOSE')
    t.notOk(err, 'as is closed')
  })
})

tape('close after both sides of a duplex stream ends', function (t) {

  t.plan(8)

  var A = mux(client, null, codec) ()
  var B = mux(null, client, codec) ({
    echo: function () {
      return pull.through(console.log, function () {
        t.ok(true)
      })
    }
  })

  var bs = B.createStream()
  var as = A.createStream()

  pull(
    pull.values([1, 2, 3, 4, 5]),
    A.echo(),
    pull.collect(function (err, ary) {
      if(err) throw err
      t.deepEqual(ary, [1,2,3,4,5])
    })
  )

  pull(as, bs, as)

  t.notOk(B.closed)
  t.notOk(A.closed)

  B.on('closed', function () {
    t.ok(true)
  })

  A.on('closed', function () {
    t.ok(true)
  })

  B.close(function (err) {
    console.log('B CLOSE')
    t.notOk(err, 'bs is closed')
  })

  A.close(function (err) {
    console.log('A CLOSE', err)
    t.notOk(err, 'as is closed')
  })

})

tape('closed is emitted when stream disconnects', function (t) {
  t.plan(2)
  var A = mux(client, null) ()
  A.on('closed', function (err) {
    console.log('EMIT CLOSED')
    t.notOk(err)
  })
  pull(pull.empty(), A.createStream(function (err) {
    console.log(err)
    t.notOk(err) //end of parent stream
  }), pull.drain())
})

tape('closed is emitted with error when stream errors', function (t) {
  t.plan(2)
  var A = mux(client, null, codec) ()
  A.on('closed', function (err) {
    t.notOk(err)
  })
  pull(pull.empty(), A.createStream(function (err) {
    console.log(err)
    t.notOk(err) //end of parent stream
  }), pull.drain())
})

}

if(!module.parent)
  module.exports(function (e) { return e })
<commit_msg>remove graceful close tests<commit_after>var pull = require('pull-stream')
var mux = require('../')
var tape = require('tape')

function delay(fun) {
  return function (a, b) {
    setImmediate(function () {
      fun(a, b)
    })
  }
}

var client = {
  hello  : 'async',
  goodbye: 'async',
  stuff  : 'source',
  bstuff : 'source',
  things : 'sink',
  echo   : 'duplex',
  suchstreamwow: 'duplex'
}

module.exports = function (codec) {

tape('closed is emitted when stream disconnects', function (t) {
  t.plan(2)
  var A = mux(client, null) ()
  A.on('closed', function (err) {
    console.log('EMIT CLOSED')
    t.notOk(err)
  })
  pull(pull.empty(), A.createStream(function (err) {
    console.log(err)
    t.notOk(err) //end of parent stream
  }), pull.drain())
})

tape('closed is emitted with error when stream errors', function (t) {
  t.plan(2)
  var A = mux(client, null, codec) ()
  A.on('closed', function (err) {
    t.notOk(err)
  })
  pull(pull.empty(), A.createStream(function (err) {
    console.log(err)
    t.notOk(err) //end of parent stream
  }), pull.drain())
})

}

if(!module.parent)
  module.exports(function (e) { return e })

<|endoftext|>