|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#![deny(warnings, unconditional_panic)] |
| 5 | +#![deny(nonstandard_style)] |
| 6 | +#![deny(clippy::all)] |
| 7 | + |
| 8 | +pub struct BoxError(String); |
| 9 | +pub struct BoxError2(String); |
| 10 | + |
| 11 | +impl std::fmt::Debug for BoxError2 { |
| 12 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 13 | + write!(f, "{}", self.0) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +impl From<BoxError> for BoxError2 { |
| 18 | + fn from(error: BoxError) -> Self { |
| 19 | + BoxError2(error.0) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl<T: std::fmt::Debug> From<T> for BoxError { |
| 24 | + fn from(error: T) -> Self { |
| 25 | + let my_str = format!("{:?}", error); |
| 26 | + BoxError(my_str) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +pub mod local_cmc_tests { |
| 31 | + use aws_mpl_rs::types::material_providers_config::MaterialProvidersConfig; |
| 32 | + use aws_mpl_rs::client as mpl_client; |
| 33 | + use aws_mpl_rs::types::CacheType; |
| 34 | + use aws_mpl_rs::types::DefaultCache; |
| 35 | + use aws_mpl_rs::types::cryptographic_materials_cache::CryptographicMaterialsCacheRef; |
| 36 | + use aws_mpl_rs::operation::get_cache_entry::GetCacheEntryOutput; |
| 37 | + use aws_mpl_rs::types::error::Error; |
| 38 | + use aws_mpl_rs::types::Materials; |
| 39 | + use aws_mpl_rs::deps::aws_cryptography_keyStore::types::BeaconKeyMaterials; |
| 40 | + use std::collections::HashMap; |
| 41 | + use chrono::Utc; |
| 42 | + use rand::{thread_rng, Rng}; |
| 43 | + use futures::stream::StreamExt; |
| 44 | + // Currently this is commented out to keep it consistent with other |
| 45 | + // runtimes but every language should eventually have a network |
| 46 | + // call delay while testing adding to the StormTrackingCache. |
| 47 | + // use std::time::Duration; |
| 48 | + |
| 49 | + async fn work_for_thread() -> Result<(), crate::BoxError> { |
| 50 | + let mpl_config: MaterialProvidersConfig = MaterialProvidersConfig::builder().build()?; |
| 51 | + let mpl: mpl_client::Client = mpl_client::Client::from_conf(mpl_config)?; |
| 52 | + |
| 53 | + let test: CryptographicMaterialsCacheRef = mpl.create_cryptographic_materials_cache() |
| 54 | + .cache(CacheType::Default( |
| 55 | + DefaultCache::builder() |
| 56 | + .entry_capacity(10) |
| 57 | + .build()?)) |
| 58 | + .send() |
| 59 | + .await?; |
| 60 | + |
| 61 | + let identifiers: Vec<&str> = vec![ |
| 62 | + "one", |
| 63 | + "two", |
| 64 | + "three", |
| 65 | + "four", |
| 66 | + "five", |
| 67 | + "six", |
| 68 | + "seven", |
| 69 | + "eight", |
| 70 | + "nine", |
| 71 | + "ten", |
| 72 | + "eleven", |
| 73 | + "twelve", |
| 74 | + "thirteen", |
| 75 | + "fourteen", |
| 76 | + "fifteen", |
| 77 | + "sixteen", |
| 78 | + "seventeen", |
| 79 | + "eighteen", |
| 80 | + "nineteen", |
| 81 | + "twenty", |
| 82 | + "twenty one" |
| 83 | + ]; |
| 84 | + |
| 85 | + let id_size: usize = identifiers.len(); |
| 86 | + |
| 87 | + let invocations = 300000; |
| 88 | + let concurrent_tasks = 10; |
| 89 | + |
| 90 | + futures::stream::iter(0..invocations) |
| 91 | + .map(|_i| { |
| 92 | + let identifiers = identifiers.clone(); |
| 93 | + let test = test.clone(); |
| 94 | + |
| 95 | + async move { |
| 96 | + println!("-----------TestLotsofAdd-----------"); |
| 97 | + let random = thread_rng().gen_range(0..id_size); |
| 98 | + let beacon_key_identifier: &str = identifiers[random]; |
| 99 | + |
| 100 | + let cache_identifier: aws_smithy_types::Blob = aws_smithy_types::Blob::new(beacon_key_identifier.as_bytes()); |
| 101 | + |
| 102 | + let cache_entry_output: Result<GetCacheEntryOutput, Error> = test.get_cache_entry() |
| 103 | + .identifier(cache_identifier.clone()) |
| 104 | + .send() |
| 105 | + .await; |
| 106 | + |
| 107 | + match cache_entry_output { |
| 108 | + Ok(_) => { |
| 109 | + println!("Cache hit for beacon key identifier: {}", beacon_key_identifier); |
| 110 | + } |
| 111 | + Err(Error::EntryDoesNotExist {message: _m}) => { |
| 112 | + let materials = Materials::BeaconKey( |
| 113 | + BeaconKeyMaterials::builder() |
| 114 | + .beacon_key_identifier(beacon_key_identifier.to_string()) |
| 115 | + // The cacheIdentifier is used as the material |
| 116 | + // because we are not testing the cryptography here. |
| 117 | + .beacon_key(cache_identifier.clone()) |
| 118 | + .encryption_context(HashMap::new()) |
| 119 | + .build()? |
| 120 | + ); |
| 121 | + // This sleep time is to mimic a KMS or DDB call |
| 122 | + // Currently this is commented out to keep it consistent with other |
| 123 | + // runtimes but every language should eventually have a network |
| 124 | + // call delay while testing adding to the StormTrackingCache. |
| 125 | + // The reason why this is commented out and not just set to 0 is because |
| 126 | + // setting it to 0 adds a significant overhead of task switching, and |
| 127 | + // with that, testing for 300_000 instances will not be possible. |
| 128 | + // let network_call_delay: u64 = 50; |
| 129 | + // tokio::time::sleep(Duration::from_millis(network_call_delay)).await; |
| 130 | + |
| 131 | + test.put_cache_entry() |
| 132 | + .identifier(cache_identifier) |
| 133 | + .creation_time(Utc::now().timestamp()) |
| 134 | + .expiry_time(Utc::now().timestamp() + 1) |
| 135 | + .materials(materials) |
| 136 | + .send() |
| 137 | + .await?; |
| 138 | + |
| 139 | + println!("Cache miss for beacon key identifier: {}", beacon_key_identifier); |
| 140 | + } |
| 141 | + Err(e) => { |
| 142 | + panic!("Unexpected error while accessing cache: {}", e); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + Ok::<(), crate::BoxError>(()) |
| 148 | + } |
| 149 | + }) |
| 150 | + .buffer_unordered(concurrent_tasks) |
| 151 | + .collect::<Vec<_>>() |
| 152 | + .await; |
| 153 | + |
| 154 | + Ok(()) |
| 155 | + } |
| 156 | + |
| 157 | + #[tokio::test(flavor = "multi_thread")] |
| 158 | + async fn test_a_lot_of_adding() -> Result<(), crate::BoxError2> { |
| 159 | + work_for_thread().await?; |
| 160 | + Ok(()) |
| 161 | + } |
| 162 | +} |
0 commit comments