<commit_before>'use strict';

const child_process = require('child_process');
const fs            = require('fs');
const byline        = require('byline');
const websocket     = require('ws');
const assert        = require('assert');
const r             = require('rethinkdb');
const fusion        = require('../src/server.js');

var db = `fusion`;
var data_dir = `./rethinkdb_data_test`;

var log_file = `./fusion_test_${process.pid}.log`;
var logger = fusion.logger;
logger.level = 'debug';
logger.add(logger.transports.File, { filename: log_file });
logger.remove(logger.transports.Console);

// Variables used by different tests
var rdb_server, rdb_port, rdb_conn; // Instantiated once
var fusion_server, fusion_port; // Instantiated for HTTP and HTTPS
var fusion_conn; // Instantiated for every test

// TODO: get rid of this class, we don't really need it
class RethinkdbServer {
  constructor() {
    this.proc = child_process.spawn('rethinkdb', [ '--http-port', '0',
                                                   '--cluster-port', '0',
                                                   '--driver-port', '0',
                                                   '--cache-size', '10',
                                                   '--directory', data_dir ]);
    this.proc.once('error', (err) => assert.ifError(err));
    this.proc.once('exit', (res) => logger.error(`rdb server exited: ${res}`));

    this.line_stream = byline(this.proc.stdout);
    this.driver_port = new Promise((resolve, reject) => {
        this.line_stream.on('data', (line) => {
            var matches = line.toString().match(/^Listening for client driver connections on port (\d+)$/);
            if (matches !== null && matches.length === 2) {
              resolve(parseInt(matches[1]));
              this.line_stream.removeAllListeners('data');
            }
          });
      });

    process.on('exit', () => {
        this.proc.kill('SIGKILL');
        try { fs.rmdirSync(data_dir); } catch (err) { /* Do nothing */ }
      });
  }
};

module.exports.start_rdb_server = (done) => {
  rdb_server = new RethinkdbServer();
  rdb_server.driver_port.then((p) => {
      rdb_port = p;
      r.connect({ port: rdb_port }).then((c) => {
          rdb_conn = c;
          done();
        });
    });
};

var create_fusion_server = function (backend, opts) {
  opts.local_port = 0;
  opts.rdb_port = rdb_port;
  opts.db = db;
  return new backend(opts);<commit_msg>cleaning up server test utils, fixing removal of rethinkdb_data_test directory<commit_after>'use strict';

const fusion        = require('../src/server.js');

const assert        = require('assert');
const byline        = require('byline');
const child_process = require('child_process');
const fs            = require('fs');
const path          = require('path');
const r             = require('rethinkdb');
const websocket     = require('ws');

const db = `fusion`;
const data_dir = `./rethinkdb_data_test`;

const log_file = `./fusion_test_${process.pid}.log`;
const logger = fusion.logger;
logger.level = 'debug';
logger.add(logger.transports.File, { filename: log_file });
logger.remove(logger.transports.Console);

// Variables used by most tests
var rdb_server, rdb_port, rdb_conn; // Instantiated once
var fusion_server, fusion_port; // Instantiated for HTTP and HTTPS
var fusion_conn; // Instantiated for every test

module.exports.start_rdb_server = (done) => {
  var proc = child_process.spawn('rethinkdb', [ '--http-port', '0',
                                                '--cluster-port', '0',
                                                '--driver-port', '0',
                                                '--cache-size', '10',
                                                '--directory', data_dir ]);
  proc.once('error', (err) => assert.ifError(err));
  proc.once('exit', (res) => logger.error(`rdb server exited: ${res}`));

  process.on('exit', () => {
      proc.kill('SIGKILL');
      var rmdirSync_recursive = (dir) => {
          fs.readdirSync(dir).forEach((item) => {
              var full_path = path.join(dir, item);
              if (fs.statSync(full_path).isDirectory()) {
                rmdirSync_recursive(full_path);
              } else {
                fs.unlinkSync(full_path);
              }
            });
          fs.rmdirSync(dir);
        };
      rmdirSync_recursive(data_dir);
    });

  var line_stream = byline(proc.stdout);
  line_stream.on('data', (line) => {
      var matches = line.toString().match(/^Listening for client driver connections on port (\d+)$/);
      if (matches === null || matches.length !== 2) { return; }

      line_stream.removeAllListeners('data');
      rdb_port = parseInt(matches[1]);
      r.connect({ port: rdb_port }).then((c) => {
          rdb_conn = c;
          done();
        });
    });
};

var create_fusion_server = function (backend, opts) {
  opts.local_port = 0;
  opts.rdb_port = rdb_port;
  opts.db = db;
  return new backend(opts);
{'fraction_non_alphanumeric': 0.15438526231477773, 'fraction_numerical': 0.0032038446135362435, 'mean_word_length': 2.208092485549133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7446446', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1468, 'n_words': 326}