-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Populate nwritten_out on error in config-store and dictionary get
- Loading branch information
Showing
5 changed files
with
105 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,59 @@ | ||
//! A guest program to test that dictionary lookups work properly. | ||
//! A guest program to test that config-store lookups work properly. | ||
use fastly::ConfigStore; | ||
use fastly_shared::FastlyStatus; | ||
|
||
fn main() { | ||
let animals = ConfigStore::open("animals"); | ||
assert_eq!(animals.get("dog").unwrap(), "woof"); | ||
assert_eq!(animals.get("cat").unwrap(), "meow"); | ||
assert_eq!(animals.get("lamp"), None); | ||
let animals = unsafe { | ||
let mut dict_handle = fastly_shared::INVALID_CONFIG_STORE_HANDLE; | ||
let res = fastly_sys::fastly_config_store::open( | ||
"animals".as_ptr(), | ||
"animals".len(), | ||
&mut dict_handle as *mut _, | ||
); | ||
assert_eq!(res, FastlyStatus::OK, "Failed to open config-store"); | ||
dict_handle | ||
}; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
enum LookupResult { | ||
Success(String), | ||
Missing, | ||
BufTooSmall(usize), | ||
Err(FastlyStatus), | ||
} | ||
|
||
let get = |key: &str, buf_len: usize| unsafe { | ||
let mut value = Vec::with_capacity(buf_len); | ||
let mut nwritten = 0; | ||
let res = fastly_sys::fastly_config_store::get( | ||
animals, | ||
key.as_ptr(), | ||
key.len(), | ||
value.as_mut_ptr(), | ||
buf_len, | ||
&mut nwritten as *mut _, | ||
); | ||
|
||
if res != FastlyStatus::OK { | ||
if res == FastlyStatus::NONE { | ||
return LookupResult::Missing; | ||
} | ||
|
||
if res == FastlyStatus::BUFLEN { | ||
assert!(nwritten > 0 && buf_len < nwritten); | ||
return LookupResult::BufTooSmall(nwritten); | ||
} | ||
|
||
return LookupResult::Err(res); | ||
} | ||
|
||
value.set_len(nwritten); | ||
value.shrink_to(nwritten); | ||
LookupResult::Success(String::from_utf8(value).unwrap()) | ||
}; | ||
|
||
assert_eq!(get("dog", 4), LookupResult::Success(String::from("woof"))); | ||
assert_eq!(get("cat", 4), LookupResult::Success(String::from("meow"))); | ||
assert_eq!(get("cat", 2), LookupResult::BufTooSmall(4)); | ||
assert_eq!(get("lamp", 4), LookupResult::Missing); | ||
} |