Skip to content

Commit

Permalink
Few new features
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed Jun 18, 2024
1 parent e1cb1c7 commit dd41dc7
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/install-redis

- name: Set up PDM
uses: pdm-project/setup-pdm@v3
Expand Down Expand Up @@ -152,6 +153,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/install-redis

# Js project
- name: "Install Bun, no npm should be needed"
Expand Down Expand Up @@ -187,6 +189,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/install-redis

- uses: actions/setup-python@v4
with:
Expand Down Expand Up @@ -230,6 +233,7 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/install-redis

# Basic python always needed:
- uses: ./.github/actions/install-python
Expand Down
11 changes: 11 additions & 0 deletions dev_scripts/initial_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,17 @@ initial_setup () {
./dev_scripts/py_rust.sh ensure_venv


echo "Make sure redis installed..."
if command -v redis-server > /dev/null 2>&1; then
echo "redis-server already installed"
else
echo "redis-server could not be found, installing..."
if [ "$(uname)" == "Darwin" ]; then
brew install redis
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sudo apt-get install redis-server
fi
fi

}

Expand Down
2 changes: 2 additions & 0 deletions dev_scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ cargo_py_rust_check () {
}

rust () {
./dev_scripts/utils.sh ensure_redis

cargo nextest run --manifest-path ./rust/Cargo.toml --all-features $@
}

rust_bench () {
./dev_scripts/utils.sh ensure_redis

cargo bench --manifest-path ./rust/Cargo.toml --all-features $@
}
Expand Down
10 changes: 10 additions & 0 deletions dev_scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ replace_text () {
awk "{sub(\"$1\",\"$2\")} {print}" $3 > temp.txt && mv temp.txt $3
}

# Make sure redis is up and running:
ensure_redis () {
if ! redis-cli ping; then
if [ "$(uname)" == "Darwin" ]; then
brew services start redis
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sudo systemctl restart redis-server
fi
fi
}


# Returns "true" if looks like in_ci, "false" otherwise:
Expand Down
3 changes: 2 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ axum-extra = { version = "0.9", features = [], optional = true }
leptos = { version = "0.6", optional = true }
leptos_axum = { version = "0.6", optional = true }
http = { version = "1", optional = true }
portpicker = { version = '0.1', optional = true }

# FEAT: hash:
sha2 = { version = "0.10", optional = true }
Expand Down Expand Up @@ -105,7 +106,6 @@ tokio = { version = '1', features = ["time", "sync"] }
[dev-dependencies]
rstest = "0.18"
criterion = { version = "0.3", features = ["html_reports", "async_tokio"] }
portpicker = '0.1.1'
tempfile = '3.8'
tokio = { version = '1', features = ["full"] }

Expand All @@ -130,6 +130,7 @@ redis = [
'dep:rand',
'chrono',
'dep:uuid',
'dep:portpicker',
]
opentelemetry-grpc = [
'dep:tracing-log',
Expand Down
12 changes: 12 additions & 0 deletions rust/bitbazaar/redis/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ impl<'a> RedisConn<'a> {
self.conn.as_mut()
}

/// Ping redis, returning true if it's up.
pub async fn ping(&mut self) -> bool {
if let Some(conn) = self.get_inner_conn().await {
redis::cmd("PING")
.query_async::<_, String>(conn)
.await
.is_ok()
} else {
false
}
}

/// Get a new [`RedisBatch`] for this connection that commands can be piped together with.
pub fn batch<'ref_lt>(&'ref_lt mut self) -> RedisBatch<'ref_lt, 'a, '_, ()> {
RedisBatch::new(self)
Expand Down
41 changes: 7 additions & 34 deletions rust/bitbazaar/redis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ mod script;
mod temp_list;
mod wrapper;

mod standalone;

pub use standalone::*;

