<commit_before>// Copyright 2015 The Delix Project Authors. See the AUTHORS file at the top level directory.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

extern crate delix;

#[allow(dead_code)] mod helper;

use std::thread;

use delix::node::{State, request};

#[test]
fn single_echo_from_local() {
    helper::set_up();

    let node = helper::build_node("127.0.0.1:3001", &[]);
    node.register("echo", Box::new(|request| Ok(request.to_vec())))
            .unwrap();

    thread::sleep_ms(100);
    helper::assert_node(&node, State::Discovering, 0);

    let response = node.request("echo", b"test message").unwrap();
    assert_eq!(b"test message".to_vec(), response);
}

#[test]
fn single_echo_from_remote() {
    helper::set_up();

    let node_one = helper::build_node("127.0.0.1:3011", &[]);
    node_one.register("echo", Box::new(|request| Ok(request.to_vec())))
            .unwrap();

    let node_two = helper::build_node("127.0.0.1:3012", &["127.0.0.1:3011"]);

    thread::sleep_ms(1000);
    helper::assert_node(&node_one, State::Joined, 1);
    helper::assert_node(&node_two, State::Joined, 1);

    let response = node_two.request("echo", b"test message").unwrap();
    assert_eq!(b"test message".to_vec(), response);
}

#[test]
fn multiple_echos_from_remote() {
    helper::set_up();

    let node_one = helper::build_node("127.0.0.1:3021", &[]);
    node_one.register("echo", Box::new(|request| Ok(request.to_vec())))
            .unwrap();

    let node_two = helper::build_node("127.0.0.1:3022", &["127.0.0.1:3021"]);

    thread::sleep_ms(1000);
    helper::assert_node(&node_one, State::Joined, 1);
    helper::assert_node(&node_two, State::Joined, 1);
    assert_eq!(1, node_one.service_count());
    assert_eq!(1, node_two.service_count());

    assert_eq!(b"test message one".to_vec(), node_two.request("echo", b"test message one").unwrap());
    assert_eq!(b"test message two".to_vec(), node_two.request("echo", b"test message two").unwrap());
}

#[test]
fn balanced_echos_from_two_remotes() {
    helper::set_up();

    let node_one = helper::build_node("127.0.0.1:3031", &[]);
    node_one.register("echo", Box::new(|_| {
        Err(request::Error::TestDuration(b"40".to_vec()))
    })).unwrap();

    let node_two = helper::build_node("127.0.0.1:3032", &["127.0.0.1:3031"]);
    node_two.register("echo", Box::new(|_| {
        Err(request::Error::TestDuration(b"100".to_vec()))
    })).unwrap();

    let node_three = helper::build_node("127.0.0.1:3033", &["127.0.0.1:3031"]);

    thread::sleep_ms(1000);
    helper::assert_node(&node_one, State::Joined, 2);
    helper::assert_node(&node_two, State::Joined, 2);
    helper::assert_node(&node_three, State::Joined, 2);
    assert_eq!(1, node_one.service_count());
    assert_eq!(1, node_two.service_count());
    assert_eq!(1, node_three.service_count());

    println!("--- round ---");

    // in the first round the balancer has no statistic, so every serivice gets a request in order.
    assert_eq!("40", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));
    assert_eq!("100", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));

    println!("--- round ---");

    // in the second round the balancer can access some respond time statistic, so this round
    // contains two requests to node one and one to node two.
    assert_eq!("40", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));
    assert_eq!("40", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));
    assert_eq!("100", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));

    println!("--- round ---");

    // if a service deregisters in the middle of a round, the changes should be processed
    assert_eq!("40", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));
    node_two.deregister("echo").unwrap();
    assert_eq!("40", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));
    assert_eq!("40", String::from_utf8_lossy(&node_three.request("echo", b"").unwrap()));

    println!("--- round ---");
}<commit_msg>restructured test<commit_after>// Copyright 2015 The Delix Project Authors. See the AUTHORS file at the top level directory.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

extern crate delix;

#[allow(dead_code)] mod helper;

use std::thread;

use delix::node::{State, request};

