Skip to content

Commit

Permalink
test: add quotes_app test (#476)
Browse files Browse the repository at this point in the history
* test: add quotes_app test

* fix: use file_serial attribute to run tests serially
  • Loading branch information
thevaibhav-dixit authored Nov 24, 2023
1 parent 1495d88 commit ff953d4
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ uuid = { workspace = true }

[dev-dependencies]
anyhow = "1.0.70"
serial_test = { version = "*" , features = ["file_locks"] }
2 changes: 2 additions & 0 deletions ledger/tests/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serial_test::file_serial;

use stablesats_ledger::*;

Expand Down Expand Up @@ -134,6 +135,7 @@ async fn adjust_exchange_position() -> anyhow::Result<()> {
}

#[tokio::test]
#[file_serial]
async fn buy_and_sell_quotes() -> anyhow::Result<()> {
let pool = init_pool().await?;

Expand Down
1 change: 1 addition & 0 deletions quotes-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ tonic-build = { version = "0.8", features = ["prost"] }
anyhow = "1.0.70"
serde = "1.0.158"
serde_json = "1.0.93"
serial_test = { version = "*" , features = [ "file_locks"] }
113 changes: 113 additions & 0 deletions quotes-server/tests/quote_app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use chrono::Duration;
use rust_decimal_macros::dec;
use serial_test::file_serial;

use quotes_server::error::QuotesAppError;
use quotes_server::{
app::*, cache::OrderBookCacheError, ExchangePriceCacheError, QuotesExchangePriceCacheConfig,
QuotesFeeCalculatorConfig,
};

use shared::{payload::*, pubsub::*, time::*};
fn load_fixture() -> OrderBookPayload {
OrderBookPayload {
bids: [(
PriceRaw::from(dec!(0.001)),
VolumeInCentsRaw::from(dec!(100_000_000)),
)]
.into_iter()
.collect(),
asks: [(
PriceRaw::from(dec!(0.01)),
VolumeInCentsRaw::from(dec!(100_000_000)),
)]
.into_iter()
.collect(),
timestamp: TimeStamp::from(10000000),
exchange: "okex".into(),
}
}

#[tokio::test]
#[file_serial]
async fn quotes_app() -> anyhow::Result<()> {
let (tick_send, tick_recv) =
memory::channel(chrono::Duration::from_std(std::time::Duration::from_secs(2)).unwrap());
let publisher = tick_send.clone();
let mut subscriber = tick_recv.resubscribe();

let (_, recv) = futures::channel::mpsc::unbounded();
let ex_cfgs = ExchangeWeights {
okex: Some(dec!(1.0)),
bitfinex: None,
};

let base_fee_rate = dec!(0.001);
let immediate_fee_rate = dec!(0.01);
let delayed_fee_rate = dec!(0.1);

let pg_host = std::env::var("PG_HOST").unwrap_or_else(|_| "localhost".into());
let pg_con = format!("postgres://user:password@{}:5432/pg", pg_host);
let pool = sqlx::PgPool::connect(&pg_con).await?;

let app = QuotesApp::run(
pool,
recv,
QuotesServerHealthCheckConfig::default(),
QuotesFeeCalculatorConfig {
base_fee_rate,
immediate_fee_rate,
delayed_fee_rate,
},
tick_recv,
QuotesExchangePriceCacheConfig::default(),
ex_cfgs,
QuotesConfig {
expiration_interval: Duration::seconds(2),
},
)
.await?;

let err = app
.quote_cents_from_sats_for_buy(dec!(100_000_000), true)
.await;
if let Err(QuotesAppError::ExchangePriceCacheError(ExchangePriceCacheError::OrderBookCache(
OrderBookCacheError::NoSnapshotAvailable,
))) = err
{
assert!(true)
} else {
assert!(false)
}

let mut payload = load_fixture();
tick_send
.publish(PriceStreamPayload::OkexBtcUsdSwapOrderBookPayload(
payload.clone(),
))
.await?;
subscriber.next().await;
tokio::time::sleep(std::time::Duration::from_millis(100)).await;

payload.timestamp = TimeStamp::now();
publisher
.publish(PriceStreamPayload::OkexBtcUsdSwapOrderBookPayload(payload))
.await?;
subscriber.next().await;
tokio::time::sleep(std::time::Duration::from_millis(100)).await;

let quote = app
.quote_cents_from_sats_for_buy(dec!(100_000_000), false)
.await;
assert!(quote.is_ok());
let accepted = app.accept_quote(quote.unwrap().id).await;
assert!(accepted.is_ok());

let quote = app
.quote_cents_from_sats_for_buy(dec!(100_000_000), true)
.await;
assert!(quote.is_ok());
assert!(quote.unwrap().is_accepted());

Ok(())
}

0 comments on commit ff953d4

Please sign in to comment.