Skip to content

Commit 4e38408

Browse files
committed
tests: Fix API changes breaking tests
The tests have actually not worked due to API changes which were not updated yet in the tests modules. This was not noticed since we're not running tests in CI. This patch fixes the tests by: 1. Updating the tests to use the latest method signatures 2. Await results due to async behaviour of system
1 parent 7569fc5 commit 4e38408

6 files changed

Lines changed: 71 additions & 33 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

reflection-doc/Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ authors = [
88
]
99

1010
[dependencies]
11-
reflection-node = { path = "../reflection-node" }
1211
anyhow = "1.0.101"
13-
async-channel = "2.5.0"
14-
glib = "0.21"
1512
gio = "0.21"
13+
glib = "0.21"
14+
hex = "0.4.3"
15+
indexmap = "2.13.0"
1616
loro = { tag = "loro-crdt@1.10.6", git = "https://github.com/loro-dev/loro.git" }
17+
rand = "0.10.0"
18+
reflection-node = { path = "../reflection-node" }
19+
serde = { version = "1.0.228", features = ["derive"] }
1720
thiserror = "2.0.18"
1821
tracing = "0.1"
22+
23+
[dev-dependencies]
24+
reflection-node = { path = "../reflection-node", features = ["test_utils"] }
1925
test-log = { version = "0.2.19", default-features = false, features = ["trace", "color"] }
20-
serde = { version = "1.0.228", features = ["derive"] }
21-
rand = "0.10.0"
22-
hex = "0.4.3"
23-
indexmap = "2.13.0"

