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

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

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

            
26
1
    let client = AsyncClient::build(Url::parse("bonsaidb://localhost:12345")?)
27
1
        .with_certificate(certificate)
28
1
        .build()?;
29

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

            
35
1
    futures::future::join_all(tasks)
36
5184
        .await
37
1
        .into_iter()
38
1
        .collect::<Result<Vec<()>, anyhow::Error>>()?;
39
1
    Ok(())
40
}
41

            
42
10
async fn test_one_client(client: AsyncClient, database_name: String) -> anyhow::Result<()> {
43
510
    for _ in 0u32..50 {
44
500
        client
45
500
            .create_database::<BasicSchema>(&database_name, false)
46
3590
            .await
47
500
            .unwrap();
48
500
        let db = client.database::<BasicSchema>(&database_name).await?;
49
500
        test_util::store_retrieve_update_delete_tests(&db)
50
44185
            .await
51
500
            .unwrap();
52
3398
        client.delete_database(&database_name).await.unwrap();
53
    }
54
10
    Ok(())
55
10
}