Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Havlik authored and Tom Havlik committed May 9, 2020
1 parent 127f979 commit 3b80a36
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 160 deletions.
5 changes: 4 additions & 1 deletion scout/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ redis = "0.15"
r2d2_redis = { git = "https://github.com/sorccu/r2d2-redis"}
anyhow = "1.0.28"
serde = "1.0.63"
bincode = "1.2.1"
bincode = "1.2.1"

[dev-dependencies]
uuid = { version = "0.8", features = ["v4"] }
4 changes: 2 additions & 2 deletions scout/lua/release.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ local resource = KEYS[1]
local claim = ARGV[1]
local current = redis.call('GET', resource)
if not current then
return {err='ErrNoClaim'}
return {err='NoClaim'}
elseif current ~= claim then
return {err='ErrBadClaim'}
return {err='BadClaim'}
end
redis.call('DEL', resource)
6 changes: 6 additions & 0 deletions scout/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use uuid::Uuid;
mod node;
mod pool;

pub mod rk {
pub fn claim(resource: &str) -> String {
format!("c:{}", resource)
}
}

#[cfg(test)]
mod test;

Expand Down
76 changes: 73 additions & 3 deletions scout/src/test/lua.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
use super::*;

#[cfg(test)]
mod release {
use super::*;

const SCRIPT: &'static str = include_str!("../../lua/release.lua");

#[test]
fn load() {
let mut client = redis::Client::open("redis://127.0.0.1:6379").unwrap();
let hash: String = redis::cmd("SCRIPT")
.arg("LOAD")
.arg(SCRIPT)
.query(&mut client)
.unwrap();
assert_eq!(hash.len(), 40);
}

#[test]
fn ok() {}
fn ok() {
let resource = Uuid::new_v4().to_string();
let claim = Uuid::new_v4().to_string();
let key = rk::claim(&resource);
let mut client = redis::Client::open("redis://127.0.0.1:6379").unwrap();
let _: () = redis::cmd("SET")
.arg(&key)
.arg(&claim)
.query(&mut client)
.unwrap();
let _: () = redis::cmd("EVAL")
.arg(SCRIPT)
.arg(1)
.arg(&key)
.arg(&claim)
.query(&mut client)
.unwrap();
let result: Option<String> = redis::cmd("GET")
.arg(&key)
.query(&mut client)
.unwrap();
assert!(result.is_none());
}

#[test]
fn err_no_claim() {}
fn err_no_claim() {
let resource = Uuid::new_v4().to_string();
let claim = Uuid::new_v4().to_string();
let key = rk::claim(&resource);
let mut client = redis::Client::open("redis://127.0.0.1:6379").unwrap();
let err = redis::cmd("EVAL")
.arg(SCRIPT)
.arg(1)
.arg(&key)
.arg(&claim)
.query::<()>(&mut client)
.unwrap_err();
assert_eq!(err.code().unwrap(), "NoClaim");
}

#[test]
fn err_bad_claim() {}
fn err_bad_claim() {
let resource = Uuid::new_v4().to_string();
let claim = Uuid::new_v4().to_string();
let key = rk::claim(&resource);
let mut client = redis::Client::open("redis://127.0.0.1:6379").unwrap();
let _: () = redis::cmd("SET")
.arg(&key)
.arg("foobarbaz")
.query(&mut client)
.unwrap();
let err = redis::cmd("EVAL")
.arg(SCRIPT)
.arg(1)
.arg(&key)
.arg(&claim)
.query::<()>(&mut client)
.unwrap_err();
assert_eq!(err.code().unwrap(), "BadClaim");
}
}
50 changes: 21 additions & 29 deletions types/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Code generated by oto; DO NOT EDIT.
pub use crate::types::*;
use anyhow::{Error, Result};
use reqwest;
use serde_json;

