Skip to content

Commit 54623cb

Browse files
authored
Merge pull request #40 from CoLearn-Dev/is-issues
- instant server: error redirect - instant server: check mq connection before starting - remove async in InstantRegistry::new()
2 parents 4c65290 + ba28504 commit 54623cb

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "colink"
3-
version = "0.2.6"
3+
version = "0.2.7"
44
edition = "2021"
55
description = "CoLink Rust SDK"
66
license = "MIT"
@@ -23,6 +23,7 @@ lapin = "2.1"
2323
prost = "0.10"
2424
rand = { version = "0.8.4", features = ["std_rng"] }
2525
rcgen = { version = "0.10", optional = true }
26+
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls-native-roots"], optional = true }
2627
secp256k1 = { version = "0.25", features = ["rand-std"] }
2728
serde = { version = "1.0", features = ["derive"] }
2829
serde_json = "1.0"
@@ -45,5 +46,5 @@ remote_storage = ["extensions"]
4546
variable_transfer = ["extensions", "remote_storage", "hyper", "jsonwebtoken", "rcgen", "tokio-rustls", "hyper-rustls"]
4647
registry = []
4748
policy_module = []
48-
instant_server = []
49+
instant_server = ["reqwest"]
4950
storage_macro = ["async-recursion"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ CoLink SDK helps both application adnd protocol developers access the functional
99
Add this to your Cargo.toml:
1010
```toml
1111
[dependencies]
12-
colink = "0.2.5"
12+
colink = "0.2.7"
1313
```
1414

1515
## Getting Started

examples/protocol_greetings_with_instant_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl ProtocolEntry for Receiver {
3636

3737
#[tokio::main]
3838
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
39-
let ir = InstantRegistry::new().await;
39+
let ir = InstantRegistry::new();
4040
// The instant server will automatically use the instant registry when there is one.
4141
let is0 = InstantServer::new();
4242
let is1 = InstantServer::new();

src/extensions/instant_server.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ impl InstantServer {
7070
} else {
7171
"http://guest:guest@localhost:15672/api".to_string()
7272
};
73+
let (mq_amqp, mq_api) = std::thread::spawn(move || {
74+
tokio::runtime::Builder::new_multi_thread()
75+
.enable_all()
76+
.build()
77+
.unwrap()
78+
.block_on(async {
79+
let res = reqwest::get(&mq_api).await.unwrap();
80+
assert!(res.status() == hyper::StatusCode::OK);
81+
lapin::Connection::connect(&mq_amqp, lapin::ConnectionProperties::default())
82+
.await
83+
.unwrap();
84+
});
85+
(mq_amqp, mq_api)
86+
})
87+
.join()
88+
.unwrap();
7389
let child = Command::new(program)
7490
.args([
7591
"--address",
@@ -88,8 +104,6 @@ impl InstantServer {
88104
])
89105
.env("COLINK_HOME", colink_home)
90106
.current_dir(working_dir.clone())
91-
.stdout(Stdio::null())
92-
.stderr(Stdio::null())
93107
.spawn()
94108
.unwrap();
95109
loop {
@@ -129,17 +143,34 @@ impl Drop for InstantRegistry {
129143
}
130144
}
131145

146+
impl Default for InstantRegistry {
147+
fn default() -> Self {
148+
Self::new()
149+
}
150+
}
151+
132152
impl InstantRegistry {
133-
pub async fn new() -> Self {
153+
pub fn new() -> Self {
134154
let is = InstantServer::new();
135155
let colink_home = get_colink_home().unwrap();
136156
let registry_file = Path::new(&colink_home).join("reg_config");
137157
let _file = File::options()
138158
.write(true)
139159
.create_new(true)
140-
.open(&registry_file)
160+
.open(registry_file)
141161
.unwrap();
142-
is.get_colink().switch_to_generated_user().await.unwrap();
162+
let is = std::thread::spawn(move || {
163+
tokio::runtime::Builder::new_multi_thread()
164+
.enable_all()
165+
.build()
166+
.unwrap()
167+
.block_on(async {
168+
is.get_colink().switch_to_generated_user().await.unwrap();
169+
});
170+
is
171+
})
172+
.join()
173+
.unwrap();
143174
Self {
144175
_instant_server: is,
145176
}

tests/test_protocol_variable_transfer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl ProtocolEntry for Receiver {
5555

5656
#[tokio::test]
5757
async fn test_vt() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
58-
let ir = InstantRegistry::new().await;
58+
let ir = InstantRegistry::new();
5959
let mut iss = vec![];
6060
let mut cls = vec![];
6161
for i in 0..8 {

0 commit comments

Comments
 (0)