<commit_before>import test from 'ava';
import portfinder from 'portfinder';
import Client from './src/client';
import Server from './src/server';

const CLIENT_COUNT = 4;

let port;
let server;
const clients = [];

// Find a free port for the server
test.before.cb((t) => {
  portfinder.getPort((err, port2) => {
    port = port2;
    t.end();
  });
});

/* eslint-disable no-param-reassign */
test.serial.cb('connect', (t) => {
  // Initialize the server
  server = new Server({
    port,
    plugins: [(wsxServer) => { wsxServer.isPluginTestSuccessful = true; }],
  });
  t.is(server.sockets.size, 0);

  let connectedClientCount = 0;
  server.on('connection', () => {
    t.is(server.sockets.size, ++connectedClientCount);

    if (connectedClientCount === CLIENT_COUNT) {
      t.end();
    }
  });

  // Initialize clients
  for (let i = CLIENT_COUNT; i > 0; --i) {
    clients.push(new Client(`ws://localhost:${port}`, {
      plugins: [(wsxClient) => { wsxClient.isPluginTestSuccessful = true; }],
    }));
  }
});
/* eslint-enable no-param-reassign */

test.serial('plugins', (t) => {
  t.true(server.isPluginTestSuccessful);

  for (const client of clients) {
    t.true(client.isPluginTestSuccessful);
  }
});

test.serial.cb.skip('send/receive data', (t) => {
  const expectedType = 'echo';
  const expectedPayload = { text: 'Hello, World!' };
  const eventsOrder = [];

  const serverSocket = server.sockets.get(0);
  serverSocket.once(expectedType, (payload) => {
    t.deepEqual(payload, expectedPayload);
    serverSocket.send(expectedType, expectedPayload);
    eventsOrder.push(1);
  });

  clients[0].once(expectedType, (payload) => {
    t.deepEqual(payload, expectedPayload);
    eventsOrder.push(2);
<commit_msg>Started to make tests asynchronous<commit_after>import test from 'ava';
import portfinder from 'portfinder';
import Client from './src/client';
import Server from './src/server';

const DEFAULT_CLIENT_COUNT = 4;

function createServerWithClientsAsync(
  clientCount = DEFAULT_CLIENT_COUNT,
  serverOptions,
  clientOptions
) {
  return new Promise((resolve) => {
    // Find a free port for the server
    portfinder.getPort((err, port) => {
      resolve([
        new Server(Object.assign({ port }, serverOptions)),
        Array.from(
          new Array(clientCount),
          () => new Client(`ws://localhost:${port}`, clientOptions)
        ),
      ]);
    });
  });
}

test.cb('connect', (t) => {
  createServerWithClientsAsync()
    .then(([server]) => {
      t.is(server.sockets.size, 0);

      let connectedClientCount = 0;
      server.on('connection', () => {
        t.is(server.sockets.size, ++connectedClientCount);

        if (connectedClientCount === DEFAULT_CLIENT_COUNT) {
          t.end();
        }
      });
    });
});

test('plugins', async (t) => {
  const [server, [client]] = await createServerWithClientsAsync(
    1,
    /* eslint-disable no-param-reassign */
    { plugins: [(wsxServer) => { wsxServer.isPluginTestSuccessful = true; }] },
    { plugins: [(wsxClient) => { wsxClient.isPluginTestSuccessful = true; }] },
    /* eslint-enable no-param-reassign */
  );

  t.true(server.isPluginTestSuccessful);
  t.true(client.isPluginTestSuccessful);
});

test.serial.cb.skip('send/receive data', (t) => {
  const expectedType = 'echo';
  const expectedPayload = { text: 'Hello, World!' };
  const eventsOrder = [];

  const serverSocket = server.sockets.get(0);
  serverSocket.once(expectedType, (payload) => {
    t.deepEqual(payload, expectedPayload);
    serverSocket.send(expectedType, expectedPayload);
    eventsOrder.push(1);
  });

  clients[0].once(expectedType, (payload) => {
    t.deepEqual(payload, expectedPayload);
    eventsOrder.push(2);

{'fraction_non_alphanumeric': 0.1651893634165995, 'fraction_numerical': 0.004834810636583401, 'mean_word_length': 3.5030229746070134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6353324', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1189, 'n_words': 238}