framework_3_0_test.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. use std::future::Future;
  2. use std::time::Duration;
  3. use exchanges::response_base::ResponseData;
  4. use std::sync::Arc;
  5. use tokio::sync::Mutex;
  6. use tracing::info;
  7. use global::log_utils::init_log_with_info;
  8. struct Node {
  9. pub count: u128
  10. }
  11. #[tokio::test]
  12. async fn main() {
  13. init_log_with_info();
  14. let node = Arc::new(Mutex::new(Node { count: 0 }));
  15. generator(handle, node).await;
  16. }
  17. // 消息创造者
  18. async fn generator<F, Fut>(handle_function: F, node: Arc<Mutex<Node>>)
  19. where
  20. F: Fn(Arc<Mutex<Node>>, ResponseData) -> Fut,
  21. Fut: Future<Output = ()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
  22. {
  23. let mut data = ResponseData::new("aaa".to_string(),
  24. "code".to_string(),
  25. "msg".to_string(),
  26. "data".to_string());
  27. loop {
  28. let mut s_data = data.clone();
  29. s_data.time = chrono::Utc::now().timestamp_micros();
  30. handle_function(node.clone(), s_data).await;
  31. tokio::time::sleep(Duration::from_micros(100)).await;
  32. }
  33. }
  34. // 消息处理者
  35. async fn handle(node: Arc<Mutex<Node>>, data: ResponseData) {
  36. let mut node = node.lock().await;
  37. // 实现您的处理逝剧
  38. // 例如,增加计数:
  39. node.count += 1;
  40. let delay = chrono::Utc::now().timestamp_micros() - data.time;
  41. // 处理响应数据
  42. info!("Handled data: {}, count is: {}, delay is: {}.", data.data, node.count, delay);
  43. }