| 73 | |
| 74 | #[test] |
| 75 | fn context_with_thread_local() { |
| 76 | let rt = tokio::runtime::Builder::new_multi_thread() |
| 77 | .max_blocking_threads(2) |
| 78 | .enable_all() |
| 79 | .build() |
| 80 | .unwrap(); |
| 81 | |
| 82 | let mut handles = Vec::with_capacity(10); |
| 83 | |
| 84 | for i in 0..=10 { |
| 85 | handles.push(rt.spawn(async move { |
| 86 | if let Some(attachments) = RpcContext::get_attachments() { |
| 87 | let mut attachments = attachments.lock().unwrap(); |
| 88 | attachments.insert("key1".into(), Value::from(format!("data-{i}"))); |
| 89 | |
| 90 | assert!(attachments.len() > 0); |
| 91 | }; |
| 92 | |
| 93 | time::sleep(Duration::from_millis(1000)).await; |
| 94 | |
| 95 | if let Some(attachments) = RpcContext::get_attachments() { |
| 96 | let attachments = attachments.lock().unwrap(); |
| 97 | assert!(attachments.len() > 0); |
| 98 | }; |
| 99 | })); |
| 100 | } |
| 101 | |
| 102 | for handle in handles { |
| 103 | rt.block_on(handle).unwrap(); |
| 104 | } |
| 105 | } |
| 106 | } |