diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-08 17:26:03 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-08 17:26:03 -0800 |
commit | a25a6e92793537277b796ca9751f77ac4b082d64 (patch) | |
tree | 1e0b35fc8b35e425073ac7eb5bc0e4b864767a9a /rust/src | |
parent | 22f017df8a564a6cc9ff9893b2886f852946cbc5 (diff) | |
download | fatcat-a25a6e92793537277b796ca9751f77ac4b082d64.tar.gz fatcat-a25a6e92793537277b796ca9751f77ac4b082d64.zip |
integrate pg_tmp into tests
Diffstat (limited to 'rust/src')
-rw-r--r-- | rust/src/lib.rs | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs index b3e6c813..0d9ef813 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -24,6 +24,7 @@ extern crate regex; extern crate lazy_static; extern crate macaroon; extern crate sha1; +extern crate rand; pub mod api_entity_crud; pub mod api_helpers; @@ -100,7 +101,9 @@ use diesel::r2d2::ConnectionManager; use dotenv::dotenv; use iron::middleware::AfterMiddleware; use iron::{Request, Response}; -use std::env; +use std::{env, thread, time}; +use std::process::Command; +use rand::Rng; #[cfg(feature = "postgres")] embed_migrations!("../migrations/"); @@ -156,22 +159,35 @@ pub fn server() -> Result<api_server::Server> { }) } +/// Generates a server for testing. Calls an external bash script to generate a random postgres +/// database, which will be unique to this process but common across threads and connections. The +/// database will automagically get cleaned up (deleted) after 60 seconds. +/// Currently, start times are staggered by up to 200ms to prevent internal postgres concurrency +/// errors; if this fails run the tests serially (one at a time), which is slower but more robust. +/// CI should run tests serially. pub fn test_server() -> Result<api_server::Server> { dotenv().ok(); - let database_url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set"); + // sleep a bit so we don't have thundering herd collisions, resuliting in + // "pg_extension_name_index" or "pg_proc_proname_args_nsp_index" or "pg_type_typname_nsp_index" + // duplicate key violations. + thread::sleep(time::Duration::from_millis(rand::thread_rng().gen_range(0, 200))); + let pg_tmp = Command::new("./tests/pg_tmp.sh") + .output() + .expect("run ./tests/pg_tmp.sh to get temporary postgres DB"); + let database_url = String::from_utf8_lossy(&pg_tmp.stdout).to_string(); env::set_var("DATABASE_URL", database_url); let mut server = server()?; server.auth_confectionary = AuthConfectionary::new_dummy(); let conn = server.db_pool.get().expect("db_pool error"); - // run migrations; revert latest (dummy data); re-run latest - diesel_migrations::run_pending_migrations(&conn).unwrap(); - diesel_migrations::revert_latest_migration(&conn).unwrap(); + // run migrations; this is a fresh/bare database diesel_migrations::run_pending_migrations(&conn).unwrap(); Ok(server) } +// TODO: move this to bin/fatcatd + /// HTTP header middleware header! { (XClacksOverhead, "X-Clacks-Overhead") => [String] } |