Skip to content

Commit

Permalink
fix running tests in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
dejankos committed Sep 17, 2020
1 parent b505149 commit 5158379
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ $ cargo build
```
### Run IT
```bash
$ cargo test -- --test-threads=1
$ cargo test
```

## Performance
Expand Down Expand Up @@ -117,7 +117,7 @@ Payload: 1kb
![Delete tps](docs/delete_tps.png?raw=true "Delete tps")

### TODO
- [ ] fix running test in parallel (TODO per test db root path)
- [X] fix running test in parallel (TODO per test db root path)
- [ ] db iterator for on startup init
- [ ] test compaction
- [ ] code coverage
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ServiceConfig {

#[derive(Serialize, Deserialize, Debug)]
pub struct RocksDbConfig {
path: String,
pub path: String,
max_open_files: i32,
fsync: bool,
bytes_per_sync: u64,
Expand Down
34 changes: 26 additions & 8 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

use actix_web::dev::ServiceResponse;
use actix_web::http::StatusCode;
use actix_web::rt as actix_rt;
use actix_web::{test, web, App, Error};

use crate::config::{DbConfig, RocksDbConfig};
use crate::conversion::bytes_to_str;
use actix_web::rt as actix_rt;

use super::*;

static COUNTER: AtomicUsize = AtomicUsize::new(1);

fn next() -> usize {
COUNTER.fetch_add(1, Ordering::Relaxed)
}

fn safe_test_name() -> String {
if let Some(name) = thread::current().name() {
name.into()
} else {
next().to_string()
}
}

impl DbConfig {
pub fn new_with_defaults() -> Self {
DbConfig(RocksDbConfig::default())
pub fn new_per_test_defaults() -> Self {
let mut cfg = RocksDbConfig::default();
cfg.path = format!("{}/{}", cfg.path, safe_test_name());

DbConfig(cfg)
}
}

#[actix_rt::test]
async fn should_open_and_close_db() -> Result<(), Error> {
std::env::set_var("RUST_BACKTRACE", "full");

let db_manager = DbManager::new(DbConfig::new_with_defaults())?;
let db_manager = DbManager::new(DbConfig::new_per_test_defaults())?;
let mut app = test::init_service(
App::new()
.app_data(web::Data::new(db_manager))
Expand Down Expand Up @@ -64,7 +82,7 @@ async fn should_open_and_close_db() -> Result<(), Error> {
async fn should_add_and_delete_record() -> Result<(), Error> {
std::env::set_var("RUST_BACKTRACE", "full");

let db_manager = DbManager::new(DbConfig::new_with_defaults())?;
let db_manager = DbManager::new(DbConfig::new_per_test_defaults())?;
let mut app = test::init_service(
App::new()
.app_data(web::Data::new(db_manager))
Expand Down Expand Up @@ -125,7 +143,7 @@ async fn should_add_and_delete_record() -> Result<(), Error> {
async fn should_expire_record() -> Result<(), Error> {
std::env::set_var("RUST_BACKTRACE", "full");

let db_manager = DbManager::new(DbConfig::new_with_defaults())?;
let db_manager = DbManager::new(DbConfig::new_per_test_defaults())?;
let mut app = test::init_service(
App::new()
.app_data(web::Data::new(db_manager))
Expand Down Expand Up @@ -189,7 +207,7 @@ async fn should_expire_record() -> Result<(), Error> {
async fn should_check_service_status() -> Result<(), Error> {
std::env::set_var("RUST_BACKTRACE", "full");

let db_manager = DbManager::new(DbConfig::new_with_defaults())?;
let db_manager = DbManager::new(DbConfig::new_per_test_defaults())?;
let mut app = test::init_service(
App::new()
.app_data(web::Data::new(db_manager))
Expand All @@ -212,7 +230,7 @@ async fn should_check_service_status() -> Result<(), Error> {
async fn should_handle_404() -> Result<(), Error> {
std::env::set_var("RUST_BACKTRACE", "full");

let db_manager = DbManager::new(DbConfig::new_with_defaults())?;
let db_manager = DbManager::new(DbConfig::new_per_test_defaults())?;
let mut app = test::init_service(
App::new()
.wrap(ErrorHandlers::new().handler(http::StatusCode::NOT_FOUND, not_found))
Expand Down

0 comments on commit 5158379

Please sign in to comment.