3
3
use fastly:: cache:: core:: * ;
4
4
use std:: io:: Write ;
5
5
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
+ }
6
16
7
17
fn test_non_concurrent ( ) {
8
- let key = CacheKey :: from_static ( "hello" . as_bytes ( ) ) ;
18
+ let key = new_key ( ) ;
9
19
10
20
{
11
21
let fetch = lookup ( key. clone ( ) )
@@ -14,7 +24,7 @@ fn test_non_concurrent() {
14
24
assert ! ( fetch. is_none( ) ) ;
15
25
}
16
26
17
- let body = "world" . as_bytes ( ) ;
27
+ let body = "hello world" . as_bytes ( ) ;
18
28
{
19
29
let mut writer = insert ( key. clone ( ) , Duration :: from_secs ( 10 ) )
20
30
. known_length ( body. len ( ) as u64 )
@@ -34,6 +44,40 @@ fn test_non_concurrent() {
34
44
}
35
45
}
36
46
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 ( )
39
83
}
0 commit comments