-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_usage.rs
More file actions
82 lines (69 loc) · 2.57 KB
/
basic_usage.rs
File metadata and controls
82 lines (69 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Basic usage integration test for the Storage Rust bindings
//!
//! This test demonstrates how to create a Storage node, start it,
//! upload a file, download it, and then clean up.
use std::fs::File;
use std::io::Write;
use storage_bindings::{
download_stream, upload_file, DownloadStreamOptions, LogLevel, StorageConfig, StorageNode,
UploadOptions,
};
use tempfile::tempdir;
#[tokio::test(flavor = "multi_thread")]
async fn test_basic_usage() -> Result<(), Box<dyn std::error::Error>> {
let _ = env_logger::try_init();
let temp_dir = tempdir()?;
let file_path = temp_dir.path().join("example.txt");
let download_path = temp_dir.path().join("downloaded.txt");
// Create a test file to upload
let mut file = File::create(&file_path)?;
file.write_all(b"Hello, Storage! This is a test file for the Rust bindings.")?;
file.sync_all()?;
// Create and start a Storage node
let config = StorageConfig::new()
.log_level(LogLevel::Info)
.data_dir(temp_dir.path().join("storage_data"))
.storage_quota(100 * 1024 * 1024)
.max_peers(50)
.discovery_port(8090);
let node = StorageNode::new(config).await?;
node.start().await?;
// Upload the file
let upload_options = UploadOptions::new()
.filepath(&file_path)
.on_progress(|progress| {
println!(
"Upload progress: {} bytes ({}%)",
progress.bytes_uploaded,
(progress.percentage * 100.0) as u32
);
});
let upload_result = upload_file(&node, upload_options).await?;
println!(
"Uploaded: CID={}, Size={} bytes",
upload_result.cid, upload_result.size
);
// Download the file
let download_options = DownloadStreamOptions::new(&upload_result.cid)
.filepath(&download_path)
.on_progress(|progress| {
println!(
"Download progress: {} bytes ({}%)",
progress.bytes_downloaded,
(progress.percentage * 100.0) as u32
);
});
let download_result = download_stream(&node, &upload_result.cid, download_options).await?;
println!("Downloaded: Size={} bytes", download_result.size);
// Verify the downloaded content
let original_content = std::fs::read_to_string(&file_path)?;
let downloaded_content = std::fs::read_to_string(&download_path)?;
assert_eq!(
original_content, downloaded_content,
"Downloaded content should match original"
);
// Cleanup
node.stop().await?;
node.destroy().await?;
Ok(())
}