Skip to content

Commit

Permalink
refactor(test): review suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: rgallor <[email protected]>
  • Loading branch information
rgallor committed Mar 22, 2024
1 parent fd41768 commit f334937
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ rust-version = { workspace = true }
[workspace]
resolver = "2"
members = [
"e2e-test", "e2e-test-forwarder",
"edgehog-device-runtime-docker",
"edgehog-device-runtime-forwarder",
"hardware-id-service",
"led-manager-service",
"e2e-test",
"e2e-test-forwarder",
"edgehog-device-runtime-docker",
"edgehog-device-runtime-forwarder",
"hardware-id-service",
"led-manager-service",
]

[workspace.package]
Expand Down
4 changes: 3 additions & 1 deletion e2e-test-forwarder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ name = "e2e-test-forwarder"
version = "0.1.0"
edition.workspace = true
homepage.workspace = true
publish = false
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { workspace = true, features = ["derive"] }
edgehog-forwarder = { workspace = true, features = ["_test-utils"] }
env_logger = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing = { workspace = true, features = ["log"] }
4 changes: 3 additions & 1 deletion e2e-test-forwarder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use clap::Parser;
use edgehog_forwarder::test_utils::con_manager;
use tokio::task::JoinSet;
use tracing::info;

Expand All @@ -23,7 +24,8 @@ struct Cli {

#[tokio::main]
async fn main() {
use edgehog_forwarder::test_utils::con_manager;
env_logger::init();

let Cli {
host,
port,
Expand Down
2 changes: 0 additions & 2 deletions edgehog-device-runtime-forwarder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ tokio = { workspace = true }
tokio-tungstenite = { workspace = true, features = ["rustls-tls-native-roots"] }
tracing = { workspace = true, features = ["log"] }
url = { workspace = true }
uuid = { workspace = true, optional = true }

[dev-dependencies]
httpmock = { workspace = true }
tokio = { workspace = true, features = ["test-util"] }
uuid = { workspace = true }

[features]
_test-utils = ["dep:httpmock", "tokio/test-util"]
12 changes: 6 additions & 6 deletions edgehog-device-runtime-forwarder/src/collection.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright 2024 SECO Mind Srl
// SPDX-License-Identifier: Apache-2.0

//! Connections' collection and respective methods.
//! Collection of connections and respective methods.
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};

use tokio::sync::mpsc::Sender;
use tracing::{debug, error, info, instrument, trace};
use tracing::{debug, error, instrument, trace};

use crate::connection::{Connection, ConnectionHandle};
use crate::connections_manager::Error;
Expand Down Expand Up @@ -52,15 +52,15 @@ impl Connections {
} = http;

// the HTTP message can't be an http response
let http_req = http_msg.into_req().ok_or_else(|| {
let Some(http_req) = http_msg.into_req() else {
error!("Http response should not be sent by Edgehog");
Error::WrongMessage(request_id.clone())
})?;
return Err(Error::WrongMessage(request_id));
};

// before executing the HTTP request, check if it is an Upgrade request.
// if so, handle it properly.
if http_req.is_ws_upgrade() {
info!("Connection upgrade");
debug!("Upgrade the HTTP connection to WS");
return self.add_ws(request_id, http_req);
}

Expand Down
12 changes: 6 additions & 6 deletions edgehog-device-runtime-forwarder/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tokio::task::JoinHandle;
use tokio_tungstenite::tungstenite::Message as TungMessage;
use tokio_tungstenite::WebSocketStream;
use tracing::{debug, instrument, warn};
use url::Url;
use url::{ParseError, Url};

/// Build a listener on a free port.
pub async fn bind_port() -> (TcpListener, u16) {
Expand Down Expand Up @@ -73,9 +73,9 @@ pub fn create_http_req(request_id: Vec<u8>, url: &str, body: Vec<u8>) -> TungMes
}

/// Create an HTTP upgrade request and wrap it into a [`tungstenite`](tokio_tungstenite::tungstenite) message.
pub fn create_http_upgrade_req(request_id: Vec<u8>, url: &str) -> TungMessage {
let url = Url::parse(url).expect("failed to pars Url");
let port = url.port().expect("nonexistent port").into();
pub fn create_http_upgrade_req(request_id: Vec<u8>, url: &str) -> Result<TungMessage, ParseError> {
let url = Url::parse(url)?;
let port = url.port().ok_or(ParseError::InvalidPort)?.into();

let mut headers = HashMap::new();
headers.insert("Host".to_string(), format!("localhost:{port}"));
Expand Down Expand Up @@ -107,9 +107,9 @@ pub fn create_http_upgrade_req(request_id: Vec<u8>, url: &str) -> TungMessage {
};

let mut buf = Vec::with_capacity(proto_msg.encoded_len());
proto_msg.encode(&mut buf).unwrap();
proto_msg.encode(&mut buf).expect("not enough capacity");

TungMessage::Binary(buf)
Ok(TungMessage::Binary(buf))
}

/// Check if the protobuf message contains an HTTP response upgrade
Expand Down
3 changes: 2 additions & 1 deletion edgehog-device-runtime-forwarder/tests/ws_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async fn test_internal_ws() {
"ws://localhost:{}/remote-terminal?session=abcd",
test_connections.mock_server.port().unwrap()
);
let http_req = create_http_upgrade_req(request_id.clone(), &url);
let http_req = create_http_upgrade_req(request_id.clone(), &url)
.expect("failed to create http upgrade request");

let protobuf_res = send_ws_and_wait_next(&mut ws_bridge, http_req).await;

Expand Down

0 comments on commit f334937

Please sign in to comment.