-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheartbeat.rs
143 lines (125 loc) · 4.51 KB
/
heartbeat.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
132
133
134
135
136
137
138
139
140
141
142
143
use crate::api::server::AppState;
use actix_web::{
web::{self, post, Data},
HttpResponse, Scope,
};
use alloy::primitives::Address;
use shared::models::heartbeat::{HeartbeatRequest, HeartbeatResponse};
use std::str::FromStr;
async fn heartbeat(
heartbeat: web::Json<HeartbeatRequest>,
app_state: Data<AppState>,
) -> HttpResponse {
let task_info = heartbeat.clone();
let node_address = Address::from_str(&heartbeat.address).unwrap();
app_state.store_context.node_store.update_node_task(
node_address,
task_info.task_id,
task_info.task_state,
);
app_state.store_context.heartbeat_store.beat(&heartbeat);
app_state
.store_context
.metrics_store
.store_metrics(heartbeat.metrics.clone(), node_address);
let current_task = app_state.store_context.task_store.get_task();
let resp: HttpResponse = HeartbeatResponse { current_task }.into();
resp
}
pub fn heartbeat_routes() -> Scope {
web::scope("/heartbeat").route("", post().to(heartbeat))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::tests::helper::create_test_app_state;
use actix_web::http::StatusCode;
use actix_web::test;
use actix_web::App;
use serde_json::json;
use shared::models::task::TaskRequest;
#[actix_web::test]
async fn test_heartbeat() {
let app_state = create_test_app_state().await;
let app = test::init_service(
App::new()
.app_data(app_state.clone())
.route("/heartbeat", web::post().to(heartbeat)),
)
.await;
let address = "0x0000000000000000000000000000000000000000".to_string();
let req_payload = json!({"address": address});
let req = test::TestRequest::post()
.uri("/heartbeat")
.set_json(&req_payload)
.to_request();
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);
let body = test::read_body(resp).await;
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["success"], serde_json::Value::Bool(true));
assert_eq!(json["current_task"], serde_json::Value::Null);
let node_address = Address::from_str(&address).unwrap();
let value = app_state
.store_context
.heartbeat_store
.get_heartbeat(&node_address);
assert_eq!(
value,
Some(HeartbeatRequest {
address: "0x0000000000000000000000000000000000000000".to_string(),
task_id: None,
task_state: None,
metrics: None,
version: None
})
);
}
#[actix_web::test]
async fn test_heartbeat_with_task() {
let app_state = create_test_app_state().await;
let app = test::init_service(
App::new()
.app_data(app_state.clone())
.route("/heartbeat", web::post().to(heartbeat)),
)
.await;
let address = "0x0000000000000000000000000000000000000000".to_string();
let task = TaskRequest {
image: "test".to_string(),
name: "test".to_string(),
command: None,
args: None,
env_vars: None,
};
app_state.store_context.task_store.set_task(task.into());
let req = test::TestRequest::post()
.uri("/heartbeat")
.set_json(json!({"address": "0x0000000000000000000000000000000000000000"}))
.to_request();
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);
let body = test::read_body(resp).await;
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["success"], serde_json::Value::Bool(true));
assert_eq!(
json["data"]["current_task"]["image"],
serde_json::Value::String("test".to_string())
);
let node_address = Address::from_str(&address).unwrap();
let value = app_state
.store_context
.heartbeat_store
.get_heartbeat(&node_address);
// Task has not started yet
let value = value.unwrap();
let heartbeat = HeartbeatRequest {
address: "0x0000000000000000000000000000000000000000".to_string(),
task_id: None,
task_state: None,
metrics: None,
version: None,
};
assert_eq!(value, heartbeat);
}
}