Skip to content

Instantly share code, notes, and snippets.

@bittrance
Created April 29, 2019 22:31
Show Gist options
  • Save bittrance/0a75ff8dfb6702ebf1e4c5482b8b1d41 to your computer and use it in GitHub Desktop.
Save bittrance/0a75ff8dfb6702ebf1e4c5482b8b1d41 to your computer and use it in GitHub Desktop.
use crate::read_events::{HandlerFn, Message, read_events};
use circbuf::CircBuf;
use evalexpr::Value;
use failure::Error;
use maplit::hashmap;
use quiche;
use rmp_serde::Serializer;
use rand::{Rng, thread_rng};
use serde::Serialize;
use speculate::speculate;
use std::rc::Rc;
fn a_message() -> Vec<u8> {
let mut buf = Vec::new();
let mut serializer = Serializer::new(&mut buf);
Message {
resource_id: "ze-id".to_owned(),
properties: hashmap!("foo".to_owned() => Value::from("bar"))
}.serialize(&mut serializer).unwrap();
buf
}
fn an_scid() -> [u8; 10] {
let mut scid = [0u8; 10];
thread_rng().fill(&mut scid[..]);
scid
}
fn quic_message(config: &mut quiche::Config) -> Result<Vec<u8>, Error> {
let mut conn = quiche::connect(None, &an_scid(), config)?;
conn.stream_send(1, &a_message()[..], false)?;
let mut buf = Vec::new();
conn.send(buf.as_mut_slice());
Ok(buf)
}
speculate! {
describe "read_events" {
errtype(Error)
before {
let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
let a_quic = quic_message(&mut config);
let mut buf = CircBuf::with_capacity(16384000).unwrap();
let mut conn = quiche::accept(&an_scid(), None, &mut config)?;
let received = Rc::new(Vec::new());
let mut r2 = received.clone();
let mut handler: HandlerFn = Box::new(move |message| {
Rc::get_mut(&mut r2).unwrap().push(message);
Ok(())
});
}
it "reads an event" {
read_events(&mut conn, &mut buf, &mut handler)?;
assert_eq!(
Some(&"ze-id".to_owned()),
received.get(0).map(|m| &m.resource_id)
);
}
}
}
@bittrance
Copy link
Author

Errors with:

warning: unused import: `Rng`
 --> barsa-server/src/test/read_events.rs:8:12
  |
8 | use rand::{Rng, thread_rng};
  |            ^^^
  |
  = note: #[warn(unused_imports)] on by default

error[E0599]: no method named `fill` found for type `rand::ThreadRng` in the current scope
  --> barsa-server/src/test/read_events.rs:25:16
   |
25 |   thread_rng().fill(&mut scid[..]);
   |                ^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: Could not compile `barsa-server`.
warning: build failed, waiting for other jobs to finish...
error: build failed
cargo +stable test  319,79s user 8,18s system 178% cpu 3:04,06 total

Cargo.toml:

[package]
name = "barsa-server"
version = "0.1.0"
authors = ["..."]
edition = "2018"

[dependencies]
circbuf = "*"
evalexpr = { version = "*", features = ["serde_support"] }
failure = "*"
log = { version = "0.4.5", features = ["std"] }
quiche = { git = "https://github.com/cloudflare/quiche" }
rand = "*"
rmp-serde = "*"
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
criterion = "*"
maplit = "*"
speculate = { path = "../speculate" }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment