memoryd: recall's Honcho part, every source at once with its own limit
Co-Authored-By: ornith-1.5-35b-a3b <agent@wntrmute.dev>
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
//! A fake Honcho for tests, on 127.0.0.1 and a port of its own: it answers each request from the
|
||||
//! responses recorded from rift (`tests/fixtures/honcho/`), by method and path (a batch of
|
||||
//! messages or conclusions answered with as many as it asked for, up to the two recorded), and
|
||||
//! records every request, body included. It can be set to answer every request with a status instead, or to
|
||||
//! answer nothing. Stopped, and its held connections released, when dropped; every wait has a
|
||||
//! deadline. Do not edit.
|
||||
//! records every request, body included. It can be set to answer every request with a status
|
||||
//! instead, to answer nothing, or to answer as a workspace that does not exist, for every
|
||||
//! workspace or for one. Stopped, and its held connections released, when dropped; every wait has
|
||||
//! a deadline. Do not edit.
|
||||
|
||||
#![allow(dead_code)] // each test file uses a different part of this module
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::path::PathBuf;
|
||||
@@ -46,10 +47,15 @@ pub enum Mode {
|
||||
Status(u16),
|
||||
/// Read the request, then answer nothing until the fake is dropped (at most 30 s).
|
||||
Hang,
|
||||
/// As a workspace that does not exist: a search or a conclusion query answers 200 with no
|
||||
/// hits (fixtures 34, 35), chat 404 (36).
|
||||
Missing,
|
||||
}
|
||||
|
||||
struct Shared {
|
||||
mode: Mutex<Mode>,
|
||||
/// A mode for one workspace's requests, by its name, instead of `mode`.
|
||||
workspaces: Mutex<HashMap<String, Mode>>,
|
||||
requests: Mutex<Vec<Request>>,
|
||||
/// What was made, by method and path and id: a second create answers 200, a second delete
|
||||
/// 404.
|
||||
@@ -85,6 +91,7 @@ impl FakeHoncho {
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let shared = Arc::new(Shared {
|
||||
mode: Mutex::new(Mode::Fixtures),
|
||||
workspaces: Mutex::new(HashMap::new()),
|
||||
requests: Mutex::new(Vec::new()),
|
||||
made: Mutex::new(HashSet::new()),
|
||||
stop: AtomicBool::new(false),
|
||||
@@ -112,6 +119,15 @@ impl FakeHoncho {
|
||||
*self.shared.mode.lock().unwrap() = mode;
|
||||
}
|
||||
|
||||
/// Answer `workspace`'s requests in `mode`, whatever `set` says for the others.
|
||||
pub fn set_workspace(&self, workspace: &str, mode: Mode) {
|
||||
self.shared
|
||||
.workspaces
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(workspace.to_string(), mode);
|
||||
}
|
||||
|
||||
/// Every request read so far, in order.
|
||||
pub fn requests(&self) -> Vec<Request> {
|
||||
self.shared.requests.lock().unwrap().clone()
|
||||
@@ -222,6 +238,21 @@ fn answer(request: &Request, made: &Mutex<HashSet<String>>) -> (u16, String) {
|
||||
request,
|
||||
"conclusions",
|
||||
),
|
||||
// A search answers the three recorded messages about tea (fixture 30), whatever it asked.
|
||||
("POST", ["v3", "workspaces", _, "search"]) => fixture("30-workspace-search-limit.json"),
|
||||
// A conclusion query answers the recorded one about the owner (32) when it asks about
|
||||
// `kyle`, else the one about an agent (33).
|
||||
("POST", ["v3", "workspaces", _, "conclusions", "query"]) => {
|
||||
let about = request
|
||||
.body
|
||||
.as_ref()
|
||||
.and_then(|b| b["filters"]["observed_id"].as_str().map(str::to_string));
|
||||
if about.as_deref() == Some("kyle") {
|
||||
fixture("32-conclusions-query-owner.json")
|
||||
} else {
|
||||
fixture("33-conclusions-query-agent.json")
|
||||
}
|
||||
}
|
||||
("DELETE", ["v3", "workspaces", _, "conclusions", _]) if again => {
|
||||
fixture("16-conclusion-delete-again.json")
|
||||
}
|
||||
@@ -251,9 +282,20 @@ fn serve(mut stream: TcpStream, shared: &Shared) {
|
||||
return;
|
||||
};
|
||||
shared.requests.lock().unwrap().push(request.clone());
|
||||
let mode = *shared.mode.lock().unwrap();
|
||||
let workspace = request.path.split('/').nth(3).unwrap_or("").to_string();
|
||||
let mode = match shared.workspaces.lock().unwrap().get(&workspace) {
|
||||
Some(mode) => *mode,
|
||||
None => *shared.mode.lock().unwrap(),
|
||||
};
|
||||
let (status, body) = match mode {
|
||||
Mode::Fixtures => answer(&request, &shared.made),
|
||||
Mode::Missing if request.path.ends_with("/search") => {
|
||||
fixture("34-workspace-search-missing-workspace.json")
|
||||
}
|
||||
Mode::Missing if request.path.ends_with("/conclusions/query") => {
|
||||
fixture("35-conclusions-query-missing-workspace.json")
|
||||
}
|
||||
Mode::Missing => fixture("36-workspace-chat-missing-workspace.json"),
|
||||
Mode::Status(422) => {
|
||||
let input = request.body.clone().unwrap_or(Value::Null);
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user