Skip to content

Commit

Permalink
Implement Mock and Noop for SpanContext. (#11)
Browse files Browse the repository at this point in the history
Building on #10, this PR adds the actual implementations
for the SpanContext for Noop and Mock libraries.
  • Loading branch information
daschl authored Mar 14, 2018
1 parent 27adb2e commit 1f5ab04
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion opentracing-api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests {
fn get_items() {
let mut items = HashMap::new();
items.insert("key".into(), "value".into());
let context = TestContext { items: items };
let context = TestContext { items };
let items: Vec<(&String, &String)> = context.baggage_items().collect();
assert_eq!(items, [(&"key".into(), &"value".into())])
}
Expand Down
5 changes: 4 additions & 1 deletion opentracing-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ documentation = "https://docs.rs/opentracing-mock"
keywords = ["opentracing", "tracing"]

[badges]
maintenance = { status = "actively-developed" }
maintenance = { status = "actively-developed" }

[dependencies]
opentracing-api = { path = "../opentracing-api" }
50 changes: 50 additions & 0 deletions opentracing-mock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
//! This crate provides a mocking implementation of the tracing
//! API, especially useful for testing.
#![doc(html_root_url = "https://docs.rs/opentracing-mock/0.1.0")]

extern crate opentracing_api;

use opentracing_api::SpanContext;
use std::collections::HashMap;
use std::collections::hash_map::Iter as HashMapIter;

pub struct MockSpanContext {
baggage: HashMap<String, String>,
}

impl MockSpanContext {
/// Create a new `MockSpanContext` with the given baggage.
pub fn new(baggage: HashMap<String, String>) -> Self {
MockSpanContext { baggage }
}
}

impl<'a> SpanContext<'a> for MockSpanContext {
type Iter = HashMapIter<'a, String, String>;

fn baggage_items(&'a self) -> Self::Iter {
self.baggage.iter()
}
}

#[cfg(test)]
mod tests {

use super::MockSpanContext;
use opentracing_api::SpanContext;
use std::collections::HashMap;

#[test]
fn test_map_baggage() {
let mut items = HashMap::new();
items.insert("key".into(), "value".into());

let ctx = MockSpanContext::new(items);
let mut iter = ctx.baggage_items();
assert_eq!(
Some((&String::from("key"), &String::from("value"))),
iter.next()
);
assert_eq!(None, iter.next());
}

}
5 changes: 4 additions & 1 deletion opentracing-noop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ documentation = "https://docs.rs/opentracing-noop"
keywords = ["opentracing", "tracing"]

[badges]
maintenance = { status = "actively-developed" }
maintenance = { status = "actively-developed" }

[dependencies]
opentracing-api = { path = "../opentracing-api" }
39 changes: 39 additions & 0 deletions opentracing-noop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
//! This crate provides a simple and cheap "noop" implementation of the
//! tracing API and all of its sub-components.
#![doc(html_root_url = "https://docs.rs/opentracing-noop/0.1.0")]

extern crate opentracing_api;

use opentracing_api::SpanContext;
use std::iter::{empty, Empty};

/// The `NoopSpanContext` just returns an empty iterator on
/// baggage items and all of its associated content.
pub struct NoopSpanContext {}

impl Default for NoopSpanContext {
fn default() -> Self {
NoopSpanContext {}
}
}

impl<'a> SpanContext<'a> for NoopSpanContext {
type Iter = Empty<(&'a String, &'a String)>;

fn baggage_items(&'a self) -> Self::Iter {
empty()
}
}

#[cfg(test)]
mod tests {

use super::NoopSpanContext;
use opentracing_api::SpanContext;

#[test]
fn test_empty_baggage() {
let ctx = NoopSpanContext::default();
assert_eq!(None, ctx.baggage_items().next());
}

}

0 comments on commit 1f5ab04

Please sign in to comment.