forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_server.rs
131 lines (110 loc) Β· 3.43 KB
/
test_server.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use {
super::*,
crate::command_builder::ToArgs,
qtumcore_rpc::{Auth, Client, RpcApi},
reqwest::blocking::Response,
};
pub(crate) struct TestServer {
child: Child,
port: u16,
#[allow(unused)]
tempdir: TempDir,
rpc_url: String,
}
impl TestServer {
pub(crate) fn spawn_with_args(
rpc_server: &test_bitcoincore_rpc::Handle,
ord_args: &[&str],
) -> Self {
Self::spawn_with_server_args(rpc_server, ord_args, &[])
}
pub(crate) fn spawn_with_server_args(
rpc_server: &test_bitcoincore_rpc::Handle,
ord_args: &[&str],
server_args: &[&str],
) -> Self {
let tempdir = TempDir::new().unwrap();
let cookie_file = match rpc_server.network().as_str() {
"mainnet" => tempdir.path().join(".cookie"),
network => {
fs::create_dir(tempdir.path().join(network)).unwrap();
tempdir.path().join(format!("{network}/.cookie"))
}
};
fs::write(cookie_file.clone(), "foo:bar").unwrap();
let port = TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let child = Command::new(executable_path("qord")).args(format!(
"--rpc-url {} --bitcoin-data-dir {} --data-dir {} {} server {} --http-port {port} --address 127.0.0.1",
rpc_server.url(),
tempdir.path().display(),
tempdir.path().display(),
ord_args.join(" "),
server_args.join(" "),
).to_args())
.env("ORD_INTEGRATION_TEST", "1")
.current_dir(&tempdir)
.spawn().unwrap();
for i in 0.. {
match reqwest::blocking::get(format!("http://127.0.0.1:{port}/status")) {
Ok(_) => break,
Err(err) => {
if i == 400 {
panic!("Server failed to start: {err}");
}
}
}
thread::sleep(Duration::from_millis(25));
}
Self {
child,
tempdir,
port,
rpc_url: rpc_server.url(),
}
}
pub(crate) fn url(&self) -> Url {
format!("http://127.0.0.1:{}", self.port).parse().unwrap()
}
pub(crate) fn assert_response_regex(&self, path: impl AsRef<str>, regex: impl AsRef<str>) {
self.sync_server();
let response = reqwest::blocking::get(self.url().join(path.as_ref()).unwrap()).unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert_regex_match!(response.text().unwrap(), regex.as_ref());
}
pub(crate) fn request(&self, path: impl AsRef<str>) -> Response {
self.sync_server();
reqwest::blocking::get(self.url().join(path.as_ref()).unwrap()).unwrap()
}
pub(crate) fn json_request(&self, path: impl AsRef<str>) -> Response {
self.sync_server();
let client = reqwest::blocking::Client::new();
client
.get(self.url().join(path.as_ref()).unwrap())
.header(reqwest::header::ACCEPT, "application/json")
.send()
.unwrap()
}
pub(crate) fn sync_server(&self) {
let client = Client::new(&self.rpc_url, Auth::None).unwrap();
let chain_block_count = client.get_block_count().unwrap() + 1;
for i in 0.. {
let response = reqwest::blocking::get(self.url().join("/blockcount").unwrap()).unwrap();
assert_eq!(response.status(), StatusCode::OK);
if response.text().unwrap().parse::<u64>().unwrap() >= chain_block_count {
break;
} else if i == 20 {
panic!("index failed to synchronize with chain");
}
thread::sleep(Duration::from_millis(25));
}
}
}
impl Drop for TestServer {
fn drop(&mut self) {
self.child.kill().unwrap()
}
}