Skip to content

Commit

Permalink
refactor: Refactor execution_spec (#1727)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
webbdays and tusharmath authored Apr 30, 2024
1 parent 28b670f commit 260907d
Show file tree
Hide file tree
Showing 874 changed files with 11,775 additions and 11,699 deletions.
83 changes: 83 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ convert_case = "0.6.0"
rand = "0.8.5"
tailcall-macros = { path = "tailcall-macros" }
tonic-types = "0.11.0"
datatest-stable = "0.2.7"
tokio-test = "0.4.4"


[dev-dependencies]
Expand Down Expand Up @@ -244,3 +246,8 @@ harness = false
[[bench]]
name = "protobuf_convert_output"
harness = false

[[test]]
name = "execution_spec"
harness = false

23 changes: 23 additions & 0 deletions tests/core/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate core;

use std::borrow::Cow;
use std::collections::HashMap;

use tailcall::EnvIO;

#[derive(Clone)]
pub struct Env {
vars: HashMap<String, String>,
}

impl EnvIO for Env {
fn get(&self, key: &str) -> Option<Cow<'_, str>> {
self.vars.get(key).map(Cow::from)
}
}

impl Env {
pub fn init(vars: Option<HashMap<String, String>>) -> Self {
Self { vars: vars.unwrap_or_default() }
}
}
67 changes: 67 additions & 0 deletions tests/core/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
extern crate core;

use std::path::PathBuf;

use anyhow::{anyhow, Context};
use tailcall::FileIO;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use super::runtime::ExecutionSpec;
pub struct File {
spec: ExecutionSpec,
}

impl File {
pub fn new(spec: ExecutionSpec) -> File {
File { spec }
}
}

#[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"))
}

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
let base = PathBuf::from(path);
let path = base
.file_name()
.context("Invalid file path")?
.to_str()
.context("Invalid OsString")?;
match self.spec.files.get(path) {
Some(x) => Ok(x.to_owned()),
None => Err(anyhow!("No such file or directory (os error 2)")),
}
}
}

#[derive(Clone)]
pub struct TestFileIO {}

impl TestFileIO {
pub fn init() -> Self {
TestFileIO {}
}
}

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

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
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))?;
Ok(String::from_utf8(buffer)?)
}
}
Loading

1 comment on commit 260907d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.53ms 3.47ms 117.67ms 72.08%
Req/Sec 3.36k 211.48 4.46k 92.33%

401656 requests in 30.02s, 2.01GB read

Requests/sec: 13379.35

Transfer/sec: 68.67MB

Please sign in to comment.