Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests around new RetryOpts features #52

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 87 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ convert_case = "0.6.0"

tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] }
serial_test = "3.1.1"
75 changes: 66 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use async_trait::async_trait;
use middleware::Chain;
use rand::{Rng, RngCore};
use serde::{Deserialize, Serialize};
use serde::{
de::{self, Deserializer, Visitor},
Deserialize, Serialize, Serializer,
};
use serde_json::Value as JsonValue;
use sha2::{Digest, Sha256};
use std::future::Future;
Expand Down Expand Up @@ -96,7 +99,7 @@ impl EnqueueOpts {
}
}

fn create_job(&self, class: String, args: impl serde::Serialize) -> Result<Job> {
pub fn create_job(&self, class: String, args: impl serde::Serialize) -> Result<Job> {
let args = serde_json::to_value(args)?;

// Ensure args are always wrapped in an array.
Expand Down Expand Up @@ -233,7 +236,7 @@ where
}

#[allow(clippy::wrong_self_convention)]
fn into_opts(&self) -> EnqueueOpts {
pub fn into_opts(&self) -> EnqueueOpts {
self.into()
}

Expand Down Expand Up @@ -403,16 +406,70 @@ impl WorkerRef {
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
#[derive(Clone, Debug, PartialEq)]
pub enum RetryOpts {
#[serde(rename = "true")]
Yes,
#[serde(rename = "false")]
Never,
Max(usize),
}

impl Serialize for RetryOpts {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
RetryOpts::Yes => serializer.serialize_bool(true),
RetryOpts::Never => serializer.serialize_bool(false),
RetryOpts::Max(value) => serializer.serialize_u64(value as u64),
}
}
}

impl<'de> Deserialize<'de> for RetryOpts {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct RetryOptsVisitor;

impl<'de> Visitor<'de> for RetryOptsVisitor {
type Value = RetryOpts;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a boolean, null, or a positive integer")
}

fn visit_bool<E>(self, value: bool) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
if value {
Ok(RetryOpts::Yes)
} else {
Ok(RetryOpts::Never)
}
}

fn visit_none<E>(self) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
Ok(RetryOpts::Never)
}

fn visit_u64<E>(self, value: u64) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
Ok(RetryOpts::Max(value as usize))
}
}

deserializer.deserialize_any(RetryOptsVisitor)
}
}

impl From<bool> for RetryOpts {
fn from(value: bool) -> Self {
match value {
Expand Down Expand Up @@ -463,8 +520,8 @@ pub struct Job {

#[derive(Debug)]
pub struct UnitOfWork {
queue: String,
job: Job,
pub queue: String,
pub job: Job,
}

impl UnitOfWork {
Expand Down
2 changes: 1 addition & 1 deletion src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Processor {
self
}

async fn fetch(&mut self) -> Result<Option<UnitOfWork>> {
pub async fn fetch(&mut self) -> Result<Option<UnitOfWork>> {
let response: Option<(String, String)> = self
.redis
.get()
Expand Down
Loading
Loading