Expand Down Expand Up @@ -41,10 +42,7 @@ impl AudioInterfaceAsyncClient {

#[async_trait]
impl AudioInterface for AudioInterfaceAsyncClient {
async fn create_stream(
&self,
req: CreateStreamRequest,
) -> Result<CreateStreamResponse, String> {
async fn create_stream(&self, req: CreateStreamRequest) -> Result<CreateStreamResponse> {
match self
.client
.post(&self.create_stream_endpoint)
Expand All @@ -58,21 +56,18 @@ impl AudioInterface for AudioInterfaceAsyncClient {
Ok(mut result) => match status {
reqwest::StatusCode::OK => Ok(result),
_ => match result.take_error() {
Some(msg) => Err(msg),
None => Err(format!("status code {}", status)),
Some(msg) => Err(Error::msg(msg)),
None => Err(Error::msg(format!("status code {}", status))),
},
},
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}

async fn delete_stream(
&self,
req: DeleteStreamRequest,
) -> Result<DeleteStreamResponse, String> {
async fn delete_stream(&self, req: DeleteStreamRequest) -> Result<DeleteStreamResponse> {
match self
.client
.post(&self.delete_stream_endpoint)
Expand All @@ -86,21 +81,18 @@ impl AudioInterface for AudioInterfaceAsyncClient {
Ok(mut result) => match status {
reqwest::StatusCode::OK => Ok(result),
_ => match result.take_error() {
Some(msg) => Err(msg),
None => Err(format!("status code {}", status)),
Some(msg) => Err(Error::msg(msg)),
None => Err(Error::msg(format!("status code {}", status))),
},
},
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}

async fn get_device_info(
&self,
req: GetDeviceInfoRequest,
) -> Result<GetDeviceInfoResponse, String> {
async fn get_device_info(&self, req: GetDeviceInfoRequest) -> Result<GetDeviceInfoResponse> {
match self
.client
.post(&self.get_device_info_endpoint)
Expand All @@ -114,18 +106,18 @@ impl AudioInterface for AudioInterfaceAsyncClient {
Ok(mut result) => match status {
reqwest::StatusCode::OK => Ok(result),
_ => match result.take_error() {
Some(msg) => Err(msg),
None => Err(format!("status code {}", status)),
Some(msg) => Err(Error::msg(msg)),
None => Err(Error::msg(format!("status code {}", status))),
},
},
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}

async fn list_streams(&self, req: ListStreamsRequest) -> Result<ListStreamsResponse, String> {
async fn list_streams(&self, req: ListStreamsRequest) -> Result<ListStreamsResponse> {
match self
.client
.post(&self.list_streams_endpoint)
Expand All @@ -139,14 +131,14 @@ impl AudioInterface for AudioInterfaceAsyncClient {
Ok(mut result) => match status {
reqwest::StatusCode::OK => Ok(result),
_ => match result.take_error() {
Some(msg) => Err(msg),
None => Err(format!("status code {}", status)),
Some(msg) => Err(Error::msg(msg)),
None => Err(Error::msg(format!("status code {}", status))),
},
},
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}
Err(e) => Err(format!("{:?}", e)),
Err(e) => Err(Error::msg(format!("{:?}", e))),
}
}
}
9 changes: 5 additions & 4 deletions types/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::types;
use actix_web;
use anyhow::{Error, Result};
use colored::*;

fn error_json<T: std::string::ToString>(e: T) -> String {
Expand Down Expand Up @@ -215,19 +216,19 @@ pub mod test {
#[async_trait]
impl<T> types::AudioInterface for Client<T> where T: types::AudioInterface + std::clone::Clone {
async fn create_stream(&self, req: types::CreateStreamRequest) -> Result<types::CreateStreamResponse, String> {
async fn create_stream(&self, req: types::CreateStreamRequest) -> Result<types::CreateStreamResponse> {
Err(String::new())
}
async fn delete_stream(&self, req: types::DeleteStreamRequest) -> Result<types::DeleteStreamResponse, String> {
async fn delete_stream(&self, req: types::DeleteStreamRequest) -> Result<types::DeleteStreamResponse> {
Err(String::new())
}
async fn get_device_info(&self, req: types::GetDeviceInfoRequest) -> Result<types::GetDeviceInfoResponse, String> {
async fn get_device_info(&self, req: types::GetDeviceInfoRequest) -> Result<types::GetDeviceInfoResponse> {
Err(String::new())
}
async fn list_streams(&self, req: types::ListStreamsRequest) -> Result<types::ListStreamsResponse, String> {
async fn list_streams(&self, req: types::ListStreamsRequest) -> Result<types::ListStreamsResponse> {
Err(String::new())
}
Expand Down
Loading

0 comments on commit 3b80a36

Please sign in to comment.