Skip to content

Commit

Permalink
Merge pull request #4 from MyK00L/mocking
Browse files Browse the repository at this point in the history
Mocking
  • Loading branch information
MyK00L authored Aug 21, 2021
2 parents ef3886b + 8db3261 commit fffe294
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 68 deletions.
1 change: 1 addition & 0 deletions protos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
tonic = "0.5"
prost = "0.8"
prost-types = "0.8"
paste = "1.0"

[build-dependencies]
tonic-build = "0.5"
Expand Down
6 changes: 3 additions & 3 deletions protos/protos/service/dispatcher.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package service.dispatcher;

import "evaluation.proto";

message DispatcherRequest {
message EvaluateSubmissionRequest {
evaluation.Submission sub = 1;
}
message DispatcherResponse {
message EvaluateSubmissionResponse {
evaluation.EvaluationResult res = 1;
}
service Dispatcher {
rpc evaluate_submission(DispatcherRequest) returns (DispatcherResponse);
rpc evaluate_submission(EvaluateSubmissionRequest) returns (EvaluateSubmissionResponse);
}
12 changes: 6 additions & 6 deletions protos/protos/service/evaluation_files.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ package service.evaluation_files;

import "scoring.proto";

message ScorerInfoRequest {}
message ScorerInfoResponse {
message GetScorerInfoRequest {}
message GetScorerInfoResponse {
uint32 problem_id = 1;
repeated scoring.Subtask subtasks_scoring = 2;
scoring.Problem submission_scoring = 3; // not really needed rn as all of these are just sum of subtasks for a single sub
}
message ScoreboardInfoRequest {}
message ScoreboardInfoResponse {
message GetScoreboardInfoRequest {}
message GetScoreboardInfoResponse {
repeated scoring.Problem problems_scoring = 1;
scoring.User user_scoring = 2;
}

service Evaluation {
rpc get_scorer_info(ScorerInfoRequest) returns (ScorerInfoResponse);
rpc get_scoreboard_info(ScoreboardInfoRequest) returns (ScoreboardInfoResponse);
rpc get_scorer_info(GetScorerInfoRequest) returns (GetScorerInfoResponse);
rpc get_scoreboard_info(GetScoreboardInfoRequest) returns (GetScoreboardInfoResponse);
}
18 changes: 9 additions & 9 deletions protos/protos/service/submission.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import "common.proto";
import "evaluation.proto";

// Submission service
message SubmissionRequest {
message EvaluateSubmissionRequest {
evaluation.Submission sub = 1;
}
message SubmissionResponse {
message EvaluateSubmissionResponse {
evaluation.EvaluationResult res = 1;
common.Id submission_id = 2;
google.protobuf.Timestamp timestamp = 3;
}
message SubmissionListRequest {
message GetSubmissionListRequest {
optional uint32 limit = 1;
optional string user = 2;
optional uint32 problem_id = 3;
}
message SubmissionListResponse {
message GetSubmissionListResponse {
message Item {
common.Id sub_id = 1;
string user = 2;
Expand All @@ -29,15 +29,15 @@ message SubmissionListResponse {
}
repeated Item list = 1;
}
message SubmissionDetailsRequest {
message GetSubmissionDetailsRequest {
common.Id submission_id = 1;
}
message SubmissionDetailsResponse {
message GetSubmissionDetailsResponse {
evaluation.Submission sub = 1;
google.protobuf.Timestamp timestamp = 2;
}
service Submission {
rpc evaluate_submission(SubmissionRequest) returns (SubmissionResponse);
rpc get_submission_list(SubmissionListRequest) returns (SubmissionListResponse);
rpc get_submission_details(SubmissionDetailsRequest) returns (SubmissionDetailsResponse);
rpc evaluate_submission(EvaluateSubmissionRequest) returns (EvaluateSubmissionResponse);
rpc get_submission_list(GetSubmissionListRequest) returns (GetSubmissionListResponse);
rpc get_submission_details(GetSubmissionDetailsRequest) returns (GetSubmissionDetailsResponse);
}
12 changes: 6 additions & 6 deletions protos/protos/service/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ syntax = "proto3";

package service.test;

message StringRequest {
message TestStringRequest {
string str = 1;
}
message StringResponse {
message TestStringResponse {
string str = 1;
}

message LogRequest {
message LogStringRequest {
string str = 1;
}
message LogResponse {}
message LogStringResponse {}

service Test {
rpc test_string(StringRequest) returns (StringResponse);
rpc log_string(LogRequest) returns (LogResponse);
rpc test_string(TestStringRequest) returns (TestStringResponse);
rpc log_string(LogStringRequest) returns (LogStringResponse);
}
18 changes: 9 additions & 9 deletions protos/protos/service/worker.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import "common.proto";
import "evaluation.proto";
import "worker.proto";

message WorkerRequest {
message EvaluateSubmissionRequest {
uint32 problem_id = 1;
common.Source source = 2;
}
message WorkerResponse {
message EvaluateSubmissionResponse {
evaluation.CompilationResult compilation_result = 1;
repeated evaluation.TestcaseResult testcase_results = 2;
}

message WorkerUpdateTestcaseRequest {
message UpdateTestcaseRequest {
.worker.Testcase tc = 1;
}
message WorkerUpdateTestcaseResponse {}
message UpdateTestcaseResponse {}

message WorkerUpdateSourceRequest {
message UpdateSourceRequest {
.worker.SourceFile file = 1;
}
message WorkerUpdateSourceResponse {}
message UpdateSourceResponse {}

service Worker {
rpc evaluate_submission(WorkerRequest) returns (WorkerResponse);
rpc update_testcase(WorkerUpdateTestcaseRequest) returns (WorkerUpdateTestcaseResponse);
rpc update_source(WorkerUpdateSourceRequest) returns (WorkerUpdateSourceResponse);
rpc evaluate_submission(EvaluateSubmissionRequest) returns (EvaluateSubmissionResponse);
rpc update_testcase(UpdateTestcaseRequest) returns (UpdateTestcaseResponse);
rpc update_source(UpdateSourceRequest) returns (UpdateSourceResponse);
}
35 changes: 35 additions & 0 deletions protos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![feature(trait_alias)]

#[macro_use]
mod mock_macro;

pub mod common {
tonic::include_proto!("common");
}
Expand All @@ -18,21 +21,53 @@ pub mod worker {
pub mod service {
pub mod contest {
tonic::include_proto!("service.contest");
rpc_mock_server!(contest_server::Contest; MockContest;
(auth_user,AuthUserRequest,AuthUserResponse),
(get_contest,GetContestRequest,GetContestResponse),
(get_problem,GetProblemRequest,GetProblemResponse),
(get_announcement_list,GetAnnouncementListRequest,GetAnnouncementListResponse),
(get_question_list,GetQuestionListRequest,GetQuestionListResponse),
(set_user,SetUserRequest,SetUserResponse),
(set_contest,SetContestRequest,SetContestResponse),
(set_problem,SetProblemRequest,SetProblemResponse),
(add_question,AddQuestionRequest,AddQuestionResponse),
(add_announcement,AddAnnouncementRequest,AddAnnouncementResponse)
);
}
pub mod dispatcher {
tonic::include_proto!("service.dispatcher");
rpc_mock_server!(dispatcher_server::Dispatcher; MockDispatcher;
(evaluate_submission,EvaluateSubmissionRequest,EvaluateSubmissionResponse)
);
}
pub mod evaluation_files {
tonic::include_proto!("service.evaluation_files");
rpc_mock_server!(evaluation_server::Evaluation; MockEvaluation;
(get_scorer_info,GetScorerInfoRequest,GetScorerInfoResponse),
(get_scoreboard_info,GetScoreboardInfoRequest,GetScoreboardInfoResponse)
);
}
pub mod submission {
tonic::include_proto!("service.submission");
rpc_mock_server!(submission_server::Submission; MockSubmission;
(evaluate_submission,EvaluateSubmissionRequest,EvaluateSubmissionResponse),
(get_submission_list,GetSubmissionListRequest,GetSubmissionListResponse),
(get_submission_details,GetSubmissionDetailsRequest,GetSubmissionDetailsResponse)
);
}
pub mod worker {
tonic::include_proto!("service.worker");
rpc_mock_server!(worker_server::Worker; MockWorker;
(evaluate_submission,EvaluateSubmissionRequest,EvaluateSubmissionResponse),
(update_testcase,UpdateTestcaseRequest,UpdateTestcaseResponse),
(update_source,UpdateSourceRequest,UpdateSourceResponse)
);
}
pub mod test {
tonic::include_proto!("service.test");
rpc_mock_server!(test_server::Test; MockTest;
(test_string,TestStringRequest,TestStringResponse),
(log_string,LogStringRequest,LogStringResponse));
}
}

Expand Down
82 changes: 82 additions & 0 deletions protos/src/mock_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// overly complicated due to async-trait
macro_rules! rpc_mock_fn {
( $stname:ident, $fname:ident, $rname:ident, $in:ty, $out:ty ) => {
fn $fname<'life0, 'async_trait>(
&'life0 self,
req: tonic::Request<$in>,
) -> core::pin::Pin<
Box<
(dyn core::future::Future<Output = Result<tonic::Response<$out>, tonic::Status>>
+ Send
+ 'async_trait),
>,
>
where
'life0: 'async_trait,
{
async fn f(
_self: &$stname,
req: tonic::Request<$in>,
) -> Result<tonic::Response<$out>, tonic::Status> {
let res = match _self.$rname.clone() {
Ok(x) => Ok(tonic::Response::new(x)),
Err(x) => Err(tonic::Status::new(x.0, x.1)),
};
eprintln!(
"{}:\nreceived {:?}\nresponding{:?}",
std::any::type_name::<$stname>(),
req,
res
);
res
}
Box::pin(f(self, req))
}
};
}

macro_rules! rpc_mock_setters {
( $fname:ident, $rname:ident, $in:ty, $out:ty ) => {
paste::paste! {
pub fn [<$fname _set>] (&mut self, val: $out) {
self.$rname = Ok(val);
}
pub fn [<$fname _set_err>] (&mut self, val: tonic::Status) {
self.$rname = Err((val.code(),val.message().to_string()));
}
}
};
}

#[macro_export]
macro_rules! rpc_mock_server {
( $trait:ty; $stname:ident; $( ($fname:ident, $in:ty, $out:ty) ),* ) => {
paste::paste!{
#[derive(Debug, Clone)]
pub struct $stname {
$(
[<$fname _return>] : Result<$out,(tonic::Code,String)>,
)*
}
impl $trait for $stname {
$(
rpc_mock_fn!($stname, $fname, [<$fname _return>], $in, $out);
)*
}
impl $stname {
$(
rpc_mock_setters!($fname,[<$fname _return>],$in,$out);
)*
}
impl Default for $stname {
fn default() -> Self {
Self {
$(
[<$fname _return>] : Err((tonic::Code::Internal,String::from("the response for this mock method was not set"))),
)*
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion rpctest-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async fn not_main<C: ChannelTrait>(
let mut index = 1u32;
loop {
let mut client = main_client.clone();
let request = tonic::Request::new(StringRequest {
let request = tonic::Request::new(TestStringRequest {
str: format!("Request#{}", index),
});

Expand Down
22 changes: 15 additions & 7 deletions rpctest-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,36 @@ impl Default for MyTest<Channel> {
impl<T: ChannelTrait> Test for MyTest<T> {
async fn test_string(
&self,
request: tonic::Request<StringRequest>,
) -> Result<tonic::Response<StringResponse>, tonic::Status> {
request: tonic::Request<TestStringRequest>,
) -> Result<tonic::Response<TestStringResponse>, tonic::Status> {
let mut test_client = self.test_client.clone();
let addr = request.remote_addr();
let inner = request.into_inner();
test_client
.log_string(tonic::Request::new(LogRequest {
.log_string(tonic::Request::new(LogStringRequest {
str: format!("received request with value {:?} from {:?}", inner, addr),
}))
.await?;
let reply = StringResponse {
eprintln!(
"mock test: {:?}",
test_client
.test_string(tonic::Request::new(TestStringRequest {
str: String::from("(ᓀ ᓀ)")
}))
.await?
);
let reply = TestStringResponse {
str: format!("Hello {}", inner.str),
};
Ok(Response::new(reply))
}
async fn log_string(
&self,
request: tonic::Request<LogRequest>,
) -> Result<tonic::Response<LogResponse>, tonic::Status> {
request: tonic::Request<LogStringRequest>,
) -> Result<tonic::Response<LogStringResponse>, tonic::Status> {
let inner = request.into_inner();
eprintln!("{:?}", inner);
Ok(Response::new(LogResponse {}))
Ok(Response::new(LogStringResponse {}))
}
}

Expand Down
Loading

0 comments on commit fffe294

Please sign in to comment.