Skip to content

Commit 665a0cf

Browse files
committed
set_up_test_env
1 parent d45849c commit 665a0cf

File tree

6 files changed

+58
-49
lines changed

6 files changed

+58
-49
lines changed

tests/common/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![allow(dead_code)]
2+
use colink::{
3+
extensions::instant_server::{InstantRegistry, InstantServer},
4+
CoLink,
5+
};
6+
7+
pub async fn set_up_test_env(
8+
num: usize,
9+
) -> Result<
10+
(InstantRegistry, Vec<InstantServer>, Vec<CoLink>),
11+
Box<dyn std::error::Error + Send + Sync + 'static>,
12+
> {
13+
let ir = InstantRegistry::new();
14+
let mut iss = vec![];
15+
let mut cls = vec![];
16+
for _ in 0..num {
17+
let is = InstantServer::new();
18+
let cl = is.get_colink().switch_to_generated_user().await?;
19+
iss.push(is);
20+
cls.push(cl);
21+
}
22+
Ok((ir, iss, cls))
23+
}
24+
25+
pub async fn set_up_test_env_single_user() -> Result<
26+
(InstantRegistry, InstantServer, CoLink),
27+
Box<dyn std::error::Error + Send + Sync + 'static>,
28+
> {
29+
let ir = InstantRegistry::new();
30+
let is = InstantServer::new();
31+
let cl = is.get_colink().switch_to_generated_user().await?;
32+
Ok((ir, is, cl))
33+
}

