From e496f7a821828e441ea8a92ccfa87646c1c3f16c Mon Sep 17 00:00:00 2001 From: nazeh Date: Wed, 4 Sep 2024 10:53:03 +0300 Subject: [PATCH 1/2] test(pubky): put event get sequence --- pubky/src/native.rs | 4 +-- pubky/src/shared/public.rs | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/pubky/src/native.rs b/pubky/src/native.rs index 7af0a05..7d96369 100644 --- a/pubky/src/native.rs +++ b/pubky/src/native.rs @@ -74,14 +74,14 @@ impl PubkyClient { /// Creates a [PubkyClient] with: /// - DHT bootstrap nodes set to the `testnet` bootstrap nodes. - /// - DHT request timout set to 500 milliseconds. (unless in CI, then it is left as default) + /// - DHT request timout set to 500 milliseconds. (unless in CI, then it is left as default 2000) /// /// For more control, you can use [PubkyClientBuilder::testnet] pub fn test(testnet: &Testnet) -> PubkyClient { let mut builder = PubkyClient::builder().testnet(testnet); if std::env::var("CI").is_err() { - builder = builder.dht_request_timeout(Duration::from_millis(100)); + builder = builder.dht_request_timeout(Duration::from_millis(500)); } builder.build() diff --git a/pubky/src/shared/public.rs b/pubky/src/shared/public.rs index 10d8d91..febd1fe 100644 --- a/pubky/src/shared/public.rs +++ b/pubky/src/shared/public.rs @@ -715,4 +715,54 @@ mod tests { ) } } + + #[tokio::test] + async fn read_after_event() { + let testnet = Testnet::new(10); + let server = Homeserver::start_test(&testnet).await.unwrap(); + + let client = PubkyClient::test(&testnet); + + let keypair = Keypair::random(); + + client.signup(&keypair, &server.public_key()).await.unwrap(); + + let pubky = keypair.public_key(); + + let url = format!("pubky://{pubky}/pub/a.com/a.txt"); + + client.put(url.as_str(), &[0]).await.unwrap(); + + let feed_url = format!("http://localhost:{}/events/", server.port()); + let feed_url = feed_url.as_str(); + + let client = PubkyClient::test(&testnet); + + { + let response = client + .request( + Method::GET, + format!("{feed_url}?limit=10").as_str().try_into().unwrap(), + ) + .send() + .await + .unwrap(); + + let text = response.text().await.unwrap(); + let lines = text.split('\n').collect::>(); + + let cursor = lines.last().unwrap().split(" ").last().unwrap().to_string(); + + assert_eq!( + lines, + vec![ + format!("PUT pubky://{pubky}/pub/a.com/a.txt"), + format!("cursor: {cursor}",) + ] + ); + } + + let get = client.get(url.as_str()).await.unwrap(); + dbg!(get); + } } From aa935a37112830b2039aa3a1f7f130c606521af9 Mon Sep 17 00:00:00 2001 From: nazeh Date: Wed, 4 Sep 2024 12:57:52 +0300 Subject: [PATCH 2/2] test(pubky): add back resolvers in PubkyClient::test() --- pubky/src/native.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/pubky/src/native.rs b/pubky/src/native.rs index 7d96369..c1835ae 100644 --- a/pubky/src/native.rs +++ b/pubky/src/native.rs @@ -1,3 +1,4 @@ +use std::net::ToSocketAddrs; use std::time::Duration; use ::pkarr::{mainline::dht::Testnet, PkarrClient, PublicKey, SignedPacket}; @@ -30,18 +31,29 @@ impl PubkyClientBuilder { self } - /// Use the bootstrap nodes of a testnet, useful mostly in unit tests. - pub fn testnet(self, testnet: &Testnet) -> Self { - self.bootstrap(testnet.bootstrap.to_vec()) - } + /// Use the bootstrap nodes of a testnet, as the bootstrap nodes and + /// resolvers in the internal Pkarr client. + pub fn testnet(mut self, testnet: &Testnet) -> Self { + self.pkarr_settings.dht.bootstrap = testnet.bootstrap.to_vec().into(); + + self.pkarr_settings.resolvers = testnet + .bootstrap + .iter() + .flat_map(|resolver| resolver.to_socket_addrs()) + .flatten() + .collect::>() + .into(); - pub fn dht_request_timeout(mut self, timeout: Duration) -> Self { - self.pkarr_settings.dht.request_timeout = timeout.into(); self } - pub fn bootstrap(mut self, bootstrap: Vec) -> Self { - self.pkarr_settings.dht.bootstrap = bootstrap.into(); + /// Set the request_timeout of the UDP socket in the Mainline DHT client in + /// the internal Pkarr client. + /// + /// Useful to speed unit tests. + /// Defaults to 2 seconds. + pub fn dht_request_timeout(mut self, timeout: Duration) -> Self { + self.pkarr_settings.dht.request_timeout = timeout.into(); self }