Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh 349 new #3

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 53 additions & 8 deletions masq/src/communications/broadcast_handler.rs
Original file line number Diff line number Diff line change
@@ -2,15 +2,16 @@

use crate::commands::change_password_command::ChangePasswordCommand;
use crate::commands::setup_command::SetupCommand;
use crate::communications::{
handle_node_is_dead_while_f_f_on_the_way_broadcast, handle_unrecognized_broadcast,
use crate::communications::broadcast_tools::tools::{
handle_node_is_dead_while_f_f_on_the_way_broadcast, handle_ui_log_broadcast,
handle_unrecognized_broadcast,
};
use crate::notifications::crashed_notification::CrashNotifier;
use crate::terminal::terminal_interface::TerminalWrapper;
use crossbeam_channel::{unbounded, RecvError, Sender};
use masq_lib::messages::{
FromMessageBody, UiNewPasswordBroadcast, UiNodeCrashedBroadcast, UiSetupBroadcast,
UiUndeliveredFireAndForget,
FromMessageBody, UiLogBroadcast, UiNewPasswordBroadcast, UiNodeCrashedBroadcast,
UiSetupBroadcast, UiUndeliveredFireAndForget,
};
use masq_lib::ui_gateway::MessageBody;
use masq_lib::utils::ExpectValue;
@@ -106,6 +107,8 @@ impl BroadcastHandlerReal {
stdout,
terminal_interface,
);
} else if let Ok((body, _)) = UiLogBroadcast::fmb(message_body.clone()) {
handle_ui_log_broadcast(body, stdout, terminal_interface)
} else {
handle_unrecognized_broadcast(message_body, stderr, terminal_interface)
}
@@ -148,7 +151,9 @@ mod tests {
TerminalPassiveMock, TestStreamFactory,
};
use crossbeam_channel::{bounded, unbounded, Receiver};
use masq_lib::messages::{CrashReason, ToMessageBody, UiNodeCrashedBroadcast};
use masq_lib::messages::{
CrashReason, SerializableLogLevel, ToMessageBody, UiLogBroadcast, UiNodeCrashedBroadcast,
};
use masq_lib::messages::{UiSetupBroadcast, UiSetupResponseValue, UiSetupResponseValueStatus};
use masq_lib::ui_gateway::MessagePath;
use std::sync::Arc;
@@ -186,6 +191,34 @@ mod tests {
);
}

#[test]
fn broadcast_of_ui_log_was_successful() {
let (factory, handle) = TestStreamFactory::new();
let subject = BroadcastHandlerReal::new(Some(TerminalWrapper::new(Arc::new(
TerminalPassiveMock::new(),
))))
.start(Box::new(factory));
let message = masq_lib::messages::UiLogBroadcast {
msg: "Empty. No Nodes to report to; continuing".to_string(),
log_level: SerializableLogLevel::Info,
}
.tmb(0);

subject.send(message);

let stdout = handle.stdout_so_far();
assert_eq!(
stdout,
"\nInfo: Empty. No Nodes to report to; continuing\n\n",
);
assert_eq!(
handle.stderr_so_far(),
"".to_string(),
"stderr: '{}'",
stdout
);
}

#[test]
fn broadcast_of_crashed_triggers_correct_handler() {
let (factory, handle) = TestStreamFactory::new();
@@ -448,9 +481,21 @@ Cannot handle crash request: Node is not running.
)
}

#[test]
fn ui_log_broadcast_handle_has_a_synchronizer_correctly_implemented() {
let ui_log_broadcast = UiLogBroadcast {
msg: "Empty. No Nodes to report to; continuing".to_string(),
log_level: SerializableLogLevel::Info,
};

let broadcast_output = "\nInfo: Empty. No Nodes to report to; continuing\n\n";

assertion_for_handle_broadcast(handle_ui_log_broadcast, ui_log_broadcast, broadcast_output)
}

fn assertion_for_handle_broadcast<F, U>(
broadcast_handler: F,
broadcast_message_body: U,
broadcast_body: U,
broadcast_desired_output: &str,
) where
F: FnOnce(U, &mut dyn Write, &TerminalWrapper) + Copy,
@@ -470,7 +515,7 @@ Cannot handle crash request: Node is not running.
Box::new(stdout_clone),
synchronizer,
broadcast_handler,
broadcast_message_body.clone(),
broadcast_body.clone(),
rx.clone(),
);

@@ -492,7 +537,7 @@ Cannot handle crash request: Node is not running.
Box::new(stdout_second_clone),
synchronizer_clone_idle,
broadcast_handler,
broadcast_message_body,
broadcast_body,
rx,
);

46 changes: 46 additions & 0 deletions masq/src/communications/broadcast_tools.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.

pub(in crate::communications) mod tools {
use crate::terminal::terminal_interface::TerminalWrapper;
use masq_lib::messages::{UiLogBroadcast, UiUndeliveredFireAndForget};
use masq_lib::short_writeln;
use masq_lib::ui_gateway::MessageBody;
use std::io::Write;

pub fn handle_node_is_dead_while_f_f_on_the_way_broadcast(
body: UiUndeliveredFireAndForget,
stdout: &mut dyn Write,
term_interface: &TerminalWrapper,
) {
let _lock = term_interface.lock();
short_writeln!(
stdout,
"\nCannot handle {} request: Node is not running.\n",
body.opcode
);
stdout.flush().expect("flush failed");
}

pub fn handle_unrecognized_broadcast(
message_body: MessageBody,
stderr: &mut dyn Write,
term_interface: &TerminalWrapper,
) {
let _lock = term_interface.lock();
short_writeln!(
stderr,
"Discarding unrecognized broadcast with opcode '{}'\n",
message_body.opcode
)
}

pub fn handle_ui_log_broadcast(
body: UiLogBroadcast,
stdout: &mut dyn Write,
term_interface: &TerminalWrapper,
) {
let _lock = term_interface.lock();
write!(stdout, "\n{:?}: {}\n\n", body.log_level, body.msg).expect("write! failed");
stdout.flush().expect("flush failed");
}
}
34 changes: 1 addition & 33 deletions masq/src/communications/mod.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,6 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.
pub mod broadcast_handler;
pub mod broadcast_tools;
mod client_listener_thread;
pub mod connection_manager;
pub mod node_conversation;

use crate::terminal::terminal_interface::TerminalWrapper;
use masq_lib::messages::UiUndeliveredFireAndForget;
use masq_lib::short_writeln;
use masq_lib::ui_gateway::MessageBody;
use std::io::Write;

fn handle_node_is_dead_while_f_f_on_the_way_broadcast(
body: UiUndeliveredFireAndForget,
stdout: &mut dyn Write,
term_interface: &TerminalWrapper,
) {
let _lock = term_interface.lock();
short_writeln!(
stdout,
"\nCannot handle {} request: Node is not running.\n",
body.opcode
);
stdout.flush().expect("flush failed");
}

fn handle_unrecognized_broadcast(
message_body: MessageBody,
stderr: &mut dyn Write,
term_interface: &TerminalWrapper,
) {
let _lock = term_interface.lock();
short_writeln!(
stderr,
"Discarding unrecognized broadcast with opcode '{}'\n",
message_body.opcode
)
}
1 change: 1 addition & 0 deletions masq_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ websocket = {version = "0.26.2", default-features = false, features = ["sync"]}

[features]
no_test_share = []
log_recipient_test = []

[target.'cfg(not(target_os = "windows"))'.dependencies]
nix = "0.23.0"
Loading