Skip to content

Commit

Permalink
fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
ValMobBIllich committed Oct 31, 2024
1 parent 5be41f3 commit 1ea6681
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ cargo test

### Running the Examples

1. Start an MQTT broker (e.g. mosquitto)
1. Start an MQTT broker or use the included Mosquitto broker:
```bash
cd tests/mosquitto
docker compose up
```

2. Set up your environment (for example with a config file at .cargo/config.toml)

Expand Down
35 changes: 23 additions & 12 deletions tests/publish_subscribe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{str::FromStr, sync::{Arc, Mutex}, time::Duration};
use std::{
str::FromStr,
sync::{Arc, Mutex},
time::Duration,
};

use tokio::time::sleep;
use up_rust::{UMessageBuilder, UPayloadFormat, UTransport, UUri};
Expand All @@ -7,20 +11,28 @@ mod test_lib;

#[tokio::test(flavor = "multi_thread")]
async fn test_publish_and_subscribe() {

let target_data = "TEST";

let publisher = test_lib::create_up_transport_mqtt("Publisher").await.unwrap();
let subscriber = test_lib::create_up_transport_mqtt("Subscriber").await.unwrap();
let publisher = test_lib::create_up_transport_mqtt("Publisher")
.await
.unwrap();
let subscriber = test_lib::create_up_transport_mqtt("Subscriber")
.await
.unwrap();

let source = UUri::from_str("//Publisher/A8000/2/8A50").expect("Failed to create source filter");
let source_filter = UUri::from_str("//Publisher/A8000/2/8A50").expect("Failed to create source filter");
let source =
UUri::from_str("//Publisher/A8000/2/8A50").expect("Failed to create source filter");
let source_filter =
UUri::from_str("//Publisher/A8000/2/8A50").expect("Failed to create source filter");

let listener = Arc::new(
test_lib::TestListener { recv_data: Arc::new(Mutex::new(String::new())) }
);
let listener = Arc::new(test_lib::TestListener {
recv_data: Arc::new(Mutex::new(String::new())),
});

subscriber.register_listener(&source_filter, None, listener.clone()).await.unwrap();
subscriber
.register_listener(&source_filter, None, listener.clone())
.await
.unwrap();

sleep(Duration::from_millis(1000)).await;

Expand All @@ -32,5 +44,4 @@ async fn test_publish_and_subscribe() {
sleep(Duration::from_millis(1000)).await;

assert_eq!(listener.get_recv_data(), target_data)

}
}
29 changes: 3 additions & 26 deletions tests/test_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use up_client_mqtt5_rust::{MqttConfig, MqttProtocol, UPClientMqtt, UPClientMqttT
use up_rust::{UListener, UMessage, UStatus, UUID};

pub struct TestListener {
pub recv_data: Arc<Mutex<String>>
pub recv_data: Arc<Mutex<String>>,
}

impl TestListener {
Expand All @@ -24,7 +24,6 @@ impl UListener for TestListener {
}

pub async fn create_up_transport_mqtt(authority_name: &str) -> Result<UPClientMqtt, UStatus> {

let config = MqttConfig {
mqtt_protocol: MqttProtocol::Mqtt,
mqtt_hostname: "localhost".to_string(),
Expand All @@ -41,30 +40,8 @@ pub async fn create_up_transport_mqtt(authority_name: &str) -> Result<UPClientMq
UUID::build(),
authority_name.to_string(),
UPClientMqttType::Device,
).await?;
)
.await?;

Ok(client)
}

pub async fn create_up_transport_mqtts(authority_name: &str) -> Result<UPClientMqtt, UStatus> {

let config = MqttConfig {
mqtt_protocol: MqttProtocol::Mqtt,
mqtt_hostname: "localhost".to_string(),
mqtt_port: 8883,
max_buffered_messages: 100,
max_subscriptions: 100,
session_expiry_interval: 3600,
ssl_options: None,
username: "testuser".to_string(),
};

let client = UPClientMqtt::new(
config,
UUID::build(),
authority_name.to_string(),
UPClientMqttType::Device,
).await?;

Ok(client)
}

0 comments on commit 1ea6681

Please sign in to comment.