reflection-doc/src/lib.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ mod tests {
8989
let test_string = "Hello World";
9090

9191
let context = glib::MainContext::ref_thread_default();
92-
println!("Context: {context:?}");
9392

9493
let private_key = PrivateKey::new();
9594
let service = Service::new(&private_key, None);
@@ -106,10 +105,9 @@ mod tests {
106105

107106
#[test_log::test(glib::async_test)]
108107
async fn basic_sync() {
109-
let test_string = "Hello World";
108+
let expected_string = "Hello World";
110109

111110
let context = glib::MainContext::ref_thread_default();
112-
println!("Context: {context:?}");
113111

114112
let private_key = PrivateKey::new();
115113
let service = Service::new(&private_key, None);
@@ -128,21 +126,29 @@ mod tests {
128126

129127
assert_eq!(document.id(), document2.id());
130128

131-
assert!(document.insert_text(0, test_string).is_ok());
132-
assert_eq!(document.text(), test_string);
129+
assert!(document.insert_text(0, expected_string).is_ok());
130+
assert_eq!(document.text(), expected_string);
131+
132+
// Wait until text got synced.
133+
loop {
134+
glib::timeout_future(std::time::Duration::from_millis(50)).await;
135+
136+
if document2.text() == expected_string {
137+
break;
138+
}
139+
}
133140

134141
service.shutdown().await;
135142
service2.shutdown().await;
136143

137-
assert_eq!(document2.text(), test_string);
144+
assert_eq!(document2.text(), expected_string);
138145
}
139146

140147
#[test_log::test(glib::async_test)]
141148
async fn sync_multiple_changes() {
142149
let expected_string = "Hello, World!";
143150

144151
let context = glib::MainContext::ref_thread_default();
145-
println!("Context: {context:?}");
146152

147153
let private_key = PrivateKey::new();
148154
let service = Service::new(&private_key, None);
@@ -167,6 +173,15 @@ mod tests {
167173
assert!(document.insert_text(7, "W").is_ok());
168174
assert_eq!(document.text(), expected_string);
169175

176+
// Wait until text got synced.
177+
loop {
178+
glib::timeout_future(std::time::Duration::from_millis(50)).await;
179+
180+
if document2.text() == expected_string {
181+
break;
182+
}
183+
}
184+
170185
service.shutdown().await;
171186
service2.shutdown().await;
172187

@@ -175,16 +190,16 @@ mod tests {
175190

176191
#[test_log::test(glib::async_test)]
177192
async fn sync_longer_text() {
178-
let test_string = "Et aut omnis eos corporis ut. Qui est blanditiis blanditiis.
179-
Sit quia nam maxime accusantium ut voluptatem. Fuga consequuntur animi et et est.
180-
Unde voluptas consequatur mollitia id odit optio harum sint. Fugit quo aut et laborum aut cupiditate.";
193+
let test_string = "Et aut omnis eos corporis ut. Qui est blanditiis blanditiis. Sit quia
194+
nam maxime accusantium ut voluptatem. Fuga consequuntur animi et et est. Unde voluptas
195+
consequatur mollitia id odit optio harum sint. Fugit quo aut et laborum aut cupiditate.";
196+
181197
let expected_string = format!(
182198
"{}{}{}{}",
183199
test_string, test_string, test_string, test_string
184200
);
185201

186202
let context = glib::MainContext::ref_thread_default();
187-
println!("Context: {context:?}");
188203

189204
let private_key = PrivateKey::new();
190205
let service = Service::new(&private_key, None);
@@ -209,6 +224,15 @@ mod tests {
209224
assert!(document.insert_text(0, test_string).is_ok());
210225
assert!(document.insert_text(0, test_string).is_ok());
211226

227+
// Wait until text got synced.
228+
loop {
229+
glib::timeout_future(std::time::Duration::from_millis(50)).await;
230+
231+
if document2.text() == expected_string {
232+
break;
233+
}
234+
}
235+
212236
service.shutdown().await;
213237
service2.shutdown().await;
214238

reflection-node/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ authors = [
88
"Julian Sparber <julian@sparber.net>"
99
]
1010

11+
[features]
12+
test_utils = []
13+
1114
[dependencies]
1215
thiserror = "2.0.18"
1316
chrono = "0.4.43"

reflection-node/src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ pub use topic::SubscribableTopic;
1515

1616
#[cfg(test)]
1717
mod tests {
18-
use crate::SubscribableTopic;
19-
use crate::node::{ConnectionMode, Node};
18+
use std::sync::Arc;
19+
2020
use p2panda_core::Hash;
2121
use p2panda_core::PrivateKey;
2222
use p2panda_core::PublicKey;
23-
use std::sync::Arc;
2423
use tokio::sync::{Mutex, mpsc};
2524

25+
use crate::node::ConnectionMode;
26+
use crate::node::Node;
27+
use crate::topic::SubscribableTopic;
28+
2629
#[tokio::test]
2730
#[test_log::test]
2831
async fn create_topic() {
2932
let private_key = PrivateKey::new();
3033
let network_id = Hash::new(b"reflection");
31-
let node = Node::new(private_key, network_id, None, ConnectionMode::Network)
32-
.await
33-
.unwrap();
34+
let node = Node::new(private_key, network_id, None).await.unwrap();
3435

3536
let id: [u8; 32] = [0; 32];
3637
let _sub = node.subscribe(id, TestTopic::new()).await;
@@ -70,14 +71,16 @@ mod tests {
7071
fn author_joined(&self, _author: PublicKey) {}
7172
fn author_left(&self, _author: PublicKey) {}
7273
fn ephemeral_bytes_received(&self, _author: PublicKey, _data: Vec<u8>) {}
74+
fn error(&self, _error: crate::topic::SubscriptionError) {}
7375
}
7476

7577
#[tokio::test]
7678
#[test_log::test]
7779
async fn subscribe_topic() {
7880
let private_key = PrivateKey::new();
7981
let network_id = Hash::new(b"reflection");
80-
let node = Node::new(private_key, network_id, None, ConnectionMode::Network)
82+
let node = Node::new(private_key, network_id, None).await.unwrap();
83+
node.set_connection_mode(ConnectionMode::Network)
8184
.await
8285
.unwrap();
8386

@@ -92,7 +95,9 @@ mod tests {
9295

9396
let private_key2 = PrivateKey::new();
9497
let network_id2 = Hash::new(b"reflection");
95-
let node2 = Node::new(private_key2, network_id2, None, ConnectionMode::Network)
98+
let node2 = Node::new(private_key2, network_id2, None).await.unwrap();
99+
node2
100+
.set_connection_mode(ConnectionMode::Network)
96101
.await
97102
.unwrap();
98103

reflection-node/src/network.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,21 @@ impl Network {
8282
) -> Result<Self, NetworkError> {
8383
let address_book = AddressBook::builder().spawn().await?;
8484

85-
if let Err(error) = address_book.insert_node_info(BOOTSTRAP_NODE.clone()).await {
85+
if cfg!(not(any(test, feature = "test_utils")))
86+
&& let Err(error) = address_book.insert_node_info(BOOTSTRAP_NODE.clone()).await
87+
{
8688
error!("Failed to add bootstrap node to the address book: {error}");
8789
}
8890

89-
let endpoint = Endpoint::builder(address_book.clone())
91+
let mut builder = Endpoint::builder(address_book.clone())
9092
.network_id(network_id.into())
91-
.private_key(private_key.clone())
92-
.relay_url(RELAY_URL.clone())
93-
.spawn()
94-
.await?;
93+
.private_key(private_key.clone());
94+
95+
if cfg!(not(any(test, feature = "test_utils"))) {
96+
builder = builder.relay_url(RELAY_URL.clone());
97+
}
98+
99+
let endpoint = builder.spawn().await?;
95100

96101
let mdns_discovery = MdnsDiscovery::builder(address_book.clone(), endpoint.clone())
97102
.mode(MdnsDiscoveryMode::Active)

0 commit comments

Comments
 (0)