vaegram 0.3.0

Telegram MTProto client library for users and bots
Documentation
//! The session task over WebSocket.

use tokio::net::TcpListener;

use super::{Arc, Options, Runner, Salt, mpsc};
use crate::connection::{open_plain_websocket, ping_when_idle};
use crate::session::internals::data_center::Dc;

#[tokio::test]
async fn an_idle_session_answers_a_websocket_ping() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let dc = Dc { id: 2, addr: listener.local_addr().unwrap(), test: false, media: false };
    // Two frames before the session is idle: the obfuscation header, sent as its own frame when the connection
    // opens, and the Runner's start-up ping (its ping interval fires at once; the next one is 60 s away).
    let server = tokio::spawn(ping_when_idle(listener, 2));
    let options = Options::default();
    let conn = open_plain_websocket(&dc, &options).await;
    let (tx, _updates) = mpsc::channel(8);
    let runner = Runner::new(dc, Arc::new(options), conn, ([7; 256], Salt::default(), true), Arc::default(), Some(tx));
    let (_requests, requests) = mpsc::unbounded_channel();
    // Spawned as `Session` spawns it, which also holds the task to `Send`.
    let task = tokio::spawn(runner.run(requests));
    let (pong, _stream) = server.await.unwrap();
    task.abort();
    assert_eq!(pong.as_deref(), Some(&b"idle"[..]), "no pong within 1 s");
}