#![allow(dead_code)]
use std::error::Error;
use std::sync::{Arc, RwLock};
use optimizely::{client::UserContext, event_api::EventDispatcher, Client, Conversion, Decision};
pub const ACCOUNT_ID: &str = "21537940595";
pub const SDK_KEY: &str = "UCtKi3qiMkQpso1GTmBFY";
pub const FILE_PATH: &str = "../datafiles/sandbox.json";
pub const REVISION: u32 = 21;
#[derive(Default)]
pub struct Counter(Arc<RwLock<usize>>);
impl Counter {
fn increment(&self) {
if let Ok(mut lock_guard) = self.0.write() {
*lock_guard += 1
}
}
pub fn value(&self) -> usize {
self.0.read().map(|value| *value).unwrap_or_default()
}
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
#[derive(Default)]
pub struct EventStore {
conversion_counter: Counter,
decision_counter: Counter,
}
impl EventDispatcher for EventStore {
fn send_conversion_event(&self, _user_context: &UserContext, _conversion: Conversion) {
self.conversion_counter.increment();
}
fn send_decision_event(&self, _user_context: &UserContext, _decision: Decision) {
self.decision_counter.increment();
}
}
pub struct TestContext {
pub client: Client,
pub conversion_counter: Counter,
pub decision_counter: Counter,
}
pub fn setup() -> Result<TestContext, Box<dyn Error>> {
let event_store = EventStore::default();
let conversion_counter = event_store.conversion_counter.clone();
let decision_counter = event_store.decision_counter.clone();
let client = Client::from_local_datafile(FILE_PATH)?
.with_event_dispatcher(|_datafile| event_store)
.initialize();
Ok(TestContext {
client,
conversion_counter,
decision_counter,
})
}