-
Notifications
You must be signed in to change notification settings - Fork 132
/
setup.rs
122 lines (111 loc) · 3.59 KB
/
setup.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
use chain_impl_mockchain::chaintypes::ConsensusVersion;
use jormungandr_lib::interfaces::TrustedPeer;
use jormungandr_testing_utils::testing::{node::grpc::JormungandrClient, SyncNode};
use assert_fs::TempDir;
use jormungandr_testing_utils::testing::{
configuration::JormungandrParams,
jormungandr::{ConfigurationBuilder, JormungandrProcess, Starter},
};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::time::Duration;
const DEFAULT_SLOT_DURATION: u8 = 2;
const LOCALHOST: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
pub struct Config {
addr: SocketAddr,
}
impl Config {
pub fn attach_to_local_node(port: u16) -> Self {
Self {
addr: SocketAddr::new(LOCALHOST, port),
}
}
pub fn client(&self) -> JormungandrClient {
JormungandrClient::new(self.addr)
}
}
pub mod client {
use super::*;
pub struct ClientBootstrap {
pub client: JormungandrClient,
pub server: JormungandrProcess,
pub config: JormungandrParams,
}
pub fn default() -> ClientBootstrap {
bootstrap(
ConfigurationBuilder::new()
.with_slot_duration(DEFAULT_SLOT_DURATION)
.to_owned(),
)
}
pub fn bootstrap(config: ConfigurationBuilder) -> ClientBootstrap {
let dir = TempDir::new().unwrap();
let config = config.build(&dir);
let server = Starter::new()
.temp_dir(dir)
.config(config.clone())
.start_async()
.unwrap();
std::thread::sleep(Duration::from_secs(4));
let client = Config::attach_to_local_node(config.get_p2p_listen_port()).client();
ClientBootstrap {
client,
server,
config,
}
}
}
pub mod server {
use super::*;
use jormungandr_testing_utils::testing::configuration;
const SERVER_RETRY_WAIT: Duration = Duration::from_secs(1);
const TIMEOUT: Duration = Duration::from_secs(60);
pub struct ServerBootstrap {
pub server: JormungandrProcess,
pub config: JormungandrParams,
pub mock_port: u16,
}
pub fn default() -> ServerBootstrap {
bootstrap(
configuration::get_available_port(),
ConfigurationBuilder::new()
.with_slot_duration(DEFAULT_SLOT_DURATION)
.with_block0_consensus(ConsensusVersion::GenesisPraos)
.to_owned(),
)
}
pub fn bootstrap(mock_port: u16, mut config: ConfigurationBuilder) -> ServerBootstrap {
let dir = TempDir::new().unwrap();
let trusted_peer = TrustedPeer {
address: format!("/ip4/{}/tcp/{}", LOCALHOST, mock_port)
.parse()
.unwrap(),
id: None,
};
let config = config.with_trusted_peers(vec![trusted_peer]).build(&dir);
let server = Starter::new()
.temp_dir(dir)
.config(config.clone())
.start_async()
.unwrap();
ServerBootstrap {
server,
config,
mock_port,
}
}
impl ServerBootstrap {
pub fn wait_server_online(&self) {
let started = std::time::Instant::now();
loop {
if self.server.is_running() {
return;
}
if started.elapsed() > TIMEOUT {
println!("{}", self.server.log_content());
panic!("Timeout elapsed while waiting for server to go online");
}
std::thread::sleep(SERVER_RETRY_WAIT);
}
}
}
}