From 600ae662a57f1c2343eeba858141de30c699fd10 Mon Sep 17 00:00:00 2001 From: Andrew Yoon Date: Thu, 25 Feb 2021 14:43:39 -0500 Subject: [PATCH] Support plain `cargo test` and disable unused doctests for speed Since DB tests execute diesel migrations automatically, concurrent execution causes flaky failures from simultaneous migrations. This can be worked around using `cargo test --workspace -- --test-threads=1`, which is what the CI config does, but this is not intuitive for newcomer developers and unnecessarily slows down the test suite for the majority of tests which are safe to run concurrently. This fixes this issue by integrating with the small test crate `serial_test` and using it to explicitly mark DB tests to run sequentially while allowing all other tests to run in parallel. Additionally, this greatly improves the speed of `cargo test` by disabling doc-tests in all crates, since these are aren't currently used and cargo's doc-test pass, even when no doc-tests exist, has significant overhead. On my machine, this change significantly improves test suite times by about 85%, making it much more practical to develop with tools like `cargo watch` auto-running tests. --- .drone.yml | 2 -- Cargo.lock | 26 +++++++++++++++++++ Cargo.toml | 3 +++ crates/api/Cargo.toml | 1 + crates/apub/Cargo.toml | 1 + crates/db_queries/Cargo.toml | 4 +++ .../src/aggregates/comment_aggregates.rs | 2 ++ .../src/aggregates/community_aggregates.rs | 2 ++ .../src/aggregates/post_aggregates.rs | 2 ++ .../src/aggregates/site_aggregates.rs | 2 ++ .../src/aggregates/user_aggregates.rs | 2 ++ crates/db_queries/src/lib.rs | 3 +++ crates/db_queries/src/source/activity.rs | 2 ++ crates/db_queries/src/source/comment.rs | 2 ++ crates/db_queries/src/source/community.rs | 2 ++ crates/db_queries/src/source/moderator.rs | 2 ++ .../src/source/password_reset_request.rs | 2 ++ crates/db_queries/src/source/post.rs | 2 ++ .../db_queries/src/source/private_message.rs | 2 ++ crates/db_queries/src/source/user.rs | 2 ++ crates/db_queries/src/source/user_mention.rs | 2 ++ crates/db_schema/Cargo.toml | 3 +++ crates/db_views/Cargo.toml | 6 +++++ crates/db_views/src/comment_view.rs | 2 ++ crates/db_views/src/lib.rs | 3 +++ crates/db_views/src/post_view.rs | 2 ++ crates/db_views_actor/Cargo.toml | 3 +++ crates/db_views_moderator/Cargo.toml | 3 +++ crates/routes/Cargo.toml | 3 +++ crates/structs/Cargo.toml | 1 + crates/utils/Cargo.toml | 1 + crates/websocket/Cargo.toml | 1 + 32 files changed, 94 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 807d6c991..b6fa86db5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -29,7 +29,6 @@ steps: environment: LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy RUST_BACKTRACE: 1 - RUST_TEST_THREADS: 1 commands: - sudo apt-get update - sudo apt-get -y install --no-install-recommends espeak postgresql-client @@ -107,7 +106,6 @@ steps: environment: LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy RUST_BACKTRACE: 1 - RUST_TEST_THREADS: 1 commands: - apt-get update - apt-get -y install --no-install-recommends espeak postgresql-client libssl-dev pkg-config libpq-dev diff --git a/Cargo.lock b/Cargo.lock index 2edcca8d7..cdf7f679c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "activitystreams" version = "0.7.0-alpha.10" @@ -1818,6 +1820,7 @@ dependencies = [ "regex", "serde 1.0.123", "serde_json", + "serial_test", "sha2", "strum", "strum_macros", @@ -1845,6 +1848,7 @@ dependencies = [ "lemmy_db_schema", "log", "serde 1.0.123", + "serial_test", "url", ] @@ -3080,6 +3084,28 @@ dependencies = [ "serde 1.0.123", ] +[[package]] +name = "serial_test" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0bccbcf40c8938196944a3da0e133e031a33f4d6b72db3bda3cc556e361905d" +dependencies = [ + "lazy_static", + "parking_lot", + "serial_test_derive", +] + +[[package]] +name = "serial_test_derive" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2acd6defeddb41eb60bb468f8825d0cfd0c2a76bc03bfd235b6a1dc4f6a1ad5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha-1" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index edec85f60..ab7d90198 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,9 @@ name = "lemmy_server" version = "0.0.1" edition = "2018" +[lib] +doctest = false + [profile.dev] debug = 0 diff --git a/crates/api/Cargo.toml b/crates/api/Cargo.toml index e3226eb85..299121a6e 100644 --- a/crates/api/Cargo.toml +++ b/crates/api/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [lib] name = "lemmy_api" path = "src/lib.rs" +doctest = false [dependencies] lemmy_apub = { path = "../apub" } diff --git a/crates/apub/Cargo.toml b/crates/apub/Cargo.toml index f2ad96838..9dd9bc82f 100644 --- a/crates/apub/Cargo.toml +++ b/crates/apub/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [lib] name = "lemmy_apub" path = "src/lib.rs" +doctest = false [dependencies] lemmy_utils = { path = "../utils" } diff --git a/crates/db_queries/Cargo.toml b/crates/db_queries/Cargo.toml index c396b39a3..0e489e228 100644 --- a/crates/db_queries/Cargo.toml +++ b/crates/db_queries/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [lib] name = "lemmy_db_queries" path = "src/lib.rs" +doctest = false [dependencies] lemmy_utils = { path = "../utils" } @@ -23,3 +24,6 @@ url = { version = "2.2.0", features = ["serde"] } lazy_static = "1.4.0" regex = "1.4.3" bcrypt = "0.9.0" + +[dev-dependencies] +serial_test = "0.5.1" \ No newline at end of file diff --git a/crates/db_queries/src/aggregates/comment_aggregates.rs b/crates/db_queries/src/aggregates/comment_aggregates.rs index a4db471b6..0826ae0d4 100644 --- a/crates/db_queries/src/aggregates/comment_aggregates.rs +++ b/crates/db_queries/src/aggregates/comment_aggregates.rs @@ -37,8 +37,10 @@ mod tests { post::{Post, PostForm}, user::{UserForm, User_}, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/aggregates/community_aggregates.rs b/crates/db_queries/src/aggregates/community_aggregates.rs index f5cd577e4..ee60da9b2 100644 --- a/crates/db_queries/src/aggregates/community_aggregates.rs +++ b/crates/db_queries/src/aggregates/community_aggregates.rs @@ -41,8 +41,10 @@ mod tests { post::{Post, PostForm}, user::{UserForm, User_}, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/aggregates/post_aggregates.rs b/crates/db_queries/src/aggregates/post_aggregates.rs index fa8b69255..404257a7b 100644 --- a/crates/db_queries/src/aggregates/post_aggregates.rs +++ b/crates/db_queries/src/aggregates/post_aggregates.rs @@ -41,8 +41,10 @@ mod tests { post::{Post, PostForm, PostLike, PostLikeForm}, user::{UserForm, User_}, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/aggregates/site_aggregates.rs b/crates/db_queries/src/aggregates/site_aggregates.rs index 46db2f0cd..81afed5c4 100644 --- a/crates/db_queries/src/aggregates/site_aggregates.rs +++ b/crates/db_queries/src/aggregates/site_aggregates.rs @@ -39,8 +39,10 @@ mod tests { site::{Site, SiteForm}, user::{UserForm, User_}, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/aggregates/user_aggregates.rs b/crates/db_queries/src/aggregates/user_aggregates.rs index fcda1d462..bdd16932a 100644 --- a/crates/db_queries/src/aggregates/user_aggregates.rs +++ b/crates/db_queries/src/aggregates/user_aggregates.rs @@ -37,8 +37,10 @@ mod tests { post::{Post, PostForm, PostLike, PostLikeForm}, user::{UserForm, User_}, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/lib.rs b/crates/db_queries/src/lib.rs index bf0de3366..5667b4262 100644 --- a/crates/db_queries/src/lib.rs +++ b/crates/db_queries/src/lib.rs @@ -9,6 +9,9 @@ extern crate lazy_static; #[macro_use] extern crate diesel_migrations; +#[cfg(test)] +extern crate serial_test; + use diesel::{result::Error, *}; use lemmy_db_schema::Url; use regex::Regex; diff --git a/crates/db_queries/src/source/activity.rs b/crates/db_queries/src/source/activity.rs index 59e1754aa..cf946d67d 100644 --- a/crates/db_queries/src/source/activity.rs +++ b/crates/db_queries/src/source/activity.rs @@ -133,8 +133,10 @@ mod tests { user::{UserForm, User_}, }; use serde_json::Value; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/comment.rs b/crates/db_queries/src/source/comment.rs index 709a8aa39..918ae4977 100644 --- a/crates/db_queries/src/source/comment.rs +++ b/crates/db_queries/src/source/comment.rs @@ -212,8 +212,10 @@ mod tests { post::*, user::{UserForm, User_}, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/community.rs b/crates/db_queries/src/source/community.rs index 08421bd5d..6a19d2211 100644 --- a/crates/db_queries/src/source/community.rs +++ b/crates/db_queries/src/source/community.rs @@ -340,8 +340,10 @@ mod tests { SortType, }; use lemmy_db_schema::source::{community::*, user::*}; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/moderator.rs b/crates/db_queries/src/source/moderator.rs index 9b6e58edf..469ada089 100644 --- a/crates/db_queries/src/source/moderator.rs +++ b/crates/db_queries/src/source/moderator.rs @@ -199,9 +199,11 @@ impl Crud for ModAdd { mod tests { use crate::{establish_unpooled_connection, Crud, ListingType, SortType}; use lemmy_db_schema::source::{comment::*, community::*, moderator::*, post::*, user::*}; + use serial_test::serial; // use Crud; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/password_reset_request.rs b/crates/db_queries/src/source/password_reset_request.rs index f58d2c014..600037d8a 100644 --- a/crates/db_queries/src/source/password_reset_request.rs +++ b/crates/db_queries/src/source/password_reset_request.rs @@ -80,8 +80,10 @@ mod tests { SortType, }; use lemmy_db_schema::source::{password_reset_request::PasswordResetRequest, user::*}; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/post.rs b/crates/db_queries/src/source/post.rs index 1c19e53d0..f6b1342ef 100644 --- a/crates/db_queries/src/source/post.rs +++ b/crates/db_queries/src/source/post.rs @@ -231,8 +231,10 @@ mod tests { community::{Community, CommunityForm}, user::*, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/private_message.rs b/crates/db_queries/src/source/private_message.rs index 8cace9384..f1908bc14 100644 --- a/crates/db_queries/src/source/private_message.rs +++ b/crates/db_queries/src/source/private_message.rs @@ -147,8 +147,10 @@ mod tests { SortType, }; use lemmy_db_schema::source::{private_message::*, user::*}; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/user.rs b/crates/db_queries/src/source/user.rs index 5f3fa6cbb..20b187b46 100644 --- a/crates/db_queries/src/source/user.rs +++ b/crates/db_queries/src/source/user.rs @@ -379,8 +379,10 @@ impl User for User_ { #[cfg(test)] mod tests { use crate::{establish_unpooled_connection, source::user::*, ListingType, SortType}; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_queries/src/source/user_mention.rs b/crates/db_queries/src/source/user_mention.rs index b0c97572f..5efc95bf6 100644 --- a/crates/db_queries/src/source/user_mention.rs +++ b/crates/db_queries/src/source/user_mention.rs @@ -81,8 +81,10 @@ mod tests { user::*, user_mention::*, }; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_schema/Cargo.toml b/crates/db_schema/Cargo.toml index a175f80e4..b637c0222 100644 --- a/crates/db_schema/Cargo.toml +++ b/crates/db_schema/Cargo.toml @@ -3,6 +3,9 @@ name = "lemmy_db_schema" version = "0.1.0" edition = "2018" +[lib] +doctest = false + [dependencies] diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] } chrono = { version = "0.4.19", features = ["serde"] } diff --git a/crates/db_views/Cargo.toml b/crates/db_views/Cargo.toml index 175e6eae6..e44f414a6 100644 --- a/crates/db_views/Cargo.toml +++ b/crates/db_views/Cargo.toml @@ -3,6 +3,9 @@ name = "lemmy_db_views" version = "0.1.0" edition = "2018" +[lib] +doctest = false + [dependencies] lemmy_db_queries = { path = "../db_queries" } lemmy_db_schema = { path = "../db_schema" } @@ -10,3 +13,6 @@ diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json serde = { version = "1.0.123", features = ["derive"] } log = "0.4.14" url = "2.2.0" + +[dev-dependencies] +serial_test = "0.5.1" \ No newline at end of file diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 4f9a6a5f7..56b22a7f0 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -443,8 +443,10 @@ mod tests { SortType, }; use lemmy_db_schema::source::{comment::*, community::*, post::*, user::*}; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_views/src/lib.rs b/crates/db_views/src/lib.rs index b46ec5a46..a9369ed24 100644 --- a/crates/db_views/src/lib.rs +++ b/crates/db_views/src/lib.rs @@ -1,3 +1,6 @@ +#[cfg(test)] +extern crate serial_test; + pub mod comment_report_view; pub mod comment_view; pub mod post_report_view; diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index ed1ef4ce1..e7b488275 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -434,8 +434,10 @@ mod tests { SortType, }; use lemmy_db_schema::source::{community::*, post::*, user::*}; + use serial_test::serial; #[test] + #[serial] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/crates/db_views_actor/Cargo.toml b/crates/db_views_actor/Cargo.toml index 8e86d4c0a..e237941e1 100644 --- a/crates/db_views_actor/Cargo.toml +++ b/crates/db_views_actor/Cargo.toml @@ -3,6 +3,9 @@ name = "lemmy_db_views_actor" version = "0.1.0" edition = "2018" +[lib] +doctest = false + [dependencies] lemmy_db_queries = { path = "../db_queries" } lemmy_db_schema = { path = "../db_schema" } diff --git a/crates/db_views_moderator/Cargo.toml b/crates/db_views_moderator/Cargo.toml index aef6abdc3..9df01522d 100644 --- a/crates/db_views_moderator/Cargo.toml +++ b/crates/db_views_moderator/Cargo.toml @@ -3,6 +3,9 @@ name = "lemmy_db_views_moderator" version = "0.1.0" edition = "2018" +[lib] +doctest = false + [dependencies] lemmy_db_queries = { path = "../db_queries" } lemmy_db_schema = { path = "../db_schema" } diff --git a/crates/routes/Cargo.toml b/crates/routes/Cargo.toml index 0c5eac665..b71ffca70 100644 --- a/crates/routes/Cargo.toml +++ b/crates/routes/Cargo.toml @@ -3,6 +3,9 @@ name = "lemmy_routes" version = "0.1.0" edition = "2018" +[lib] +doctest = false + [dependencies] lemmy_utils = { path = "../utils" } lemmy_websocket = { path = "../websocket" } diff --git a/crates/structs/Cargo.toml b/crates/structs/Cargo.toml index f895a8c95..de4f38cbf 100644 --- a/crates/structs/Cargo.toml +++ b/crates/structs/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [lib] name = "lemmy_structs" path = "src/lib.rs" +doctest = false [dependencies] lemmy_db_queries = { path = "../db_queries" } diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml index c10b7c2c7..3308669a4 100644 --- a/crates/utils/Cargo.toml +++ b/crates/utils/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [lib] name = "lemmy_utils" path = "src/lib.rs" +doctest = false [dependencies] regex = "1.4.3" diff --git a/crates/websocket/Cargo.toml b/crates/websocket/Cargo.toml index 1cb71ea1d..3116d71e7 100644 --- a/crates/websocket/Cargo.toml +++ b/crates/websocket/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [lib] name = "lemmy_websocket" path = "src/lib.rs" +doctest = false [dependencies] lemmy_utils = { path = "../utils" }