Skip to content

Commit

Permalink
test(general): Added basic snapshot tests for normalize and event par…
Browse files Browse the repository at this point in the history
…sing (#154)
  • Loading branch information
mitsuhiko authored Jan 22, 2019
1 parent 3a2ceaf commit 82987e3
Show file tree
Hide file tree
Showing 33 changed files with 7,044 additions and 209 deletions.
478 changes: 286 additions & 192 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion common/src/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a> UpstreamDescriptor<'a> {

/// Returns a URL relative to the upstream.
pub fn get_url(&self, path: &str) -> Url {
format!("{}{}", self, path.trim_left_matches(&['/'][..]))
format!("{}{}", self, path.trim_start_matches(&['/'][..]))
.parse()
.unwrap()
}
Expand Down
2 changes: 2 additions & 0 deletions general/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ serde_urlencoded = "0.5.4"
smallvec = { version = "0.6.7", features = ["serde"] }
url = "1.7.2"
uuid = { version = "0.7.1", features = ["v4", "serde"] }
insta = "0.4.1"

[dev-dependencies]
difference = "2.0.0"

[features]
bench = []

11 changes: 7 additions & 4 deletions general/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
macro_rules! benchmark {
($sdk:ident) => {
mod $sdk {
extern crate test;
use std::collections::BTreeSet;

use semaphore_general::processor::process_value;
use semaphore_general::protocol::Event;
use semaphore_general::store::{StoreConfig, StoreNormalizeProcessor};
use semaphore_general::store::{StoreConfig, StoreProcessor};
use semaphore_general::types::Annotated;

fn load_json() -> String {
let path = concat!("fixtures/payloads/", stringify!($sdk), ".json");
let path = concat!("tests/fixtures/payloads/", stringify!($sdk), ".json");
std::fs::read_to_string(path).expect("failed to load json")
}

Expand Down Expand Up @@ -50,15 +51,17 @@ macro_rules! benchmark {
valid_platforms: platforms,
max_secs_in_future: Some(3600),
max_secs_in_past: Some(2_592_000),
enable_trimming: Some(true),
max_stacktrace_frames: Some(50),
};

let json = load_json();
let mut processor = StoreNormalizeProcessor::new(config, None);
let mut processor = StoreProcessor::new(config, None);
let event = Annotated::<Event>::from_json(&json).expect("failed to deserialize");

b.iter(|| {
let mut event = test::black_box(event.clone());
process_value(&mut event, &mut processor, Default::default());
process_value(&mut event, &mut processor, &Default::default());
event
});
}
Expand Down
16 changes: 5 additions & 11 deletions general/src/types/annotated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ impl From<bool> for ValueAction {
#[derive(Clone, PartialEq)]
pub struct Annotated<T>(pub Option<T>, pub Meta);

/// An utility to serialize annotated objects with payload.
pub struct SerializableAnnotated<'a, T>(pub &'a Annotated<T>);

impl<T: fmt::Debug> fmt::Debug for Annotated<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Expand Down Expand Up @@ -418,21 +421,12 @@ impl<T> Default for Annotated<T> {
}
}

impl<T: Serialize> Serialize for Annotated<T> {
impl<'a, T: ToValue> Serialize for SerializableAnnotated<'a, T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Serialize::serialize(&self.value(), serializer)
}
}

impl<'de, T: Deserialize<'de>> Deserialize<'de> for Annotated<T> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Annotated(
Deserialize::deserialize(deserializer)?,
Default::default(),
))
self.0.serialize_with_meta(serializer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion general/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod meta;
mod traits;
mod value;

pub use self::annotated::{Annotated, MetaMap, MetaTree, ValueAction};
pub use self::annotated::{Annotated, MetaMap, MetaTree, SerializableAnnotated, ValueAction};
pub use self::impls::SerializePayload;
pub use self::meta::{Error, ErrorKind, Meta, Range, Remark, RemarkType};
pub use self::traits::{Empty, FromValue, SkipSerialization, ToValue};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 82987e3

Please sign in to comment.