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

            
3
use bonsaidb::{
4
    client::{url::Url, Client},
5
    core::{
6
        connection::StorageConnection,
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
18
    )
21
18
    .await?;
22
8
    server.install_self_signed_certificate(false).await?;
23
1
    let certificate = server
24
1
        .certificate_chain()
25
1
        .await?
26
1
        .into_end_entity_certificate();
27
3
    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
        .await?;
33

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

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

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