Repository: nautechsystems/nautilus_trader
File: crates/adapters/dydx/tests/websocket.rs (Rust)

Code before:
    client.disconnect().await.unwrap();
}

#[rstest]
#[tokio::test]
async fn test_multiple_clones_subscribe() {
    let (addr, state) = start_test_server().await.unwrap();
    let ws_url = format!("ws://{addr}/v4/ws");

    let mut client = DydxWebSocketClient::new_public(ws_url, Some(30));
    client.connect().await.unwrap();

    wait_until_async(|| async { client.is_connected() }, Duration::from_secs(5)).await;

    // Create multiple clones
    let clone1 = client.clone();
    let clone2 = client.clone();

    // Subscribe from different clones
    clone1.subscribe_markets().await.unwrap();
    clone2.subscribe_block_height().await.unwrap();

    let btc_id = InstrumentId::from("BTC-USD.DYDX");
    client.subscribe_trades(btc_id).await.unwrap();

    tokio::time::sleep(Duration::from_millis(300)).await;

    let subs = state.subscriptions.lock().await;
    assert!(
        subs.iter().any(|s| s.contains("v4_markets")),
        "Clone 1 subscription should work"
    );
    assert!(
        subs.iter().any(|s| s.contains("v4_block_height")),
        "Clone 2 subscription should work"
    );
    assert!(
        subs.iter().any(|s| s.contains("v4_trades")),
        "Original client subscription should work"
    );

    client.disconnect().await.unwrap();
}

#[rstest]
#[tokio::test]
async fn test_double_connect_is_noop() {
    let (addr, state) = start_test_server().await.unwrap();
    let ws_url = format!("ws://{addr}/v4/ws");

    let mut client = DydxWebSocketClient::new_public(ws_url, Some(30));

Review comment (suggestion):
Best to use `wait_until_async` in tests where possible rather than sleeps with arbitrary durations.

- More robust
- Tests run faster

Code after:
    client.disconnect().await.unwrap();
}

#[rstest]
#[tokio::test]
async fn test_multiple_clones_subscribe() {
    let (addr, state) = start_test_server().await.unwrap();
    let ws_url = format!("ws://{addr}/v4/ws");

    let mut client = DydxWebSocketClient::new_public(ws_url, Some(30));
    client.connect().await.unwrap();

    wait_until_async(|| async { client.is_connected() }, Duration::from_secs(5)).await;

    // Create multiple clones
    let clone1 = client.clone();
    let clone2 = client.clone();

    // Subscribe from different clones
    clone1.subscribe_markets().await.unwrap();
    clone2.subscribe_block_height().await.unwrap();

    let btc_id = InstrumentId::from("BTC-USD.DYDX");
    client.subscribe_trades(btc_id).await.unwrap();

    tokio::time::sleep(Duration::from_millis(1500)).await;

    let subs = state.subscriptions.lock().await;
    assert!(
        subs.iter().any(|s| s.contains("v4_markets")),
        "Clone 1 subscription should work"
    );
    assert!(
        subs.iter().any(|s| s.contains("v4_block_height")),
        "Clone 2 subscription should work"
    );
    assert!(
        subs.iter().any(|s| s.contains("v4_trades")),
        "Original client subscription should work"
    );

    client.disconnect().await.unwrap();
}

#[rstest]
#[tokio::test]
async fn test_double_connect_is_noop() {
    let (addr, state) = start_test_server().await.unwrap();
    let ws_url = format!("ws://{addr}/v4/ws");

    let mut client = DydxWebSocketClient::new_public(ws_url, Some(30));