pub use batch::{RedisBatch, RedisBatchFire, RedisBatchReturningOps};
pub use conn::RedisConn;
pub use dlock::{RedisLock, RedisLockErr};
Expand All @@ -22,33 +26,19 @@ pub use wrapper::Redis;
#[cfg(test)]
mod tests {
use std::{
process::{Child, Command},
sync::{atomic::AtomicU8, Arc},
time::Duration,
};

use portpicker::is_free;
use rstest::*;

use super::*;
use crate::{
errors::prelude::*,
log::GlobalLog,
misc::in_ci,
redis::{dlock::redis_dlock_tests, temp_list::redis_temp_list_tests},
};

struct ChildGuard(Child);

impl Drop for ChildGuard {
fn drop(&mut self) {
match self.0.kill() {
Err(e) => println!("Could not kill child process: {}", e),
Ok(_) => println!("Successfully killed child process"),
}
}
}

#[derive(
PartialEq, Debug, serde::Serialize, serde::Deserialize, FromRedisValue, ToRedisArgs,
)]
Expand Down Expand Up @@ -87,31 +77,14 @@ mod tests {
#[rstest]
#[tokio::test]
async fn test_redis_working(#[allow(unused_variables)] logging: ()) -> RResult<(), AnyErr> {
// Don't want to install redis in ci, just run this test locally:
if in_ci() {
return Ok(());
}

// Make sure redis is running on port 6379, starting it otherwise. (this means you must have redis installed)
let mut _redis_guard: Option<ChildGuard> = None;
if is_free(6379) {
_redis_guard = Some(ChildGuard(
Command::new("redis-server")
.arg("--port")
.arg("6379")
.spawn()
.unwrap(),
));
// sleep for 50ms to give redis time to start:
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
let rs = RedisStandalone::new().await?;

let work_r = Redis::new("redis://localhost:6379", uuid::Uuid::new_v4().to_string())?;
let work_r = rs.instance()?;
let mut work_conn = work_r.conn();

// Also create a fake version on a random port, this will be used to check failure cases.
let fail_r = Redis::new(
"redis://localhost:6372",
"redis://FAKKEEEE:6372",
format!("test_{}", uuid::Uuid::new_v4()),
)?;
let mut fail_conn = fail_r.conn();
Expand Down
64 changes: 64 additions & 0 deletions rust/bitbazaar/redis/standalone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::time::Instant;

use crate::log::record_exception;
use crate::prelude::*;

use super::Redis;

/// Standalone redis client, using a unique free port.
/// Useful for testing.
pub struct RedisStandalone {
/// The port the redis server is running on.
pub port: u16,
child: std::process::Child,
}

impl RedisStandalone {
/// Start a standalone redis server process on an unused port.
/// This process will be killed on drop.
pub async fn new() -> RResult<Self, AnyErr> {
let port = portpicker::pick_unused_port()
.ok_or_else(|| anyerr!("Could not find a free port to run RedisStandalone on."))?;

let child = std::process::Command::new("redis-server")
.arg("--port")
.arg(port.to_string())
.spawn()
.change_context(AnyErr)?;

// Wait for redis to come up, raising if waited for 10 seconds.
let mut up = false;
let elapsed = Instant::now();
while !up && elapsed.elapsed() < std::time::Duration::from_secs(10) {
up = Redis::new(
format!("redis://localhost:{}", port),
uuid::Uuid::new_v4().to_string(),
)?
.conn()
.ping()
.await
}
if up {
Ok(Self { child, port })
} else {
Err(anyerr!("RedisStandalone process not ready in 10 seconds."))
}
}

/// Get a new [`Redis`] instance connected to this standalone redis server.
pub fn instance(&self) -> RResult<Redis, AnyErr> {
Redis::new(
format!("redis://localhost:{}", self.port),
uuid::Uuid::new_v4().to_string(),
)
}
}

impl Drop for RedisStandalone {
fn drop(&mut self) {
match self.child.kill() {
Ok(_) => {}
Err(e) => record_exception("Could not kill child process.", format!("{:?}", e)),
}
}
}
1 change: 1 addition & 0 deletions zetch.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ DEBUG = { default = true, coerce = "bool" }
IN_DOCKER = { default = false, coerce = "bool" }
OTLP_OO_ENDPOINT = { default = "http://localhost:5080/api/default/" }
OTLP_OO_AUTH = { default = "Basic ZGV2QGRldi5jb206cGFzcw==" } # Not security issue! This is just base64('dev@dev.com:pass')
REDIS_URL = { default = "redis://localhost:6379" }

[context.cli]
ROOT_DIR = { commands = ["pwd"] }

0 comments on commit dd41dc7

Please sign in to comment.