|
| 1 | +#![cfg(all(feature = "net_protocol", feature = "rpc"))] |
| 2 | +use futures_lite::StreamExt; |
| 3 | +use futures_util::Stream; |
| 4 | +use iroh::Endpoint; |
| 5 | +use iroh_blobs::{ |
| 6 | + net_protocol::Blobs, |
| 7 | + rpc::{ |
| 8 | + client::tags::{self, TagInfo}, |
| 9 | + proto::RpcService, |
| 10 | + }, |
| 11 | + BlobFormat, Hash, |
| 12 | +}; |
| 13 | +use testresult::TestResult; |
| 14 | + |
| 15 | +async fn to_vec<T>(stream: impl Stream<Item = anyhow::Result<T>>) -> anyhow::Result<Vec<T>> { |
| 16 | + let res = stream.collect::<Vec<_>>().await; |
| 17 | + res.into_iter().collect::<anyhow::Result<Vec<_>>>() |
| 18 | +} |
| 19 | + |
| 20 | +fn expected(tags: impl IntoIterator<Item = &'static str>) -> Vec<TagInfo> { |
| 21 | + tags.into_iter() |
| 22 | + .map(|tag| TagInfo { |
| 23 | + name: tag.into(), |
| 24 | + hash: Hash::new(tag), |
| 25 | + format: BlobFormat::Raw, |
| 26 | + }) |
| 27 | + .collect() |
| 28 | +} |
| 29 | + |
| 30 | +async fn tags_smoke<C: quic_rpc::Connector<RpcService>>(tags: tags::Client<C>) -> TestResult<()> { |
| 31 | + tags.set("a", Hash::new("a")).await?; |
| 32 | + tags.set("b", Hash::new("b")).await?; |
| 33 | + tags.set("c", Hash::new("c")).await?; |
| 34 | + tags.set("d", Hash::new("d")).await?; |
| 35 | + tags.set("e", Hash::new("e")).await?; |
| 36 | + let stream = tags.list().await?; |
| 37 | + let res = to_vec(stream).await?; |
| 38 | + assert_eq!(res, expected(["a", "b", "c", "d", "e"])); |
| 39 | + |
| 40 | + let stream = tags.list_range("b".."d").await?; |
| 41 | + let res = to_vec(stream).await?; |
| 42 | + assert_eq!(res, expected(["b", "c"])); |
| 43 | + |
| 44 | + let stream = tags.list_range("b"..).await?; |
| 45 | + let res = to_vec(stream).await?; |
| 46 | + assert_eq!(res, expected(["b", "c", "d", "e"])); |
| 47 | + |
| 48 | + let stream = tags.list_range(.."d").await?; |
| 49 | + let res = to_vec(stream).await?; |
| 50 | + assert_eq!(res, expected(["a", "b", "c"])); |
| 51 | + |
| 52 | + let stream = tags.list_range(..="d").await?; |
| 53 | + let res = to_vec(stream).await?; |
| 54 | + assert_eq!(res, expected(["a", "b", "c", "d"])); |
| 55 | + |
| 56 | + tags.delete_range("b"..).await?; |
| 57 | + let stream = tags.list().await?; |
| 58 | + let res = to_vec(stream).await?; |
| 59 | + assert_eq!(res, expected(["a"])); |
| 60 | + |
| 61 | + tags.delete_range(..="a").await?; |
| 62 | + let stream = tags.list().await?; |
| 63 | + let res = to_vec(stream).await?; |
| 64 | + assert_eq!(res, expected([])); |
| 65 | + |
| 66 | + tags.set("a", Hash::new("a")).await?; |
| 67 | + tags.set("aa", Hash::new("aa")).await?; |
| 68 | + tags.set("aaa", Hash::new("aaa")).await?; |
| 69 | + tags.set("aab", Hash::new("aab")).await?; |
| 70 | + tags.set("b", Hash::new("b")).await?; |
| 71 | + |
| 72 | + let stream = tags.list_prefix("aa").await?; |
| 73 | + let res = to_vec(stream).await?; |
| 74 | + assert_eq!(res, expected(["aa", "aaa", "aab"])); |
| 75 | + |
| 76 | + tags.delete_prefix("aa").await?; |
| 77 | + let stream = tags.list().await?; |
| 78 | + let res = to_vec(stream).await?; |
| 79 | + assert_eq!(res, expected(["a", "b"])); |
| 80 | + |
| 81 | + tags.delete_prefix("").await?; |
| 82 | + let stream = tags.list().await?; |
| 83 | + let res = to_vec(stream).await?; |
| 84 | + assert_eq!(res, expected([])); |
| 85 | + |
| 86 | + tags.set("a", Hash::new("a")).await?; |
| 87 | + tags.set("b", Hash::new("b")).await?; |
| 88 | + tags.set("c", Hash::new("c")).await?; |
| 89 | + |
| 90 | + tags.delete("b").await?; |
| 91 | + let stream = tags.list().await?; |
| 92 | + let res = to_vec(stream).await?; |
| 93 | + assert_eq!(res, expected(["a", "c"])); |
| 94 | + |
| 95 | + Ok(()) |
| 96 | +} |
| 97 | + |
| 98 | +#[tokio::test] |
| 99 | +async fn tags_smoke_mem() -> TestResult<()> { |
| 100 | + let endpoint = Endpoint::builder().bind().await?; |
| 101 | + let blobs = Blobs::memory().build(&endpoint); |
| 102 | + let client = blobs.client(); |
| 103 | + tags_smoke(client.tags()).await |
| 104 | +} |
| 105 | + |
| 106 | +#[tokio::test] |
| 107 | +async fn tags_smoke_fs() -> TestResult<()> { |
| 108 | + let td = tempfile::tempdir()?; |
| 109 | + let endpoint = Endpoint::builder().bind().await?; |
| 110 | + let blobs = Blobs::persistent(td.path().join("blobs.db")) |
| 111 | + .await? |
| 112 | + .build(&endpoint); |
| 113 | + let client = blobs.client(); |
| 114 | + tags_smoke(client.tags()).await |
| 115 | +} |
0 commit comments