#[test]
fn single_echo_from_local() {
    helper::set_up();

    let node = helper::build_node("127.0.0.1:3001", &[]);
    node.register("echo", Box::new(|request| Ok(request.to_vec())))
            .unwrap();

    thread::sleep_ms(100);
    helper::assert_node(&node, State::Discovering, 0);

    let response = node.request("echo", b"test message").unwrap();
    assert_eq!(b"test message".to_vec(), response);
}

#[test]
fn single_echo_from_remote() {
    helper::set_up();

    let node_one = helper::build_node("127.0.0.1:3011", &[]);
    node_one.register("echo", Box::new(|request| Ok(request.to_vec())))
            .unwrap();

    let node_two = helper::build_node("127.0.0.1:3012", &["127.0.0.1:3011"]);

    thread::sleep_ms(1000);
    helper::assert_node(&node_one, State::Joined, 1);
    helper::assert_node(&node_two, State::Joined, 1);

    let response = node_two.request("echo", b"test message").unwrap();
    assert_eq!(b"test message".to_vec(), response);
}

#[test]
fn multiple_echos_from_remote() {
    helper::set_up();

    let node_one = helper::build_node("127.0.0.1:3021", &[]);
    node_one.register("echo", Box::new(|request| Ok(request.to_vec())))
            .unwrap();

    let node_two = helper::build_node("127.0.0.1:3022", &["127.0.0.1:3021"]);

    thread::sleep_ms(1000);
    helper::assert_node(&node_one, State::Joined, 1);
    helper::assert_node(&node_two, State::Joined, 1);
    assert_eq!(1, node_one.service_count());
    assert_eq!(1, node_two.service_count());

    assert_eq!(b"test message one".to_vec(), node_two.request("echo", b"test message one").unwrap());
    assert_eq!(b"test message two".to_vec(), node_two.request("echo", b"test message two").unwrap());
}

#[test]
fn balanced_echos_from_two_remotes() {
    helper::set_up();

    let node_one = helper::build_node("127.0.0.1:3031", &[]);

    let node_two = helper::build_node("127.0.0.1:3032", &["127.0.0.1:3031"]);
    node_two.register("echo", Box::new(|_| {
        Err(request::Error::TestDuration(b"40".to_vec()))
    })).unwrap();

    thread::sleep_ms(1000);
    helper::assert_node(&node_one, State::Joined, 1);
    helper::assert_node(&node_two, State::Joined, 1);

    let node_three = helper::build_node("127.0.0.1:3033", &["127.0.0.1:3031"]);
    node_three.register("echo", Box::new(|_| {
        Err(request::Error::TestDuration(b"100".to_vec()))
    })).unwrap();

    thread::sleep_ms(1000);
    helper::assert_node(&node_one, State::Joined, 2);
    helper::assert_node(&node_two, State::Joined, 2);
    helper::assert_node(&node_three, State::Joined, 2);
    assert_eq!(1, node_one.service_count());
    assert_eq!(1, node_two.service_count());
    assert_eq!(1, node_three.service_count());

    println!("--- round ---");

    // in the first round the balancer has no statistic, so every serivice gets a request in order.
    assert_eq!("40", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));
    assert_eq!("100", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));

    println!("--- round ---");

    // in the second round the balancer can access some respond time statistic, so this round
    // contains two requests to node one and one to node two.
    assert_eq!("40", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));
    assert_eq!("40", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));
    assert_eq!("100", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));

    println!("--- round ---");

    // if a service deregisters in the middle of a round, the changes should be processed
    assert_eq!("40", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));
    node_three.deregister("echo").unwrap();
    assert_eq!("40", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));
    assert_eq!("40", String::from_utf8_lossy(&node_one.request("echo", b"").unwrap()));

    println!("--- round ---");
}
{'fraction_non_alphanumeric': 0.19143927233814875, 'fraction_numerical': 0.04066345639379347, 'mean_word_length': 4.414831981460023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 24, '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': '2176001', 'n_tokens_mistral': 3326, 'n_tokens_neox': 3158, 'n_words': 664}