1
1
//! Tests a single server with multiple simultaneous connections.
2

            
3
use bonsaidb::{
4
    client::{url::Url, Client},
5
    core::{
6
        connection::AsyncStorageConnection,
7
        test_util::{self, BasicSchema, TestDirectory},
8
    },
9
    local::config::Builder,
10
    server::{DefaultPermissions, Server, ServerConfiguration},
11
};
12

            
13
1
#[tokio::test]
14
1
async fn simultaneous_connections() -> anyhow::Result<()> {
15
1
    let dir = TestDirectory::new("simultaneous-connections.bonsaidb");
16
1
    let server = Server::open(
17
1
        ServerConfiguration::new(&dir)
18
1
            .default_permissions(DefaultPermissions::AllowAll)
19
1
            .with_schema::<BasicSchema>()?,
20
3
    )
21
3
    .await?;
22
10
    server.install_self_signed_certificate(false).await?;
23
1
    let certificate = server
24
2
        .certificate_chain()
25
2
        .await?
26
1
        .into_end_entity_certificate();
27
5
    tokio::spawn(async move { server.listen_on(12345).await });
28

            
29
1
    let client = Client::build(Url::parse("bonsaidb://localhost:12345")?)
30
1
        .with_certificate(certificate)
31
1
        .finish()?;
32

            
33
1
    let mut tasks = Vec::new();
34
11
    for i in 0usize..10 {
35
10
        tasks.push(test_one_client(client.clone(), format!("test{}", i)));
36
10
    }
37

            
38
2994
    futures::future::join_all(tasks)
39
2994
        .await
40
1
        .into_iter()
41
1
        .collect::<Result<Vec<()>, anyhow::Error>>()?;
42
1
    Ok(())
43
1
}
44

            
45
10
async fn test_one_client(client: Client, database_name: String) -> anyhow::Result<()> {
46
510
    for _ in 0u32..50 {
47
500
        client
48
2078
            .create_database::<BasicSchema>(&database_name, false)
49
2078
            .await
50
500
            .unwrap();
51
500
        let db = client.database::<BasicSchema>(&database_name).await?;
52
24409
        test_util::store_retrieve_update_delete_tests(&db)
53
24409
            .await
54
500
            .unwrap();
55
1845
        client.delete_database(&database_name).await.unwrap();
56
    }
57
10
    Ok(())
58
10
}