Add new package with methods to save and load compressor state (#63)
This commit is contained in:
@@ -13,6 +13,7 @@ postgres = "0.19.0"
|
||||
postgres-openssl = "0.5.0"
|
||||
rand = "0.8.0"
|
||||
synapse_compress_state = { path = "../" }
|
||||
auto_compressor = { path = "../auto_compressor/" }
|
||||
|
||||
[dependencies.state-map]
|
||||
git = "https://github.com/matrix-org/rust-matrix-state-map"
|
||||
@@ -69,7 +69,7 @@ pub fn add_contents_to_database(room_id: &str, state_group_map: &BTreeMap<i64, S
|
||||
client.batch_execute(&sql).unwrap();
|
||||
}
|
||||
|
||||
// Clears the contents of the testing database
|
||||
/// Clears the contents of the testing database
|
||||
pub fn empty_database() {
|
||||
// connect to the database
|
||||
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
@@ -80,9 +80,9 @@ pub fn empty_database() {
|
||||
|
||||
// delete all the contents from all three tables
|
||||
let sql = r"
|
||||
DELETE FROM state_groups;
|
||||
DELETE FROM state_group_edges;
|
||||
DELETE FROM state_groups_state;
|
||||
TRUNCATE state_groups;
|
||||
TRUNCATE state_group_edges;
|
||||
TRUNCATE state_groups_state;
|
||||
";
|
||||
|
||||
client.batch_execute(sql).unwrap();
|
||||
@@ -300,6 +300,24 @@ pub fn database_structure_matches_map(state_group_map: &BTreeMap<i64, StateGroup
|
||||
true
|
||||
}
|
||||
|
||||
/// Clears the compressor state from the database
|
||||
pub fn clear_compressor_state() {
|
||||
// connect to the database
|
||||
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
builder.set_verify(SslVerifyMode::NONE);
|
||||
let connector = MakeTlsConnector::new(builder.build());
|
||||
|
||||
let mut client = Client::connect(DB_URL, connector).unwrap();
|
||||
|
||||
// delete all the contents from the state compressor tables
|
||||
let sql = r"
|
||||
TRUNCATE state_compressor_state;
|
||||
TRUNCATE state_compressor_progress;
|
||||
";
|
||||
|
||||
client.batch_execute(sql).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn functions_are_self_consistent() {
|
||||
let mut initial: BTreeMap<i64, StateGroupEntry> = BTreeMap::new();
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
use auto_compressor::state_saving::{
|
||||
connect_to_database, create_tables_if_needed, read_room_compressor_state,
|
||||
write_room_compressor_state,
|
||||
};
|
||||
use compressor_integration_tests::{clear_compressor_state, DB_URL};
|
||||
use serial_test::serial;
|
||||
use synapse_compress_state::Level;
|
||||
|
||||
#[test]
|
||||
#[serial(db)]
|
||||
fn write_then_read_state_gives_correct_results() {
|
||||
let mut client = connect_to_database(DB_URL).unwrap();
|
||||
create_tables_if_needed(&mut client).unwrap();
|
||||
clear_compressor_state();
|
||||
|
||||
let room_id = "room1";
|
||||
let written_info: Vec<Level> =
|
||||
vec![Level::restore(3, 1, Some(6)), Level::restore(3, 2, Some(6))];
|
||||
let written_num = 53;
|
||||
write_room_compressor_state(&mut client, room_id, &written_info, written_num).unwrap();
|
||||
|
||||
let (read_num, read_info) = read_room_compressor_state(&mut client, room_id)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(written_info, read_info);
|
||||
assert_eq!(written_num, read_num);
|
||||
}
|
||||
Reference in New Issue
Block a user