Skip to content

Commit 5ca4bd5

Browse files
Integration test of concurrent reads and writes
This covers the stream-through-cache case. Include a note on why we can't exercise "input error from stream" in an integration test.
1 parent afa8623 commit 5ca4bd5

File tree

3 files changed

+161
-6
lines changed

3 files changed

+161
-6
lines changed

test-fixtures/Cargo.lock

+112-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-fixtures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ http = "1.1.0"
1919
rustls-pemfile = "1.0.3"
2020
serde = "1.0.114"
2121
sha2 = "0.10.8"
22+
uuid = { version = "1.15.1", features = ["v4"] }

test-fixtures/src/bin/cache.rs

+48-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
use fastly::cache::core::*;
44
use std::io::Write;
55
use std::time::Duration;
6+
use uuid::Uuid;
7+
8+
fn main() {
9+
test_non_concurrent();
10+
test_concurrent();
11+
// We don't have a way of testing "incomplete streaming results in an error"
12+
// in a single instance. If we fail to close the (write) body handle, the underlying host object
13+
// is still hanging around, ready for more writes, until the instance is done.
14+
// Oh well -- that's what we have collecting_body::tests::unfinished_stream for.
15+
}
616

717
fn test_non_concurrent() {
8-
let key = CacheKey::from_static("hello".as_bytes());
18+
let key = new_key();
919

1020
{
1121
let fetch = lookup(key.clone())
@@ -14,7 +24,7 @@ fn test_non_concurrent() {
1424
assert!(fetch.is_none());
1525
}
1626

17-
let body = "world".as_bytes();
27+
let body = "hello world".as_bytes();
1828
{
1929
let mut writer = insert(key.clone(), Duration::from_secs(10))
2030
.known_length(body.len() as u64)
@@ -34,6 +44,40 @@ fn test_non_concurrent() {
3444
}
3545
}
3646

37-
fn main() {
38-
test_non_concurrent();
47+
fn test_concurrent() {
48+
let key = new_key();
49+
50+
{
51+
let fetch = lookup(key.clone())
52+
.execute()
53+
.expect("failed initial lookup");
54+
assert!(fetch.is_none());
55+
}
56+
57+
let mut writer = insert(key.clone(), Duration::from_secs(10))
58+
.execute()
59+
.unwrap();
60+
61+
let fetch: Found = lookup(key.clone()).execute().unwrap().unwrap();
62+
let mut body = fetch.to_stream().unwrap();
63+
let mut body = body.read_chunks(6);
64+
65+
write!(writer, "hello ").unwrap();
66+
writer.flush().unwrap();
67+
68+
// This appears to be the only read mechanism that won't block for more.
69+
let hello = body.next().unwrap().unwrap();
70+
assert_eq!(hello, b"hello ");
71+
72+
write!(writer, "world").unwrap();
73+
writer.finish().unwrap();
74+
75+
let cached = body.next().unwrap().unwrap();
76+
assert_eq!(cached, b"world");
77+
78+
assert!(body.next().is_none());
79+
}
80+
81+
fn new_key() -> CacheKey {
82+
Uuid::new_v4().into_bytes().to_vec().into()
3983
}

0 commit comments

Comments
 (0)