use std::sync::{Arc, Mutex, MutexGuard};
use super::super::workers::{MOST, Workers};
use super::{CROWD, Route, route};
use crate::pipe::fixture::{Client, answered};
use crate::pipe::{Connect, Pipe};
use crate::wire::codec::Message;
use crate::wire::frame::{Frame, Kind};
use crate::wire::protocol::{KvCall, KvEntry, METHODS, method};
use crate::{Options, Store};
fn get_at_once(client: &mut Client, handle: u64, streams: std::ops::Range<u32>) -> Vec<u8> {
let get = KvCall { handle, key: "k".to_owned(), ..KvCall::default() };
let mut bytes = Vec::new();
for stream in streams {
Frame { method: method::KV_GET, ..Frame::new(Kind::Request, stream, get.encode()) }
.ending()
.encode_into(&mut bytes);
}
client.pipe.send(&bytes)
}
fn hold_every_worker<'gates>(store: &Store, gates: &'gates [Arc<Mutex<()>>]) -> Vec<MutexGuard<'gates, ()>> {
let workers = Workers::of(store).unwrap();
let closed = gates.iter().map(|gate| gate.lock().unwrap()).collect();
for gate in gates {
let gate = Arc::clone(gate);
workers.run(Box::new(move || drop(gate.lock())));
}
closed
}
fn gates() -> Vec<Arc<Mutex<()>>> {
(0..MOST).map(|_| Arc::new(Mutex::new(()))).collect()
}
#[test]
fn a_read_goes_to_the_workers_while_they_have_reads_of_its_connection() {
let dir = tempfile::tempdir().unwrap();
let store = Store::open(dir.path(), Options { background: false, ..Options::default() }).unwrap();
let mut client = Client::over(Pipe::connect(&store, Connect::default()).unwrap());
client.hello(1).unwrap();
let handle = client.bucket("sessions");
let crowd = CROWD as u32;
let alone = get_at_once(&mut client, handle, 1000..1001);
assert!(!alone.is_empty(), "a read alone is answered before its write returns");
client.reader.push(&alone);
answered::<KvEntry>(&client.next_on(1000)).unwrap();
let gates = gates();
let closed = hold_every_worker(&store, &gates);
assert!(get_at_once(&mut client, handle, 2000..2000 + crowd).is_empty(), "a crowd goes to the workers");
assert!(get_at_once(&mut client, handle, 3000..3001).is_empty(), "and a read that comes meanwhile goes with it");
drop(closed);
for stream in (2000..2000 + crowd).chain(3000..3001) {
answered::<KvEntry>(&client.next_on(stream)).unwrap();
}
assert_eq!(client.pipe.streams(), 0, "every stream ended once");
assert!(!get_at_once(&mut client, handle, 4000..4001).is_empty(), "with none away, a read is answered here again");
drop(client);
store.close().unwrap();
}
#[test]
fn every_method_runs_as_the_shape_its_schema_line_says() {
for &(name, called, shape) in METHODS {
let runs = match route(called) {
Route::Inline | Route::Submit | Route::Worker => "call",
Route::Handover => "handover",
#[cfg(feature = "jobs")]
Route::Exchange => "exchange",
#[cfg(feature = "jobs")]
Route::Watch => "download",
#[cfg(feature = "sql")]
Route::Download => "download",
#[cfg(feature = "sql")]
Route::Transaction => "exchange",
#[cfg(feature = "blobs")]
Route::Read => "download",
#[cfg(feature = "blobs")]
Route::Upload => "exchange",
#[cfg(feature = "records")]
Route::Tail => "download",
#[cfg(feature = "records")]
Route::Lines => "exchange",
#[cfg(feature = "metrics")]
Route::Parts => "download",
#[cfg(feature = "metrics")]
Route::Instruments => "exchange",
#[cfg(feature = "config")]
Route::Follow => "download",
#[cfg(feature = "backup")]
Route::Backup => "download",
#[cfg(feature = "locks")]
Route::Hold => "call",
#[cfg(feature = "channels")]
Route::Subscribe => "download",
};
assert_eq!(runs, shape, "{name}");
}
}