tests/test_counter_with_lock.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use colink::extensions::instant_server::{InstantRegistry, InstantServer};
1+
mod common;
2+
use common::*;
23
use std::{
34
sync::{Arc, Mutex},
45
thread,
@@ -7,9 +8,7 @@ use std::{
78
#[tokio::test]
89
async fn test_counter_with_lock() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
910
{
10-
let _ir = InstantRegistry::new();
11-
let is = InstantServer::new();
12-
let cl = is.get_colink().switch_to_generated_user().await?;
11+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
1312

1413
cl.update_entry("example_lock_counter", &0_i32.to_le_bytes())
1514
.await

tests/test_policy_module.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use colink::extensions::instant_server::{InstantRegistry, InstantServer};
1+
mod common;
22
use colink::extensions::policy_module::{Action, Rule, TaskFilter};
3+
use common::*;
34

45
#[tokio::test]
56
async fn test_policy_module() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
6-
let _ir = InstantRegistry::new();
7-
let is = InstantServer::new();
8-
let cl = is.get_colink().switch_to_generated_user().await?;
7+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
98
//default policy
109
let res = cl.policy_module_get_rules().await?;
1110
assert!(res.len() == 1);

tests/test_protocol_variable_transfer.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![allow(unused_variables)]
2-
use colink::{
3-
extensions::instant_server::{InstantRegistry, InstantServer},
4-
CoLink, Participant, ProtocolEntry,
5-
};
2+
mod common;
3+
use colink::{CoLink, Participant, ProtocolEntry};
4+
use common::*;
65

76
struct Initiator;
87
#[colink::async_trait]
@@ -55,19 +54,13 @@ impl ProtocolEntry for Receiver {
5554

5655
#[tokio::test]
5756
async fn test_vt() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
58-
let ir = InstantRegistry::new();
59-
let mut iss = vec![];
60-
let mut cls = vec![];
61-
for i in 0..8 {
62-
let is = InstantServer::new();
63-
let cl = is.get_colink().switch_to_generated_user().await?;
57+
let (ir, iss, cls) = set_up_test_env(8).await?;
58+
for cl in &cls {
6459
colink::protocol_attach!(
6560
cl,
6661
("variable_transfer_test:initiator", Initiator),
6762
("variable_transfer_test:receiver", Receiver)
6863
);
69-
iss.push(is);
70-
cls.push(cl);
7164
}
7265
let mut participants = vec![Participant {
7366
user_id: cls[0].get_user_id()?,

tests/test_remote_storage.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use colink::{
2-
extensions::instant_server::{InstantRegistry, InstantServer},
3-
SubscriptionMessage,
4-
};
1+
mod common;
2+
use colink::SubscriptionMessage;
3+
use common::*;
54

65
#[tokio::test]
76
async fn test_remote_storage() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
8-
let _ir = InstantRegistry::new();
9-
let is_a = InstantServer::new();
10-
let is_b = InstantServer::new();
11-
let cl_a = is_a.get_colink().switch_to_generated_user().await?;
12-
let cl_b = is_b.get_colink().switch_to_generated_user().await?;
7+
let (_ir, _iss, mut cls) = set_up_test_env(2).await?;
8+
let (cl_a, cl_b) = (cls.pop().unwrap(), cls.pop().unwrap());
139
let msg = "hello";
1410
let user_id_a = cl_a.get_user_id().unwrap();
1511
let user_id_b = cl_b.get_user_id().unwrap();

tests/test_storage_macro.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use colink::{
2-
extensions::instant_server::{InstantRegistry, InstantServer},
3-
CoLink,
4-
};
1+
mod common;
2+
use colink::CoLink;
3+
use common::*;
54
use rand::Rng;
65

76
#[tokio::test]
87
async fn test_storage_macro_chunk() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
98
{
10-
let (_ir, _is, cl) = get_colink_user().await?;
9+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
1110

1211
let key_name = "storage_macro_test_chunk:$chunk";
1312
test_crud(&cl, key_name).await?;
@@ -18,7 +17,7 @@ async fn test_storage_macro_chunk() -> Result<(), Box<dyn std::error::Error + Se
1817
#[tokio::test]
1918
async fn test_storage_macro_redis() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
2019
{
21-
let (_ir, _is, cl) = get_colink_user().await?;
20+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
2221

2322
cl.create_entry("storage_macro_test_redis:redis_url", b"redis://localhost")
2423
.await?;
@@ -56,7 +55,7 @@ async fn test_crud(
5655
#[tokio::test]
5756
async fn test_storage_macro_append(
5857
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
59-
let (_ir, _is, cl) = get_colink_user().await?;
58+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
6059

6160
let key_name = "storage_macro_test_append";
6261
test_append(&cl, key_name, 10 as usize).await?;
@@ -89,7 +88,7 @@ async fn test_append(
8988
#[tokio::test]
9089
async fn test_storage_macro_redis_append(
9190
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
92-
let (_ir, _is, cl) = get_colink_user().await?;
91+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
9392

9493
cl.create_entry(
9594
"test_storage_macro_redis_append:redis_url",
@@ -105,7 +104,7 @@ async fn test_storage_macro_redis_append(
105104
#[tokio::test]
106105
async fn test_storage_macro_chunk_append(
107106
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
108-
let (_ir, _is, cl) = get_colink_user().await?;
107+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
109108

110109
let key_name = "test_storage_macro_chunk_append:$chunk";
111110
test_append(&cl, key_name, 5e6 as usize).await?;
@@ -119,7 +118,7 @@ async fn test_storage_macro_chunk_append(
119118
#[tokio::test]
120119
async fn test_storage_macro_redis_chunk(
121120
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
122-
let (_ir, _is, cl) = get_colink_user().await?;
121+
let (_ir, _is, cl) = set_up_test_env_single_user().await?;
123122

124123
cl.create_entry(
125124
"test_storage_macro_redis_chunk:redis_url",
@@ -131,13 +130,3 @@ async fn test_storage_macro_redis_chunk(
131130

132131
Ok(())
133132
}
134-
135-
async fn get_colink_user() -> Result<
136-
(InstantRegistry, InstantServer, CoLink),
137-
Box<dyn std::error::Error + Send + Sync + 'static>,
138-
> {
139-
let ir = InstantRegistry::new();
140-
let is = InstantServer::new();
141-
let cl = is.get_colink().switch_to_generated_user().await?;
142-
Ok((ir, is, cl))
143-
}

0 commit comments

Comments
 (0)