-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add integration tests #7
Merged
PLeVasseur
merged 1 commit into
eclipse-uprotocol:main
from
ValtechMobility:add_integration_tests
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
cd tests/mosquitto | ||
docker compose up | ||
``` | ||
|
||
2. Set up your environment (for example with a config file at .cargo/config.toml) | ||
|
||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
- ./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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