-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This provides a barebones chat client used for testing within cucumber. The client is relatively isolated from the test suite itself and could be expanded on in the future.
- Loading branch information
Showing
9 changed files
with
497 additions
and
10 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,44 @@ | ||
# Copyright 2023 The Tari Project | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
Feature: Chat messaging | ||
|
||
Scenario: A message is propagated between nodes | ||
Given I have a seed node SEED_A | ||
When I have a chat client CHAT_A connected to seed node SEED_A | ||
When I have a chat client CHAT_B connected to seed node SEED_A | ||
When I wait 5 seconds | ||
When I use CHAT_A to send a message 'Hey there' to CHAT_B | ||
When I wait 5 seconds | ||
Then CHAT_B will have 1 message with CHAT_A | ||
|
||
Scenario: A message is sent directly between nodes | ||
Given I have a seed node SEED_A | ||
When I have a chat client CHAT_A connected to seed node SEED_A | ||
When I have a chat client CHAT_B connected to seed node SEED_A | ||
When I add CHAT_B as a contact to CHAT_A | ||
When I wait 5 seconds | ||
When I use CHAT_A to send a message 'Hey there' to CHAT_B | ||
When I wait 5 seconds | ||
Then CHAT_B will have 1 message with CHAT_A | ||
|
||
Scenario: Message counts are distinct | ||
Given I have a seed node SEED_A | ||
When I have a chat client CHAT_A connected to seed node SEED_A | ||
When I have a chat client CHAT_B connected to seed node SEED_A | ||
When I have a chat client CHAT_C connected to seed node SEED_A | ||
When I wait 5 seconds | ||
When I use CHAT_A to send a message 'Message 1ab' to CHAT_B | ||
When I use CHAT_A to send a message 'Message 2ab' to CHAT_B | ||
When I use CHAT_C to send a message 'Message 1cb' to CHAT_B | ||
When I use CHAT_A to send a message 'Message 1ac' to CHAT_C | ||
When I use CHAT_B to send a message 'Message 1bc' to CHAT_C | ||
When I use CHAT_B to send a message 'Message 2bc' to CHAT_C | ||
When I wait 5 seconds | ||
Then CHAT_B will have 2 messages with CHAT_A | ||
Then CHAT_B will have 3 messages with CHAT_C | ||
Then CHAT_C will have 1 messages with CHAT_A | ||
Then CHAT_C will have 3 messages with CHAT_B | ||
Then CHAT_A will have 2 messages with CHAT_B | ||
Then CHAT_A will have 1 messages with CHAT_C | ||
|
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,23 @@ | ||
[package] | ||
name = "tari_chat_client" | ||
authors = ["The Tari Development Community"] | ||
description = "Tari cucumber chat client" | ||
license = "BSD-3-Clause" | ||
version = "0.48.0-pre.1" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
tari_common_types = { path = "../../../../base_layer/common_types" } | ||
tari_common_sqlite = { path = "../../../../common_sqlite" } | ||
tari_comms = { path = "../../../../comms/core" } | ||
tari_comms_dht = { path = "../../../../comms/dht" } | ||
tari_contacts = { path = "../../../../base_layer/contacts" } | ||
tari_p2p = { path = "../../../../base_layer/p2p" } | ||
tari_service_framework= { path = "../../../../base_layer/service_framework" } | ||
tari_shutdown = { path = "../../../../infrastructure/shutdown" } | ||
tari_test_utils = { path = "../../../../infrastructure/test_utils" } | ||
tari_storage = { path = "../../../../infrastructure/storage" } | ||
|
||
anyhow = "1.0.41" | ||
diesel = { version = "2.0.3", features = ["sqlite", "r2d2", "serde_json", "chrono", "64-column-tables"] } | ||
lmdb-zero = "0.4.4" |
149 changes: 149 additions & 0 deletions
149
integration_tests/tests/utils/chat_client/src/client.rs
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,149 @@ | ||
// Copyright 2023. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
use std::{ | ||
fmt::{Debug, Formatter}, | ||
path::PathBuf, | ||
sync::Arc, | ||
time::Duration, | ||
}; | ||
|
||
use tari_common_types::tari_address::TariAddress; | ||
use tari_comms::{peer_manager::Peer, CommsNode, NodeIdentity}; | ||
use tari_contacts::contacts_service::{ | ||
handle::ContactsServiceHandle, | ||
types::{Message, MessageBuilder}, | ||
}; | ||
use tari_shutdown::Shutdown; | ||
|
||
use crate::{database, networking}; | ||
|
||
#[derive(Clone)] | ||
pub struct Client { | ||
pub identity: Arc<NodeIdentity>, | ||
pub base_dir: PathBuf, | ||
pub seed_peers: Vec<Peer>, | ||
pub shutdown: Shutdown, | ||
pub contacts: Option<ContactsServiceHandle>, | ||
} | ||
|
||
impl Debug for Client { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("Client") | ||
.field("identity", &self.identity) | ||
.field("base_dir", &self.base_dir) | ||
.field("seed_peers", &self.seed_peers) | ||
.field("shutdown", &self.shutdown) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Drop for Client { | ||
fn drop(&mut self) { | ||
self.quit(); | ||
} | ||
} | ||
|
||
impl Client { | ||
pub fn new(identity: NodeIdentity, seed_peers: Vec<Peer>, base_dir: PathBuf) -> Self { | ||
Self { | ||
identity: Arc::new(identity), | ||
base_dir, | ||
seed_peers, | ||
shutdown: Shutdown::new(), | ||
contacts: None, | ||
} | ||
} | ||
|
||
pub async fn add_contact(&self, address: &TariAddress) { | ||
if let Some(mut contacts_service) = self.contacts.clone() { | ||
contacts_service | ||
.upsert_contact(address.into()) | ||
.await | ||
.expect("Contact wasn't added"); | ||
} | ||
} | ||
|
||
pub async fn send_message(&self, receiver: TariAddress, message: String) { | ||
if let Some(mut contacts_service) = self.contacts.clone() { | ||
contacts_service | ||
.send_message(MessageBuilder::new().message(message).address(receiver).build()) | ||
.await | ||
.expect("Message wasn't sent"); | ||
} | ||
} | ||
|
||
pub async fn get_messages(&self, sender: TariAddress) -> Vec<Message> { | ||
let mut messages = vec![]; | ||
if let Some(mut contacts_service) = self.contacts.clone() { | ||
messages = contacts_service | ||
.get_messages(sender, 0, 0) | ||
.await | ||
.expect("Messages not fetched"); | ||
} | ||
|
||
messages | ||
} | ||
|
||
pub async fn initialize(&mut self) { | ||
println!("initializing chat"); | ||
|
||
let signal = self.shutdown.to_signal(); | ||
let db = database::create_chat_storage(self.base_dir.clone()).unwrap(); | ||
|
||
let (contacts, comms_node) = networking::start( | ||
self.identity.clone(), | ||
self.base_dir.clone(), | ||
self.seed_peers.clone(), | ||
db, | ||
signal, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
if !self.seed_peers.is_empty() { | ||
loop { | ||
println!("Waiting for peer connections..."); | ||
match wait_for_connectivity(comms_node.clone()).await { | ||
Ok(_) => break, | ||
Err(e) => println!("{}. Still waiting...", e), | ||
} | ||
} | ||
} | ||
|
||
self.contacts = Some(contacts); | ||
|
||
println!("Connections established") | ||
} | ||
|
||
pub fn quit(&mut self) { | ||
self.shutdown.trigger(); | ||
} | ||
} | ||
|
||
pub async fn wait_for_connectivity(comms: CommsNode) -> anyhow::Result<()> { | ||
comms | ||
.connectivity() | ||
.wait_for_connectivity(Duration::from_secs(30)) | ||
.await?; | ||
Ok(()) | ||
} |
Oops, something went wrong.