<commit_before>'use strict';

var Dht = require('../lib/dht');
var MockRpc = require('../lib/mock-rpc');

// Spawn a node. A node is composed of two elements: the local Dht and the Rpc.
//
function spawnNode(endpoint, seeds, cb) {
    MockRpc.spawn(endpoint, function (err, rpc) {
        if (err) return cb(err);
        Dht.spawn(rpc, seeds, function (err, dht) {
            if (err) return cb(err);
            cb(err, dht);
        });
    });
}

var nextPort = '1000';

function spawnSome(dhts, seeds, nb, cb) {
    ++nextPort;
    spawnNode('local:' + nextPort, seeds, function (err, dht) {
        if (err) return cb(err);
        dhts.push(dht);
        --nb;
        if (nb === 0)
            return cb(null, dhts);
        if (seeds.length < 3)
            seeds.push(dht.rpc.endpoint);
        return spawnSome(dhts, seeds, nb, cb);
    });
}

var WORDS = ['foo']; //, 'bar']; //, 'baz', 'glo', 'ick'];
var locals = {};

function doStuff(dhts, count) {
    setTimeout(function () {
        if (count === 0) return;
        var key = '';
        for (var i = 0; i < 1; ++i) {
            key += WORDS[Math.random() * WORDS.length | 0];
        }
        var ix = Math.random() * dhts.length | 0;
        if (Math.random() < 0.5) {
            var value = Math.random() * 256 | 0;
            console.log('[master] set %s=%d on #%s', key, value,
                        dhts[ix].rpc.endpoint);
            locals[key] = value;
            dhts[ix].set(key, value, function (err) {
                if (err) return console.log('  ERROR! %s', err.message);
                doStuff(dhts, count - 1);
            });
        } else if (Math.random() < 1) {
            console.log('[master] get %s on #%s', key, dhts[ix].rpc.endpoint);
            dhts[ix].get(key, function (err, value) {
                if (err) return console.log('  ERROR! %s', err.message);
                if (value !== locals[key]) {
                    console.log('/!\\ warning:' +
                                ' expected %s=%d but got %s', key,
                                locals[key], value);
                }
                doStuff(dhts, count - 1);
            });
        } else {
            var seeds = [dhts[Math.random() * dhts.length | 0].rpc.endpoint];
            spawnNode('local:' + nextPort, seeds, function (err, dht) {
                ++nextPort;
                if (err) return console.log('  ERROR! %s', err.message);
                dhts.push(dht);
                console.log('[master] NEW node! %s', dht.rpc.endpoint);
                doStuff(dhts, count - 1);
            });
        }
    }, (Math.random() * 20) | 0);
}

spawnSome([], [], 20, function (err, dhts) {
    if (err) throw err;
    doStuff(dhts, 200);
});<commit_msg>Tweak heavy test.<commit_after>'use strict';

var Dht = require('../lib/dht');
var MockRpc = require('../lib/mock-rpc');

// Spawn a node. A node is composed of two elements: the local Dht and the Rpc.
//
function spawnNode(endpoint, seeds, cb) {
    MockRpc.spawn(endpoint, function (err, rpc) {
        if (err) return cb(err);
        Dht.spawn(rpc, seeds, function (err, dht) {
            if (err) return cb(err);
            cb(err, dht);
        });
    });
}

var nextPort = '1000';

function spawnSome(dhts, seeds, nb, cb) {
    ++nextPort;
    spawnNode('local:' + nextPort, seeds, function (err, dht) {
        if (err) return cb(err);
        dhts.push(dht);
        --nb;
        if (nb === 0)
            return cb(null, dhts);
        if (seeds.length < 3)
            seeds.push(dht.rpc.endpoint);
        return spawnSome(dhts, seeds, nb, cb);
    });
}

var WORDS = ['foo', 'bar', 'baz', 'glo', 'ick'];
var locals = {};

function doStuff(dhts, count) {
    setTimeout(function () {
        if (count === 0) return;
        var key = WORDS[Math.random() * WORDS.length | 0];
        var ix = Math.random() * dhts.length | 0;
        if (Math.random() < 0.5) {
            var value = Math.random() * 256 | 0;
            console.log('[master] set %s=%d on #%s', key, value,
                        dhts[ix].rpc.endpoint);
            locals[key] = value;
            dhts[ix].set(key, value, function (err) {
                if (err) return console.log('  ERROR! %s', err.message);
                doStuff(dhts, count - 1);
            });
        } else if (Math.random() < 1) {
            console.log('[master] get %s on #%s', key, dhts[ix].rpc.endpoint);
            dhts[ix].get(key, function (err, value) {
                if (err) return console.log('  ERROR! %s', err.message);
                if (value !== locals[key]) {
                    console.log('/!\\ warning:' +
                                ' expected %s=%d but got %s', key,
                                locals[key], value);
                }
                doStuff(dhts, count - 1);
            });
        } else {
            var seeds = [dhts[Math.random() * dhts.length | 0].rpc.endpoint];
            spawnNode('local:' + nextPort, seeds, function (err, dht) {
                ++nextPort;
                if (err) return console.log('  ERROR! %s', err.message);
                dhts.push(dht);
                console.log('[master] NEW node! %s', dht.rpc.endpoint);
                doStuff(dhts, count - 1);
            });
        }
    }, (Math.random() * 20) | 0);
}

spawnSome([], [], 100, function (err, dhts) {
    if (err) throw err;
    doStuff(dhts, 200);
});
{'fraction_non_alphanumeric': 0.17073170731707318, 'fraction_numerical': 0.01090169992609017, 'mean_word_length': 1.6638779527559056, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2079613', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1782, 'n_words': 456}