diff --git a/pubky/src/native.rs b/pubky/src/native.rs index a72fd85..1e917f8 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}; @@ -26,18 +27,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 } @@ -79,14 +91,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); + } }