Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRollen committed Jun 17, 2021
1 parent 8488da8 commit 630f997
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ edition = "2018"
[dependencies]
bytes = "1.0.1"
tokio = { version = "1.7.0", default-features = false, features = ["net"] }

[dev-dependencies]
tokio = { version = "1.7.0", default-features = false, features = ["macros", "rt"] }
46 changes: 33 additions & 13 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,49 @@ use crate::metric::Metric;
use std::io::Error;
use tokio::net::{ToSocketAddrs, UdpSocket};

pub struct Client<'a> {
pub struct Client {
socket: UdpSocket,
target: &'a str,
}

impl<'a> Client<'a> {
impl Client {
pub async fn new<T: ToSocketAddrs>(
host_address: &T,
target_address: &'a str,
) -> Result<Client<'a>, Error> {
Ok(Self {
socket: UdpSocket::bind(host_address).await?,
target: target_address,
})
host_address: T,
target_address: &str,
) -> Result<Client, Error> {
let socket = UdpSocket::bind(host_address).await?;
socket.connect(target_address).await?;
Ok(Self { socket })
}

pub async fn send<I, T>(&self, metric: Metric<'_, I, T>) -> Result<usize, Error>
pub async fn send<I, T>(&self, metric: Metric<'_, I, T>) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
{
self.socket
.send_to(metric.to_bytes().as_ref(), self.target)
let bytes = metric.into_bytes();
self.socket.send(&bytes).await?;
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;

#[tokio::test]
async fn test_client() {
let udp_receiver = UdpSocket::bind("127.0.0.1:8125").await.unwrap();
let v: &[&str] = &[];
let client = Client::new("127.0.0.1:1234", "127.0.0.1:8125")
.await
.unwrap();
client.send(Metric::increase("test", v)).await.unwrap();
udp_receiver.connect("127.0.0.1:1234").await.unwrap();
let mut bytes_received: usize = 0;
let mut buf = [0; 8];
while bytes_received < 8 {
bytes_received += udp_receiver.recv_from(&mut buf).await.unwrap().0;
}
assert_eq!(&buf, b"test:1|c");
}
}
30 changes: 21 additions & 9 deletions src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
}
}

pub(crate) fn to_bytes(self) -> Bytes {
pub(crate) fn into_bytes(self) -> Bytes {
let mut buf;
match self.frame_type {
Type::Count(count) => {
Expand Down Expand Up @@ -155,29 +155,41 @@ mod test {
use super::*;

#[test]
fn test_to_bytes() {
fn test_into_bytes() {
let v: &[&str] = &[];
assert_eq!(Metric::increase("test", v).to_bytes().as_ref(), b"test:1|c");
assert_eq!(
Metric::decrease("test", v).to_bytes().as_ref(),
Metric::increase("test", v).into_bytes().as_ref(),
b"test:1|c"
);
assert_eq!(
Metric::decrease("test", v).into_bytes().as_ref(),
b"test:-1|c"
);
assert_eq!(Metric::count(2, "test", v).to_bytes().as_ref(), b"test:2|c");
assert_eq!(
Metric::gauge("1.2", "test", v).to_bytes().as_ref(),
Metric::count(2, "test", v).into_bytes().as_ref(),
b"test:2|c"
);
assert_eq!(
Metric::gauge("1.2", "test", v).into_bytes().as_ref(),
b"test:1.2|g"
);
assert_eq!(
Metric::histogram("1.2", "test", v).to_bytes().as_ref(),
Metric::histogram("1.2", "test", v).into_bytes().as_ref(),
b"test:1.2|h"
);
assert_eq!(
Metric::distribution("1.2", "test", v).to_bytes().as_ref(),
Metric::distribution("1.2", "test", v).into_bytes().as_ref(),
b"test:1.2|d"
);
assert_eq!(
Metric::set("1.2", "test", v).to_bytes().as_ref(),
Metric::set("1.2", "test", v).into_bytes().as_ref(),
b"test:1.2|s"
);
assert_eq!(
Metric::increase("test", &["a:b", "c"])
.into_bytes()
.as_ref(),
b"test:1|c|#a:b,c"
);
}
}

0 comments on commit 630f997

Please sign in to comment.