-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtests.rs
843 lines (817 loc) · 29.5 KB
/
tests.rs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use std::io::Cursor;
use bao_tree::ChunkRanges;
use bytes::BytesMut;
use iroh_io::AsyncSliceReaderExt;
use crate::{
store::{
bao_file::test_support::{
decode_response_into_batch, make_wire_data, random_test_data, simulate_remote, validate,
},
Map as _, MapEntry, MapEntryMut, MapMut, ReadableStore, Store as _, ValidateProgress,
},
util::{progress::AsyncChannelProgressSender, raw_outboard},
IROH_BLOCK_SIZE,
};
macro_rules! assert_matches {
($expression:expr, $pattern:pat) => {
match $expression {
$pattern => (),
_ => panic!("assertion failed: `(expr matches pattern)` \
expression: `{:?}`, pattern: `{}`", $expression, stringify!($pattern)),
}
};
($expression:expr, $pattern:pat, $($arg:tt)+) => {
match $expression {
$pattern => (),
_ => panic!("{}: expression: `{:?}`, pattern: `{}`", format_args!($($arg)+), $expression, stringify!($pattern)),
}
};
}
use super::*;
/// Helper to simulate a slow request.
pub fn to_stream(
data: &[u8],
mtu: usize,
delay: std::time::Duration,
) -> impl Stream<Item = io::Result<Bytes>> + 'static {
let parts = data
.chunks(mtu)
.map(Bytes::copy_from_slice)
.collect::<Vec<_>>();
futures_lite::stream::iter(parts)
.then(move |part| async move {
tokio::time::sleep(delay).await;
io::Result::Ok(part)
})
.boxed()
}
async fn create_test_db() -> (tempfile::TempDir, Store) {
let _ = tracing_subscriber::fmt::try_init();
let testdir = tempfile::tempdir().unwrap();
let db_path = testdir.path().join("db.redb");
let options = Options {
path: PathOptions::new(testdir.path()),
batch: Default::default(),
inline: Default::default(),
};
let db = Store::new(db_path, options).await.unwrap();
(testdir, db)
}
/// small file that does not have outboard at all
const SMALL_SIZE: u64 = 1024;
/// medium file that has inline outboard but file data
const MID_SIZE: u64 = 1024 * 32;
/// large file that has file outboard and file data
const LARGE_SIZE: u64 = 1024 * 1024 * 10;
#[tokio::test]
async fn get_cases() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (tempdir, db) = create_test_db().await;
{
let small = Bytes::from(random_test_data(SMALL_SIZE as usize));
let (_outboard, hash) = raw_outboard(&small);
let res = db.get(&hash).await.unwrap();
assert_matches!(res, None);
let tt = db
.import_bytes(small.clone(), BlobFormat::Raw)
.await
.unwrap();
let res = db.get(&hash).await.unwrap();
let entry = res.expect("entry not found");
let actual = entry.data_reader().read_to_end().await.unwrap();
assert_eq!(actual, small);
drop(tt);
}
{
let mid = Bytes::from(random_test_data(MID_SIZE as usize));
let (_outboard, hash) = raw_outboard(&mid);
let res = db.get(&hash).await.unwrap();
assert_matches!(res, None);
let tt = db.import_bytes(mid.clone(), BlobFormat::Raw).await.unwrap();
let res = db.get(&hash).await.unwrap();
let entry = res.expect("entry not found");
let actual = entry.data_reader().read_to_end().await.unwrap();
assert_eq!(actual, mid);
drop(tt);
}
{
let large = Bytes::from(random_test_data(LARGE_SIZE as usize));
let (_outboard, hash) = raw_outboard(&large);
let res = db.get(&hash).await.unwrap();
assert_matches!(res, None);
let tt = db
.import_bytes(large.clone(), BlobFormat::Raw)
.await
.unwrap();
let res = db.get(&hash).await.unwrap();
let entry = res.expect("entry not found");
let actual = entry.data_reader().read_to_end().await.unwrap();
assert_eq!(actual, large);
drop(tt);
}
{
let mid = random_test_data(MID_SIZE as usize);
let path = tempdir.path().join("mid.data");
std::fs::write(&path, &mid).unwrap();
let (_outboard, hash) = raw_outboard(&mid);
let res = db.get(&hash).await.unwrap();
assert_matches!(res, None);
let tt = db
.import_file(path, ImportMode::TryReference, BlobFormat::Raw, np())
.await
.unwrap();
let res = db.get(&hash).await.unwrap();
let entry = res.expect("entry not found");
let actual = entry.data_reader().read_to_end().await.unwrap();
assert_eq!(actual, mid);
drop(tt);
}
}
#[tokio::test]
async fn get_or_create_cases() {
let (_tempdir, db) = create_test_db().await;
{
const SIZE: u64 = SMALL_SIZE;
let data = random_test_data(SIZE as usize);
let (hash, reader) = simulate_remote(&data);
let entry = db.get_or_create(hash, 0).await.unwrap();
{
let state = db.entry_state(hash).await.unwrap();
assert_eq!(state.db, None);
assert_matches!(state.mem, Some(_));
}
let writer = entry.batch_writer().await.unwrap();
decode_response_into_batch(hash, IROH_BLOCK_SIZE, ChunkRanges::all(), reader, writer)
.await
.unwrap();
{
let state = db.entry_state(hash).await.unwrap();
assert_matches!(state.mem, Some(_));
assert_matches!(state.db, None);
}
db.insert_complete(entry.clone()).await.unwrap();
{
let state = db.entry_state(hash).await.unwrap();
assert_matches!(state.mem, Some(_));
assert_matches!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Inline(_),
..
})
);
}
drop(entry);
// sync so we know the msg sent on drop is processed
db.sync().await.unwrap();
let state = db.entry_state(hash).await.unwrap();
assert_matches!(state.mem, None);
}
{
const SIZE: u64 = MID_SIZE;
let data = random_test_data(SIZE as usize);
let (hash, reader) = simulate_remote(&data);
let entry = db.get_or_create(hash, 0).await.unwrap();
{
let state = db.entry_state(hash).await.unwrap();
assert_eq!(state.db, None);
assert_matches!(state.mem, Some(_));
}
let writer = entry.batch_writer().await.unwrap();
decode_response_into_batch(hash, IROH_BLOCK_SIZE, ChunkRanges::all(), reader, writer)
.await
.unwrap();
{
let state = db.entry_state(hash).await.unwrap();
assert_matches!(state.mem, Some(_));
assert_matches!(state.db, Some(EntryState::Partial { .. }));
}
db.insert_complete(entry.clone()).await.unwrap();
{
let state = db.entry_state(hash).await.unwrap();
assert_matches!(state.mem, Some(_));
assert_matches!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Owned(SIZE),
..
})
);
}
drop(entry);
// // sync so we know the msg sent on drop is processed
// db.sync().await.unwrap();
// let state = db.entry_state(hash).await.unwrap();
// assert_matches!(state.mem, None);
}
}
/// Import mem cases, small (data inline, outboard none), mid (data file, outboard inline), large (data file, outboard file)
#[tokio::test]
async fn import_mem_cases() {
let (_tempdir, db) = create_test_db().await;
{
const SIZE: u64 = SMALL_SIZE;
let small = random_test_data(SIZE as usize);
let (outboard, hash) = raw_outboard(&small);
let tt = db
.import_bytes(small.clone().into(), BlobFormat::Raw)
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Inline(small),
outboard_location: OutboardLocation::NotNeeded,
};
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert!(outboard.is_empty());
}
{
const SIZE: u64 = MID_SIZE;
let mid = Bytes::from(random_test_data(SIZE as usize));
let (outboard, hash) = raw_outboard(&mid);
let tt = db.import_bytes(mid.clone(), BlobFormat::Raw).await.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Owned(SIZE),
outboard_location: OutboardLocation::Inline(outboard),
};
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert_eq!(mid, std::fs::read(db.owned_data_path(&hash)).unwrap());
}
{
const SIZE: u64 = LARGE_SIZE;
let large = Bytes::from(random_test_data(SIZE as usize));
let (outboard, hash) = raw_outboard(&large);
let tt = db
.import_bytes(large.clone(), BlobFormat::Raw)
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Owned(SIZE),
outboard_location: OutboardLocation::Owned,
};
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert_eq!(large, std::fs::read(db.owned_data_path(&hash)).unwrap());
assert_eq!(
outboard,
tokio::fs::read(db.owned_outboard_path(&hash))
.await
.unwrap()
);
}
}
/// Import mem cases, small (data inline, outboard none), mid (data file, outboard inline), large (data file, outboard file)
#[tokio::test]
async fn import_stream_cases() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (_tempdir, db) = create_test_db().await;
{
const SIZE: u64 = SMALL_SIZE;
let small = random_test_data(SIZE as usize);
let (outboard, hash) = raw_outboard(&small);
let (tt, size) = db
.import_stream(
to_stream(&small, 100, Duration::from_millis(1)),
BlobFormat::Raw,
np(),
)
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Inline(small),
outboard_location: OutboardLocation::NotNeeded,
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert!(outboard.is_empty());
}
{
const SIZE: u64 = MID_SIZE;
let mid = Bytes::from(random_test_data(SIZE as usize));
let (outboard, hash) = raw_outboard(&mid);
let (tt, size) = db
.import_stream(
to_stream(&mid, 1000, Duration::from_millis(1)),
BlobFormat::Raw,
np(),
)
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Owned(SIZE),
outboard_location: OutboardLocation::Inline(outboard),
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert_eq!(mid, std::fs::read(db.owned_data_path(&hash)).unwrap());
}
{
const SIZE: u64 = LARGE_SIZE;
let large = Bytes::from(random_test_data(SIZE as usize));
let (outboard, hash) = raw_outboard(&large);
let (tt, size) = db
.import_stream(
to_stream(&large, 100000, Duration::from_millis(1)),
BlobFormat::Raw,
np(),
)
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Owned(SIZE),
outboard_location: OutboardLocation::Owned,
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert_eq!(large, std::fs::read(db.owned_data_path(&hash)).unwrap());
assert_eq!(
outboard,
tokio::fs::read(db.owned_outboard_path(&hash))
.await
.unwrap()
);
}
}
/// Import file cases, small (data inline, outboard none), mid (data file, outboard inline), large (data file, outboard file)
#[tokio::test]
async fn import_file_cases() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (tempdir, db) = create_test_db().await;
{
const SIZE: u64 = SMALL_SIZE;
let small = random_test_data(SIZE as usize);
let path = tempdir.path().join("small.data");
std::fs::write(&path, &small).unwrap();
let (outboard, hash) = raw_outboard(&small);
let (tt, size) = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Inline(small),
outboard_location: OutboardLocation::NotNeeded,
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert!(outboard.is_empty());
}
{
const SIZE: u64 = MID_SIZE;
let mid = Bytes::from(random_test_data(SIZE as usize));
let path = tempdir.path().join("mid.data");
std::fs::write(&path, &mid).unwrap();
let (outboard, hash) = raw_outboard(&mid);
let (tt, size) = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Owned(SIZE),
outboard_location: OutboardLocation::Inline(outboard),
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert_eq!(mid, std::fs::read(db.owned_data_path(&hash)).unwrap());
}
{
const SIZE: u64 = LARGE_SIZE;
let large = Bytes::from(random_test_data(SIZE as usize));
let path = tempdir.path().join("mid.data");
std::fs::write(&path, &large).unwrap();
let (outboard, hash) = raw_outboard(&large);
let (tt, size) = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Owned(SIZE),
outboard_location: OutboardLocation::Owned,
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert_eq!(large, std::fs::read(db.owned_data_path(&hash)).unwrap());
assert_eq!(
outboard,
tokio::fs::read(db.owned_outboard_path(&hash))
.await
.unwrap()
);
}
}
#[tokio::test]
async fn import_file_reference_cases() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (tempdir, db) = create_test_db().await;
{
const SIZE: u64 = SMALL_SIZE;
let small = random_test_data(SIZE as usize);
let path = tempdir.path().join("small.data");
std::fs::write(&path, &small).unwrap();
let (outboard, hash) = raw_outboard(&small);
let (tt, size) = db
.import_file(path, ImportMode::TryReference, BlobFormat::Raw, np())
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::Inline(small),
outboard_location: OutboardLocation::NotNeeded,
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert!(outboard.is_empty());
}
{
const SIZE: u64 = MID_SIZE;
let mid = random_test_data(SIZE as usize);
let path = tempdir.path().join("mid.data");
std::fs::write(&path, &mid).unwrap();
let (outboard, hash) = raw_outboard(&mid);
let (tt, size) = db
.import_file(
path.clone(),
ImportMode::TryReference,
BlobFormat::Raw,
np(),
)
.await
.unwrap();
let actual = db.entry_state(*tt.hash()).await.unwrap();
let expected = EntryState::Complete {
data_location: DataLocation::External(vec![path.clone()], SIZE),
outboard_location: OutboardLocation::Inline(outboard),
};
assert_eq!(size, SIZE);
assert_eq!(tt.hash(), &hash);
assert_eq!(actual.db, Some(expected));
assert_eq!(mid, std::fs::read(path).unwrap());
assert!(!db.owned_data_path(&hash).exists());
}
}
#[tokio::test]
async fn import_file_error_cases() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (tempdir, db) = create_test_db().await;
// relative path is not allowed
{
let path = PathBuf::from("relativepath.data");
let cause = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap_err();
assert_eq!(cause.kind(), io::ErrorKind::InvalidInput);
}
// file does not exist
{
let path = tempdir.path().join("pathdoesnotexist.data");
let cause = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap_err();
assert_eq!(cause.kind(), io::ErrorKind::InvalidInput);
}
// file is a directory
{
let path = tempdir.path().join("pathisdir.data");
std::fs::create_dir_all(&path).unwrap();
let cause = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap_err();
assert_eq!(cause.kind(), io::ErrorKind::InvalidInput);
}
}
#[tokio::test]
async fn import_file_tempdir_is_file() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (tempdir, db) = create_test_db().await;
// temp dir is readonly, this is a bit mean since we mess with the internals of the store
{
let temp_dir = db.0.temp_file_name().parent().unwrap().to_owned();
std::fs::remove_dir_all(&temp_dir).unwrap();
std::fs::write(temp_dir, []).unwrap();
// std::fs::set_permissions(temp_dir, std::os::unix::fs::PermissionsExt::from_mode(0o0))
// .unwrap();
let path = tempdir.path().join("mid.data");
let data = random_test_data(MID_SIZE as usize);
std::fs::write(&path, &data).unwrap();
let _cause = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap_err();
// cause is NotADirectory, but unstable
// assert_eq!(cause.kind(), io::ErrorKind::NotADirectory);
}
drop(tempdir);
}
#[tokio::test]
async fn import_file_datadir_is_file() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (tempdir, db) = create_test_db().await;
// temp dir is readonly, this is a bit mean since we mess with the internals of the store
{
let data_dir = db.0.path_options.data_path.to_owned();
std::fs::remove_dir_all(&data_dir).unwrap();
std::fs::write(data_dir, []).unwrap();
let path = tempdir.path().join("mid.data");
let data = random_test_data(MID_SIZE as usize);
std::fs::write(&path, &data).unwrap();
let _cause = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap_err();
// cause is NotADirectory, but unstable
// assert_eq!(cause.kind(), io::ErrorKind::NotADirectory);
}
drop(tempdir);
}
/// tests that owned wins over external in both cases
#[tokio::test]
async fn import_file_overwrite() {
let np = IgnoreProgressSender::<ImportProgress>::default;
let (tempdir, db) = create_test_db().await;
// overwrite external with owned
{
let path = tempdir.path().join("mid.data");
let data = random_test_data(MID_SIZE as usize);
let (_outboard, hash) = raw_outboard(&data);
std::fs::write(&path, &data).unwrap();
let (tt1, size1) = db
.import_file(path.clone(), ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap();
assert_eq!(size1, MID_SIZE);
assert_eq!(tt1.hash(), &hash);
let state = db.entry_state(hash).await.unwrap();
assert_matches!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Owned(_),
..
})
);
let (tt2, size2) = db
.import_file(path, ImportMode::TryReference, BlobFormat::Raw, np())
.await
.unwrap();
assert_eq!(size2, MID_SIZE);
assert_eq!(tt2.hash(), &hash);
let state = db.entry_state(hash).await.unwrap();
assert_matches!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Owned(_),
..
})
);
}
{
let path = tempdir.path().join("mid2.data");
let data = random_test_data(MID_SIZE as usize);
let (_outboard, hash) = raw_outboard(&data);
std::fs::write(&path, &data).unwrap();
let (tt1, size1) = db
.import_file(
path.clone(),
ImportMode::TryReference,
BlobFormat::Raw,
np(),
)
.await
.unwrap();
let state = db.entry_state(hash).await.unwrap();
assert_eq!(size1, MID_SIZE);
assert_eq!(tt1.hash(), &hash);
assert_matches!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::External(_, _),
..
})
);
let (tt2, size2) = db
.import_file(path, ImportMode::Copy, BlobFormat::Raw, np())
.await
.unwrap();
let state = db.entry_state(hash).await.unwrap();
assert_eq!(size2, MID_SIZE);
assert_eq!(tt2.hash(), &hash);
assert_matches!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Owned(_),
..
})
);
}
}
/// tests that export works in copy mode
#[tokio::test]
async fn export_copy_cases() {
let np = || Box::new(|_: u64| io::Result::Ok(()));
let (tempdir, db) = create_test_db().await;
let small = random_test_data(SMALL_SIZE as usize);
let mid = random_test_data(MID_SIZE as usize);
let large = random_test_data(LARGE_SIZE as usize);
let small_tt = db
.import_bytes(small.clone().into(), BlobFormat::Raw)
.await
.unwrap();
let mid_tt = db
.import_bytes(mid.clone().into(), BlobFormat::Raw)
.await
.unwrap();
let large_tt = db
.import_bytes(large.clone().into(), BlobFormat::Raw)
.await
.unwrap();
let small_path = tempdir.path().join("small.data");
let mid_path = tempdir.path().join("mid.data");
let large_path = tempdir.path().join("large.data");
db.export(*small_tt.hash(), small_path.clone(), ExportMode::Copy, np())
.await
.unwrap();
assert_eq!(small.to_vec(), std::fs::read(&small_path).unwrap());
db.export(*mid_tt.hash(), mid_path.clone(), ExportMode::Copy, np())
.await
.unwrap();
assert_eq!(mid.to_vec(), std::fs::read(&mid_path).unwrap());
db.export(*large_tt.hash(), large_path.clone(), ExportMode::Copy, np())
.await
.unwrap();
assert_eq!(large.to_vec(), std::fs::read(&large_path).unwrap());
let state = db.entry_state(*small_tt.hash()).await.unwrap();
assert_eq!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Inline(small),
outboard_location: OutboardLocation::NotNeeded,
})
);
let state = db.entry_state(*mid_tt.hash()).await.unwrap();
assert_eq!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Owned(MID_SIZE),
outboard_location: OutboardLocation::Inline(raw_outboard(&mid).0),
})
);
let state = db.entry_state(*large_tt.hash()).await.unwrap();
assert_eq!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Owned(LARGE_SIZE),
outboard_location: OutboardLocation::Owned,
})
);
}
/// tests that export works in reference mode
#[tokio::test]
async fn export_reference_cases() {
let np = || Box::new(|_: u64| io::Result::Ok(()));
let (tempdir, db) = create_test_db().await;
let small = random_test_data(SMALL_SIZE as usize);
let mid = random_test_data(MID_SIZE as usize);
let large = random_test_data(LARGE_SIZE as usize);
let small_tt = db
.import_bytes(small.clone().into(), BlobFormat::Raw)
.await
.unwrap();
let mid_tt = db
.import_bytes(mid.clone().into(), BlobFormat::Raw)
.await
.unwrap();
let large_tt = db
.import_bytes(large.clone().into(), BlobFormat::Raw)
.await
.unwrap();
let small_path = tempdir.path().join("small.data");
let mid_path = tempdir.path().join("mid.data");
let large_path = tempdir.path().join("large.data");
db.export(
*small_tt.hash(),
small_path.clone(),
ExportMode::TryReference,
np(),
)
.await
.unwrap();
assert_eq!(small.to_vec(), std::fs::read(&small_path).unwrap());
db.export(
*mid_tt.hash(),
mid_path.clone(),
ExportMode::TryReference,
np(),
)
.await
.unwrap();
assert_eq!(mid.to_vec(), std::fs::read(&mid_path).unwrap());
db.export(
*large_tt.hash(),
large_path.clone(),
ExportMode::TryReference,
np(),
)
.await
.unwrap();
assert_eq!(large.to_vec(), std::fs::read(&large_path).unwrap());
let state = db.entry_state(*small_tt.hash()).await.unwrap();
// small entries will never use external references
assert_eq!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::Inline(small),
outboard_location: OutboardLocation::NotNeeded,
})
);
// mid entries should now use external references
let state = db.entry_state(*mid_tt.hash()).await.unwrap();
assert_eq!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::External(vec![mid_path], MID_SIZE),
outboard_location: OutboardLocation::Inline(raw_outboard(&mid).0),
})
);
// large entries should now use external references
let state = db.entry_state(*large_tt.hash()).await.unwrap();
assert_eq!(
state.db,
Some(EntryState::Complete {
data_location: DataLocation::External(vec![large_path], LARGE_SIZE),
outboard_location: OutboardLocation::Owned,
})
);
}
#[tokio::test]
async fn actor_store_smoke() {
let testdir = tempfile::tempdir().unwrap();
let db_path = testdir.path().join("test.redb");
let options = Options {
path: PathOptions::new(testdir.path()),
batch: Default::default(),
inline: Default::default(),
};
let db = Store::new(db_path, options).await.unwrap();
db.dump().await.unwrap();
let data = random_test_data(1024 * 1024);
#[allow(clippy::single_range_in_vec_init)]
let ranges = [0..data.len() as u64];
let (hash, chunk_ranges, wire_data) = make_wire_data(&data, &ranges);
let handle = db.get_or_create(hash, 0).await.unwrap();
decode_response_into_batch(
hash,
IROH_BLOCK_SIZE,
chunk_ranges.clone(),
Cursor::new(wire_data.as_slice()),
handle.batch_writer().await.unwrap(),
)
.await
.unwrap();
validate(&handle, &data, &ranges).await;
db.insert_complete(handle).await.unwrap();
db.sync().await.unwrap();
db.dump().await.unwrap();
}
#[tokio::test]
async fn verifiable_stream_smoke() -> testresult::TestResult {
let db1 = crate::store::mem::Store::new();
let db2 = crate::store::mem::Store::new();
const SIZE: usize = 16 * 1024 * 1024;
let data = random_test_data(SIZE);
let tag = db1.import_bytes(Bytes::from(data), BlobFormat::Raw).await?;
let mut buffer = BytesMut::with_capacity(SIZE + 1024 * 1024);
let entry = db1.get(tag.hash()).await?.expect("We just wrote this hash");
entry.write_verifiable_stream(0, &mut buffer).await?;
db2.import_verifiable_stream(*tag.hash(), SIZE as u64, 0, buffer.freeze())
.await?;
let (tx, rx) = async_channel::bounded(128);
let handle = tokio::spawn(async move {
while let Ok(progress) = rx.recv().await {
if let ValidateProgress::Abort(err) = progress {
panic!("Got an error: {err}");
}
}
});
db2.validate(false, AsyncChannelProgressSender::new(tx).boxed())
.await?;
handle.await?;
Ok(())
}