Skip to content

Commit

Permalink
Add recording logger for further tests
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed May 12, 2024
1 parent c4c63e9 commit 60538f3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::constants::SDK_KEY_PROXY_PREFIX;
use crate::errors::{ClientError, ErrorKind};
use crate::model::enums::DataGovernance;
use crate::modes::PollingMode;
use crate::r#override::FlagOverrides;
use crate::r#override::{FlagOverrides, OptionalOverrides};
use crate::{Client, ConfigCache, OverrideBehavior, OverrideDataSource};
use std::borrow::Borrow;
use std::time::Duration;
Expand Down Expand Up @@ -255,7 +255,9 @@ impl ClientBuilder {
"SDK Key cannot be empty".to_owned(),
));
}
if !self.is_sdk_key_valid(self.sdk_key.as_str(), self.base_url.is_some()) {
if !self.overrides.is_local()
&& !self.is_sdk_key_valid(self.sdk_key.as_str(), self.base_url.is_some())
{
return Err(ClientError::new(
ErrorKind::InvalidSdkKey,
format!("SDK Key '{}' is invalid.", self.sdk_key),
Expand Down
2 changes: 1 addition & 1 deletion src/eval/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ fn eval_prerequisite_cond(
.iter()
.map(|k| format!("'{k}'"))
.collect::<Vec<String>>()
.join(" => ");
.join(" -> ");
return Fatal(output);
}

Expand Down
38 changes: 38 additions & 0 deletions tests/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![allow(dead_code)]

use crate::utils::RecordingLogger;
use configcat::OverrideBehavior::LocalOnly;
use configcat::{Client, FileDataSource};
use log::set_max_level;

mod utils;

fn init() {
set_max_level(log::LevelFilter::Info);
_ = log::set_logger(&RecordingLogger {});
}

#[tokio::test]
async fn prerequisite_circular_deps() {
init();

let tests = vec![
("key1", "'key1' -> 'key1'"),
("key2", "'key2' -> 'key3' -> 'key2'"),
("key4", "'key4' -> 'key3' -> 'key2' -> 'key3'"),
];

let client = Client::builder("local")
.overrides(
Box::new(FileDataSource::new("tests/data/test_circulardependency_v6.json").unwrap()),
LocalOnly,
)
.build()
.unwrap();

for test in tests {
_ = client.get_flag_details(test.0, None).await;
let logs = RecordingLogger::LOGS.take();
assert!(logs.contains(test.1));
}
}
8 changes: 4 additions & 4 deletions tests/override.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{construct_bool_json_payload, produce_mock_path, rand_sdk_key};
use crate::utils::{construct_bool_json_payload, produce_mock_path};
use configcat::OverrideBehavior::{LocalOnly, LocalOverRemote, RemoteOverLocal};
use configcat::Value::{Bool, Float, Int};
use configcat::{Client, FileDataSource, MapDataSource, Value};
Expand All @@ -8,7 +8,7 @@ mod utils;

#[tokio::test]
async fn file_simple() {
let client = Client::builder(rand_sdk_key().as_str())
let client = Client::builder("local")
.overrides(
Box::new(FileDataSource::new("tests/data/test_json_simple.json").unwrap()),
LocalOnly,
Expand All @@ -33,7 +33,7 @@ async fn file_simple() {

#[tokio::test]
async fn file_complex() {
let client = Client::builder(rand_sdk_key().as_str())
let client = Client::builder("local")
.overrides(
Box::new(FileDataSource::new("tests/data/test_json_complex.json").unwrap()),
LocalOnly,
Expand All @@ -58,7 +58,7 @@ async fn file_complex() {

#[tokio::test]
async fn map() {
let client = Client::builder(rand_sdk_key().as_str())
let client = Client::builder("local")
.overrides(
Box::new(MapDataSource::new(HashMap::from([
("enabledFeature".to_owned(), Bool(true)),
Expand Down
29 changes: 29 additions & 0 deletions tests/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use log::{Level, Log, Metadata, Record};
use rand::distributions::{Alphanumeric, DistString};
use std::cell::RefCell;

pub fn produce_mock_path() -> (String, String) {
let sdk_key = rand_sdk_key();
Expand Down Expand Up @@ -44,3 +45,31 @@ impl Log for PrintLog {

fn flush(&self) {}
}

pub struct RecordingLogger {}

impl RecordingLogger {
thread_local!(pub static LOGS: RefCell<String> = RefCell::new(String::default()));
}

impl Log for RecordingLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= log::max_level() && metadata.target().contains("configcat")
}

fn log(&self, record: &Record) {
if !self.enabled(record.metadata()) {
return;
}
let level = match record.level() {
Level::Error => "[ERROR]",
Level::Warn => "[WARN]",
Level::Info => "[INFO]",
Level::Debug => "[DEBUG]",
Level::Trace => "[TRACE]",
};
Self::LOGS.with_borrow_mut(|l| l.push_str(format!("{level} {}", record.args()).as_str()));
}

fn flush(&self) {}
}

0 comments on commit 60538f3

Please sign in to comment.