Skip to content

Commit

Permalink
benches & tests errors fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulmathur16 committed Jun 13, 2024
1 parent 8f50e7b commit fbaac3d
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 54 deletions.
10 changes: 7 additions & 3 deletions benches/data_loader_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use criterion::Criterion;
use hyper::body::Bytes;
use reqwest::Request;
use tailcall::core::config::Batch;
use tailcall::core::error::file::FileError;
use tailcall::core::error::http::HttpError;
use tailcall::core::http::{DataLoaderRequest, HttpDataLoader, Response};
use tailcall::core::ir::IoId;
use tailcall::core::runtime::TargetRuntime;
Expand All @@ -23,7 +25,8 @@ struct MockHttpClient {

#[async_trait::async_trait]
impl HttpIO for MockHttpClient {
async fn execute(&self, _req: Request) -> anyhow::Result<Response<Bytes>> {
type Error = HttpError;
async fn execute(&self, _req: Request) -> Result<Response<Bytes>, Self::Error> {
Ok(Response::empty())
}
}
Expand All @@ -39,11 +42,12 @@ struct File;

#[async_trait::async_trait]
impl FileIO for File {
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> anyhow::Result<()> {
type Error = FileError;
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> Result<(), Self::Error> {
unimplemented!("Not needed for this bench")
}

async fn read<'a>(&'a self, _: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, _: &'a str) -> Result<String, Self::Error> {
unimplemented!("Not needed for this bench")
}
}
Expand Down
10 changes: 7 additions & 3 deletions benches/impl_path_string_for_evaluation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use reqwest::{Client, Request};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use tailcall::core::blueprint::{Server, Upstream};
use tailcall::core::cache::InMemoryCache;
use tailcall::core::error::file::FileError;
use tailcall::core::error::http::HttpError;
use tailcall::core::http::{RequestContext, Response};
use tailcall::core::ir::{EvaluationContext, ResolverContextLike};
use tailcall::core::path::PathString;
Expand Down Expand Up @@ -70,7 +72,8 @@ impl Http {

#[async_trait]
impl HttpIO for Http {
async fn execute(&self, mut request: Request) -> anyhow::Result<Response<Bytes>> {
type Error = HttpError;
async fn execute(&self, mut request: Request) -> Result<Response<Bytes>, Self::Error> {
if self.http2_only {
*request.version_mut() = reqwest::Version::HTTP_2;
}
Expand All @@ -90,11 +93,12 @@ impl EnvIO for Env {
struct File;
#[async_trait]
impl FileIO for File {
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> anyhow::Result<()> {
type Error = FileError;
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> Result<(), Self::Error> {
unimplemented!("Not needed for this bench")
}

async fn read<'a>(&'a self, _: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, _: &'a str) -> Result<String, Self::Error> {
unimplemented!("Not needed for this bench")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/errata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fmt::{Debug, Display};
use colored::Colorize;
use derive_setters::Setters;

use crate::core::valid::ValidationError;
use crate::core::error::Error as CoreError;
use crate::core::valid::ValidationError;

/// The moral equivalent of a serde_json::Value but for errors.
/// It's a data structure like Value that can hold any error in an untyped
Expand Down
30 changes: 29 additions & 1 deletion src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub mod file {

#[derive(From, thiserror::Error, Debug)]
pub enum FileError {
#[error("File not found")]
#[error("No such file or directory (os error 2)")]
NotFound,

#[error("No permission to access the file")]
Expand All @@ -145,6 +145,12 @@ pub mod file {
#[error("Invalid file format")]
InvalidFormat,

#[error("Invalid file path")]
InvalidFilePath,

#[error("Invalid OS string")]
InvalidOsString,

#[error("Failed to read file : {0}")]
FileReadFailed(String),

Expand All @@ -160,6 +166,9 @@ pub mod file {

#[error("File writing not supported on Lambda.")]
LambdaFileWriteNotSupported,

#[error("Cannot write to a file in an execution spec")]
ExecutionSpecFileWriteFailed,
}
}

Expand Down Expand Up @@ -195,6 +204,25 @@ pub mod http {
#[error("Unable to find key {0} in query params")]
#[from(ignore)]
KeyNotFound(String),

#[error("Invalid Status Code")]
InvalidStatusCode(hyper::http::status::InvalidStatusCode),

#[error("Status Code error")]
StatusCode,

#[error("Invalid Header Value")]
InvalidHeaderValue(hyper::header::InvalidHeaderValue),

#[error("Invalid Header Name")]
InvalidHeaderName(hyper::header::InvalidHeaderName),

#[error("No mock found for request: {method} {url} in {spec_path}")]
NoMockFound {
method: String,
url: String,
spec_path: String,
},
}

#[derive(From, thiserror::Error, Debug)]
Expand Down
3 changes: 1 addition & 2 deletions src/core/generator/tests/json_to_config_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use std::path::Path;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use tailcall::core::error::Error;
use tailcall::core::generator::{from_json, ConfigGenerationRequest};
use url::Url;

use crate::core::error::Error;

#[derive(Serialize, Deserialize)]
struct JsonFixture {
url: String,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
use std::cell::Cell;

use mimalloc::MiMalloc;
use tailcall::core::error::Error;
use tailcall::core::tracing::default_tracing_tailcall;
use tailcall::core::Errata;
use tracing::subscriber::DefaultGuard;
use tailcall::core::error::Error;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
Expand Down
5 changes: 4 additions & 1 deletion tailcall-cloudflare/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ fn init_env(env: Rc<worker::Env>) -> Arc<dyn EnvIO> {
Arc::new(env::CloudflareEnv::init(env))
}

fn init_file(env: Rc<worker::Env>, bucket_id: &str) -> anyhow::Result<Arc<dyn FileIO<Error = FileError>>> {
fn init_file(
env: Rc<worker::Env>,
bucket_id: &str,
) -> anyhow::Result<Arc<dyn FileIO<Error = FileError>>> {
Ok(Arc::new(file::CloudflareFileIO::init(env, bucket_id)?))
}

Expand Down
29 changes: 14 additions & 15 deletions tests/core/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate core;

use std::path::PathBuf;

use anyhow::{anyhow, Context};
use tailcall::core::error::file::FileError;
use tailcall::core::FileIO;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

Expand All @@ -19,20 +19,22 @@ impl File {

#[async_trait::async_trait]
impl FileIO for File {
async fn write<'a>(&'a self, _path: &'a str, _content: &'a [u8]) -> anyhow::Result<()> {
Err(anyhow!("Cannot write to a file in an execution spec"))
type Error = FileError;
async fn write<'a>(&'a self, _path: &'a str, _content: &'a [u8]) -> Result<(), Self::Error> {
Err(FileError::ExecutionSpecFileWriteFailed)
}

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, path: &'a str) -> Result<String, Self::Error> {
let base = PathBuf::from(path);
let path = base
.file_name()
.context("Invalid file path")?
.ok_or(FileError::InvalidFilePath)?
.to_str()
.context("Invalid OsString")?;
.ok_or(FileError::InvalidOsString)?;

match self.spec.files.get(path) {
Some(x) => Ok(x.to_owned()),
None => Err(anyhow!("No such file or directory (os error 2)")),
None => Err(FileError::NotFound),
}
}
}
Expand All @@ -48,20 +50,17 @@ impl TestFileIO {

#[async_trait::async_trait]
impl FileIO for TestFileIO {
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> anyhow::Result<()> {
type Error = FileError;
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> Result<(), Self::Error> {
let mut file = tokio::fs::File::create(path).await?;
file.write_all(content)
.await
.map_err(|e| anyhow!("{}", e))?;
file.write_all(content).await?;
Ok(())
}

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, path: &'a str) -> Result<String, Self::Error> {
let mut file = tokio::fs::File::open(path).await?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.await
.map_err(|e| anyhow!("{}", e))?;
file.read_to_end(&mut buffer).await?;
Ok(String::from_utf8(buffer)?)
}
}
18 changes: 9 additions & 9 deletions tests/core/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::str::FromStr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use anyhow::anyhow;
use hyper::body::Bytes;
use reqwest::header::{HeaderName, HeaderValue};
use tailcall::core::error::http::HttpError;
use tailcall::core::http::Response;
use tailcall::core::HttpIO;

Expand Down Expand Up @@ -54,7 +54,8 @@ impl Http {

#[async_trait::async_trait]
impl HttpIO for Http {
async fn execute(&self, req: reqwest::Request) -> anyhow::Result<Response<Bytes>> {
type Error = HttpError;
async fn execute(&self, req: reqwest::Request) -> Result<Response<Bytes>, Self::Error> {
// Try to find a matching mock for the incoming request.
let execution_mock = self
.mocks
Expand Down Expand Up @@ -94,12 +95,11 @@ impl HttpIO for Http {
});
method_match && url_match && headers_match && body_match
})
.ok_or(anyhow!(
"No mock found for request: {:?} {} in {}",
req.method(),
req.url(),
self.spec_path
))?;
.ok_or_else(|| HttpError::NoMockFound {
method: req.method().to_string(),
url: req.url().to_string(),
spec_path: self.spec_path.to_string(),
})?;

execution_mock.actual_hits.fetch_add(1, Ordering::Relaxed);

Expand All @@ -110,7 +110,7 @@ impl HttpIO for Http {
let status_code = reqwest::StatusCode::from_u16(mock_response.0.status)?;

if status_code.is_client_error() || status_code.is_server_error() {
return Err(anyhow::format_err!("Status code error"));
return Err(HttpError::StatusCode);
}

let mut response = Response { status: status_code, ..Default::default() };
Expand Down
15 changes: 8 additions & 7 deletions tests/core/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tailcall::cli::javascript;
use tailcall::core::blueprint::Blueprint;
use tailcall::core::cache::InMemoryCache;
use tailcall::core::config::{ConfigModule, Source};
use tailcall::core::error::worker::WorkerError;
use tailcall::core::http::AppContext;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::{Command, Event};
Expand Down Expand Up @@ -277,19 +278,19 @@ impl ExecutionSpec {

let http2_only = http.clone();

let http_worker: Option<Arc<dyn WorkerIO<Event, Command>>> =
let http_worker: Option<Arc<dyn WorkerIO<Event, Command, Error = WorkerError>>> =
if let Some(script) = script.clone() {
Some(javascript::init_worker_io(script))
} else {
None
};

let worker: Option<Arc<dyn WorkerIO<ConstValue, ConstValue>>> = if let Some(script) = script
{
Some(javascript::init_worker_io(script))
} else {
None
};
let worker: Option<Arc<dyn WorkerIO<ConstValue, ConstValue, Error = WorkerError>>> =
if let Some(script) = script {
Some(javascript::init_worker_io(script))
} else {
None
};

let runtime = TargetRuntime {
http,
Expand Down
3 changes: 2 additions & 1 deletion tests/core/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tailcall::core::async_graphql_hyper::{GraphQLBatchRequest, GraphQLRequest};
use tailcall::core::blueprint::Blueprint;
use tailcall::core::config::reader::ConfigReader;
use tailcall::core::config::{Config, ConfigModule, Source};
use tailcall::core::error::Error;
use tailcall::core::http::{handle_request, AppContext};
use tailcall::core::merge_right::MergeRight;
use tailcall::core::print_schema::print_schema;
Expand Down Expand Up @@ -301,7 +302,7 @@ pub async fn load_and_test_execution_spec(path: &Path) -> anyhow::Result<()> {
async fn run_test(
app_ctx: Arc<AppContext>,
request: &APIRequest,
) -> anyhow::Result<hyper::Response<Body>> {
) -> Result<hyper::Response<Body>, Error> {
let body = request
.body
.as_ref()
Expand Down
20 changes: 10 additions & 10 deletions tests/server_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod test {
use std::sync::Arc;
use std::time::Duration;

use anyhow::{anyhow, Result};
use anyhow::Result;
use async_graphql::Value;
use http_cache_reqwest::{Cache, CacheMode, HttpCache, HttpCacheOptions};
use hyper::body::Bytes;
Expand All @@ -14,6 +14,8 @@ pub mod test {
use tailcall::cli::javascript::init_worker_io;
use tailcall::core::blueprint::{Script, Upstream};
use tailcall::core::cache::InMemoryCache;
use tailcall::core::error::file::FileError;
use tailcall::core::error::http::HttpError;
use tailcall::core::http::Response;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::{Command, Event};
Expand Down Expand Up @@ -73,7 +75,8 @@ pub mod test {

#[async_trait::async_trait]
impl HttpIO for TestHttp {
async fn execute(&self, request: reqwest::Request) -> Result<Response<Bytes>> {
type Error = HttpError;
async fn execute(&self, request: reqwest::Request) -> Result<Response<Bytes>, HttpError> {
let response = self.client.execute(request).await;
Response::from_reqwest(
response?
Expand All @@ -95,20 +98,17 @@ pub mod test {

#[async_trait::async_trait]
impl FileIO for TestFileIO {
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> anyhow::Result<()> {
type Error = FileError;
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> Result<(), Self::Error> {
let mut file = tokio::fs::File::create(path).await?;
file.write_all(content)
.await
.map_err(|e| anyhow!("{}", e))?;
file.write_all(content).await?;
Ok(())
}

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, path: &'a str) -> Result<String, Self::Error> {
let mut file = tokio::fs::File::open(path).await?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.await
.map_err(|e| anyhow!("{}", e))?;
file.read_to_end(&mut buffer).await?;
Ok(String::from_utf8(buffer)?)
}
}
Expand Down

0 comments on commit fbaac3d

Please sign in to comment.