Skip to content

Commit 0c58faa

Browse files
authored
Merge pull request #483 from AmbireTech/fix-randomly-failing-campaign-test
Fix randomly failing campaign test
2 parents b56c9ba + 93a05df commit 0c58faa

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

primitives/src/campaign.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ mod campaign_id {
2929
use thiserror::Error;
3030
use uuid::Uuid;
3131

32-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
3333
/// an Id of 16 bytes, (de)serialized as a `0x` prefixed hex
3434
///
3535
/// In this implementation of the `CampaignId` the value is generated from a `Uuid::new_v4().to_simple()`
3636
pub struct CampaignId([u8; 16]);
3737

38+
impl fmt::Debug for CampaignId {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
write!(f, "CampaignId({})", self)
41+
}
42+
}
43+
3844
impl CampaignId {
3945
/// Generates randomly a `CampaignId` using `Uuid::new_v4().to_simple()`
4046
pub fn new() -> Self {

sentry/src/db.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ pub mod redis_pool {
553553
}
554554
}
555555

556-
/// Flushing (`FLUSDB`) is synchronous by default in Redis
556+
/// Flushing (`FLUSHDB`) is synchronous by default in Redis
557557
pub async fn flush_db(connection: &mut MultiplexedConnection) -> Result<String, Error> {
558558
redis::cmd("FLUSHDB")
559559
.query_async::<_, String>(connection)
@@ -619,15 +619,17 @@ pub mod redis_pool {
619619
async fn recycle(&self, database: &mut Database) -> RecycleResult<Self::Error> {
620620
// always make a new connection because of know redis crate issue
621621
// see https://github.com/mitsuhiko/redis-rs/issues/325
622-
let connection = redis_connection(format!("{}{}", Self::URL, database.index))
622+
let mut connection = redis_connection(format!("{}{}", Self::URL, database.index))
623623
.await
624624
.expect("Should connect");
625+
// first flush the database
626+
// this avoids the problem of flushing after the DB is picked up again by the Pool
627+
let flush_result = Self::flush_db(&mut connection).await;
625628
// make the database available
626629
database.available = true;
627630
database.connection = connection;
628-
Self::flush_db(&mut database.connection)
629-
.await
630-
.expect("Should flush");
631+
632+
flush_result.expect("Should have flushed the redis DB successfully");
631633

632634
Ok(())
633635
}

sentry/src/routes/campaign.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,8 @@ mod test {
10261026
channel_context.context.id(),
10271027
for_address,
10281028
Deposit {
1029-
// a deposit 4 times larger than the Campaign Budget
1030-
// I.e. 4 TOKENS
1029+
// a deposit 4 times larger than the first Campaign.budget = 500
1030+
// I.e. 2 000 TOKENS
10311031
total: UnifiedNum::from(200_000_000_000)
10321032
.to_precision(channel_context.token.precision.get()),
10331033
still_on_create2: BigNum::from(0),
@@ -1089,6 +1089,10 @@ mod test {
10891089
};
10901090

10911091
// modify campaign
1092+
// Deposit = 2 000
1093+
// old Campaign.budget = 500
1094+
// new Campaign.budget = 1 000
1095+
// Deposit left = 1 000
10921096
let modified = {
10931097
let new_budget = UnifiedNum::from(1000 * multiplier);
10941098
let modify = ModifyCampaign {
@@ -1100,7 +1104,9 @@ mod test {
11001104
ad_units: None,
11011105
targeting_rules: None,
11021106
};
1103-
// prepare for Campaign modification
1107+
1108+
// prepare for Campaign modification.
1109+
// does not alter the deposit amount
11041110
add_deposit_call(&channel_context, campaign_context.context.creator);
11051111

11061112
let modified_campaign = modify_campaign(
@@ -1121,6 +1127,9 @@ mod test {
11211127
};
11221128

11231129
// we have 1000 left from our deposit, so we are using half of it
1130+
// remaining Deposit = 1 000
1131+
// new Campaign.budget = 500
1132+
// Deposit left = 500
11241133
let _second_campaign = {
11251134
// erases the CampaignId for the CreateCampaign request
11261135
let mut create_second =
@@ -1146,8 +1155,8 @@ mod test {
11461155
};
11471156

11481157
// No budget left for new campaigns
1149-
// remaining: 500
1150-
// new campaign budget: 600
1158+
// remaining Deposit = 500
1159+
// new Campaign.budget = 600
11511160
{
11521161
// erases the CampaignId for the CreateCampaign request
11531162
let mut create = CreateCampaign::from_campaign_erased(DUMMY_CAMPAIGN.clone(), None);
@@ -1201,8 +1210,8 @@ mod test {
12011210
};
12021211

12031212
// Just enough budget to create this Campaign
1204-
// remaining: 600
1205-
// new campaign budget: 600
1213+
// remaining Deposit = 600
1214+
// new Campaign.budget = 600
12061215
{
12071216
// erases the CampaignId for the CreateCampaign request
12081217
let mut create = CreateCampaign::from_campaign_erased(DUMMY_CAMPAIGN.clone(), None);
@@ -1224,9 +1233,9 @@ mod test {
12241233
}
12251234

12261235
// Modify a campaign without enough budget
1227-
// remaining: 0
1228-
// new campaign budget: 1100
1229-
// current campaign budget: 900
1236+
// remaining Deposit = 0
1237+
// new Campaign.budget = 1100
1238+
// current Campaign.budget = 900
12301239
{
12311240
let new_budget = UnifiedNum::from(110_000_000_000);
12321241
let modify = ModifyCampaign {

0 commit comments

Comments
 (0)