-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ValtechMobility/add_integration_tests
Add integration tests
- Loading branch information
Showing
6 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
mosquitto: | ||
image: eclipse-mosquitto:2.0 | ||
volumes: | ||
# read-only prevents the container changing file owners on the host | ||
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro | ||
ports: | ||
- 1883:1883 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
persistence false | ||
allow_anonymous true | ||
log_type all | ||
log_type debug | ||
log_dest stdout | ||
connection_messages true | ||
listener 1883 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{ | ||
str::FromStr, | ||
sync::{Arc, Mutex}, | ||
time::Duration, | ||
}; | ||
|
||
use tokio::time::sleep; | ||
use up_rust::{UMessageBuilder, UPayloadFormat, UTransport, UUri}; | ||
|
||
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 source = UUri::from_str("//Publisher/A8000/2/8A50").expect("Failed to create source"); | ||
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())), | ||
}); | ||
|
||
subscriber | ||
.register_listener(&source_filter, None, listener.clone()) | ||
.await | ||
.unwrap(); | ||
|
||
sleep(Duration::from_millis(1000)).await; | ||
|
||
let umessage = UMessageBuilder::publish(source) | ||
.build_with_payload(target_data, UPayloadFormat::UPAYLOAD_FORMAT_TEXT) | ||
.unwrap(); | ||
publisher.send(umessage).await.unwrap(); | ||
|
||
sleep(Duration::from_millis(1000)).await; | ||
|
||
assert_eq!(listener.get_recv_data(), target_data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use async_trait::async_trait; | ||
use up_client_mqtt5_rust::{MqttConfig, MqttProtocol, UPClientMqtt, UPClientMqttType}; | ||
use up_rust::{UListener, UMessage, UStatus, UUID}; | ||
|
||
pub struct TestListener { | ||
pub recv_data: Arc<Mutex<String>>, | ||
} | ||
|
||
impl TestListener { | ||
pub fn get_recv_data(&self) -> String { | ||
self.recv_data.lock().unwrap().to_string() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl UListener for TestListener { | ||
async fn on_receive(&self, message: UMessage) { | ||
let data = message.payload.unwrap(); | ||
let value = data.into_iter().map(|c| c as char).collect::<String>(); | ||
*self.recv_data.lock().unwrap() = value; | ||
} | ||
} | ||
|
||
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(), | ||
mqtt_port: 1883, | ||
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) | ||
} |