|
| 1 | +use super::common::*; |
| 2 | +use crate::pocketoption::types::Action; |
| 3 | +use rust_decimal_macros::dec; |
| 4 | +use std::sync::Arc; |
| 5 | +use tokio::time::{timeout, Duration}; |
| 6 | +use binary_options_tools_core_pre::reimports::Message; |
| 7 | +use uuid::Uuid; |
| 8 | + |
| 9 | +#[tokio::test] |
| 10 | +async fn test_concurrent_identical_trades_hammer() { |
| 11 | + let setup = create_test_setup().await; |
| 12 | + let asset = "EURUSD_otc".to_string(); |
| 13 | + let amount = dec!(10.0); |
| 14 | + let time = 60; |
| 15 | + |
| 16 | + // Place 5 identical trades |
| 17 | + let mut handles = Vec::new(); |
| 18 | + for _ in 0..5 { |
| 19 | + let h = setup.handle.clone(); |
| 20 | + let a = asset.clone(); |
| 21 | + handles.push(tokio::spawn(async move { |
| 22 | + h.trade(a, Action::Call, amount, time).await |
| 23 | + })); |
| 24 | + } |
| 25 | + |
| 26 | + // Wait for all 5 to be sent to WS |
| 27 | + let mut req_ids = Vec::new(); |
| 28 | + for _ in 0..5 { |
| 29 | + if let Ok(Message::Text(text)) = timeout(Duration::from_secs(1), setup.ws_rx.recv()).await.unwrap() { |
| 30 | + // 42["openOrder",{"amount":"10.0","asset":"EURUSD_otc","action":"call","isDemo":0,"requestId":"...","optionType":100,"time":60}] |
| 31 | + let start = text.find('{').unwrap(); |
| 32 | + let end = text.rfind('}').unwrap(); |
| 33 | + let json_str = &text[start..end+1]; |
| 34 | + let v: serde_json::Value = serde_json::from_str(json_str).unwrap(); |
| 35 | + let req_id = Uuid::parse_str(v["requestId"].as_str().unwrap()).unwrap(); |
| 36 | + req_ids.push(req_id); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + assert_eq!(req_ids.len(), 5); |
| 41 | + |
| 42 | + // Now simulate responses from server. |
| 43 | + // Server doesn't send requestId for failures, it uses asset/amount matching. |
| 44 | + // For successes, it DOES send requestId. |
| 45 | + |
| 46 | + // 1. Success for 2nd request |
| 47 | + let deal2 = create_test_deal(req_ids[1], &asset); |
| 48 | + let resp2 = format!(r#"42["successopenOrder",{}]"#, serde_json::to_string(&deal2).unwrap()); |
| 49 | + setup.msg_tx.send(Arc::new(Message::Text(resp2.into()))).await.unwrap(); |
| 50 | + |
| 51 | + // 2. Failure (will be matched to 1st request because it's the oldest in failure_matching queue) |
| 52 | + let fail_data = create_test_fail(&asset, amount); |
| 53 | + let resp_fail = format!(r#"42["failopenOrder",{}]"#, serde_json::to_string(&fail_data).unwrap()); |
| 54 | + setup.msg_tx.send(Arc::new(Message::Text(resp_fail.into()))).await.unwrap(); |
| 55 | + |
| 56 | + // 3. Success for 4th request |
| 57 | + let deal4 = create_test_deal(req_ids[3], &asset); |
| 58 | + let resp4 = format!(r#"42["successopenOrder",{}]"#, serde_json::to_string(&deal4).unwrap()); |
| 59 | + setup.msg_tx.send(Arc::new(Message::Text(resp4.into()))).await.unwrap(); |
| 60 | + |
| 61 | + // Now collect results from handles |
| 62 | + // handle 2 (req_ids[1]) should be Success |
| 63 | + // handle 1 (req_ids[0]) should be Fail |
| 64 | + // handle 4 (req_ids[3]) should be Success |
| 65 | + // handle 3 and 5 are still pending (we can ignore or fail them later) |
| 66 | + |
| 67 | + // Note: Since we spawned them, we don't easily know which JoinHandle corresponds to which req_id |
| 68 | + // unless we mapped them. But the TradesApiModule routes them based on req_id for success. |
| 69 | + // For failure, it routes to the OLDEST one in its internal queue. |
| 70 | + |
| 71 | + // Let's wait for the ones we expect to finish |
| 72 | + // Since they are in tokio::spawn, we just await them. |
| 73 | + // Actually, req_ids[1] was deal2, so handles[1] should return Ok(deal2). |
| 74 | + // req_ids[0] was matched to fail_data, so handles[0] should return Err(FailOpenOrder). |
| 75 | + // req_ids[3] was deal4, so handles[3] should return Ok(deal4). |
| 76 | + |
| 77 | + let res1 = handles.remove(0).await.unwrap(); |
| 78 | + let res2 = handles.remove(0).await.unwrap(); // handles[1] is now at index 0 |
| 79 | + let _res3 = handles.remove(0); // skip handles[2] |
| 80 | + let res4 = handles.remove(0).await.unwrap(); // handles[3] is now at index 0 |
| 81 | + |
| 82 | + assert!(res1.is_err()); |
| 83 | + assert!(res2.is_ok()); |
| 84 | + assert!(res4.is_ok()); |
| 85 | + |
| 86 | + if let Err(e) = res1 { |
| 87 | + assert!(format!("{:?}", e).contains("Insufficient balance")); |
| 88 | + } |
| 89 | +} |
0 commit comments