Skip to content

Commit 59e5b2c

Browse files
refactor: use educe instead of manually implementing traits for tests (#643)
1 parent 0df3c3f commit 59e5b2c

File tree

5 files changed

+89
-67
lines changed

5 files changed

+89
-67
lines changed

Cargo.lock

Lines changed: 38 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tap-agent/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ bon.workspace = true
5353
test-assets = { path = "../test-assets", optional = true }
5454
rand = { version = "0.8", optional = true }
5555
itertools = "0.14.0"
56+
educe = "0.6.0"
5657

5758
[dev-dependencies]
5859
# Release-please breaks with cyclical dependencies if dev-dependencies

crates/tap-agent/src/agent/sender_account.rs

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type Balance = U256;
102102

103103
/// Information for Ravs that are abstracted away from the SignedRav itself
104104
#[derive(Debug, Default, PartialEq, Eq)]
105+
#[cfg_attr(test, derive(Clone))]
105106
pub struct RavInformation {
106107
/// Allocation Id of a Rav
107108
pub allocation_id: Address,
@@ -140,6 +141,8 @@ impl From<&tap_graph::v2::SignedRav> for RavInformation {
140141
///
141142
/// It has different logic depending on the variant
142143
#[derive(Debug)]
144+
#[cfg_attr(test, derive(educe::Educe))]
145+
#[cfg_attr(test, educe(PartialEq, Eq, Clone))]
143146
pub enum ReceiptFees {
144147
/// Adds the receipt value to the fee tracker
145148
///
@@ -153,7 +156,11 @@ pub enum ReceiptFees {
153156
///
154157
/// If the rav response was successful, update the rav tracker
155158
/// If not, signalize the fee_tracker to apply proper backoff
156-
RavRequestResponse((UnaggregatedReceipts, anyhow::Result<Option<RavInformation>>)),
159+
RavRequestResponse(
160+
UnaggregatedReceipts,
161+
#[cfg_attr(test, educe(PartialEq(ignore), Clone(method(clone_rav_result))))]
162+
anyhow::Result<Option<RavInformation>>,
163+
),
157164
/// Ignores all logic and simply retry Allow/Deny and Rav Request logic
158165
///
159166
/// This is used inside a scheduler to trigger a Rav request in case the
@@ -162,8 +169,20 @@ pub enum ReceiptFees {
162169
Retry,
163170
}
164171

172+
#[cfg(test)]
173+
fn clone_rav_result(
174+
res: &anyhow::Result<Option<RavInformation>>,
175+
) -> anyhow::Result<Option<RavInformation>> {
176+
match res {
177+
Ok(val) => Ok(val.clone()),
178+
Err(_) => Err(anyhow::anyhow!("Some error")),
179+
}
180+
}
181+
165182
/// Enum containing all types of messages that a [SenderAccount] can receive
166183
#[derive(Debug)]
184+
#[cfg_attr(test, derive(educe::Educe))]
185+
#[cfg_attr(test, educe(PartialEq, Eq, Clone))]
167186
pub enum SenderAccountMessage {
168187
/// Updates the sender balance and
169188
UpdateBalanceAndLastRavs(Balance, RavMap),
@@ -185,13 +204,22 @@ pub enum SenderAccountMessage {
185204
UpdateRav(RavInformation),
186205
#[cfg(test)]
187206
/// Returns the sender fee tracker, used for tests
188-
GetSenderFeeTracker(ractor::RpcReplyPort<SenderFeeTracker>),
207+
GetSenderFeeTracker(
208+
#[educe(PartialEq(ignore), Clone(method(crate::test::actors::clone_rpc_reply)))]
209+
ractor::RpcReplyPort<SenderFeeTracker>,
210+
),
189211
#[cfg(test)]
190212
/// Returns the Deny status, used for tests
191-
GetDeny(ractor::RpcReplyPort<bool>),
213+
GetDeny(
214+
#[educe(PartialEq(ignore), Clone(method(crate::test::actors::clone_rpc_reply)))]
215+
ractor::RpcReplyPort<bool>,
216+
),
192217
#[cfg(test)]
193218
/// Returns if the scheduler is enabled, used for tests
194-
IsSchedulerEnabled(ractor::RpcReplyPort<bool>),
219+
IsSchedulerEnabled(
220+
#[educe(PartialEq(ignore), Clone(method(crate::test::actors::clone_rpc_reply)))]
221+
ractor::RpcReplyPort<bool>,
222+
),
195223
}
196224

197225
/// A SenderAccount manages the receipts accounting between the indexer and the sender across
@@ -1040,8 +1068,8 @@ impl Actor for SenderAccount {
10401068
.unwrap_or_default() as f64,
10411069
);
10421070
}
1043-
ReceiptFees::RavRequestResponse(rav_result) => {
1044-
state.finalize_rav_request(allocation_id, rav_result);
1071+
ReceiptFees::RavRequestResponse(fees, rav_result) => {
1072+
state.finalize_rav_request(allocation_id, (fees, rav_result));
10451073
}
10461074
ReceiptFees::UpdateValue(unaggregated_fees) => {
10471075
state.update_sender_fee(allocation_id, unaggregated_fees);
@@ -1415,50 +1443,6 @@ pub mod tests {
14151443
},
14161444
};
14171445

1418-
// we implement the PartialEq and Eq traits for SenderAccountMessage to be able to compare
1419-
impl Eq for SenderAccountMessage {}
1420-
1421-
impl PartialEq for SenderAccountMessage {
1422-
fn eq(&self, other: &Self) -> bool {
1423-
match (self, other) {
1424-
(Self::UpdateAllocationIds(l0), Self::UpdateAllocationIds(r0)) => l0 == r0,
1425-
(Self::UpdateReceiptFees(l0, l1), Self::UpdateReceiptFees(r0, r1)) => {
1426-
l0 == r0
1427-
&& match (l1, r1) {
1428-
(ReceiptFees::NewReceipt(l1, l2), ReceiptFees::NewReceipt(r1, r2)) => {
1429-
r1 == l1 && r2 == l2
1430-
}
1431-
(ReceiptFees::UpdateValue(l), ReceiptFees::UpdateValue(r)) => r == l,
1432-
(
1433-
ReceiptFees::RavRequestResponse(l),
1434-
ReceiptFees::RavRequestResponse(r),
1435-
) => match (l, r) {
1436-
((fee, Ok(rav)), (fee1, Ok(rav1))) => fee == fee1 && rav == rav1,
1437-
((fee, Err(error)), (fee1, Err(error1))) => {
1438-
fee == fee1 && error.to_string() == error1.to_string()
1439-
}
1440-
_ => false,
1441-
},
1442-
(ReceiptFees::Retry, ReceiptFees::Retry) => true,
1443-
_ => false,
1444-
}
1445-
}
1446-
(
1447-
Self::UpdateInvalidReceiptFees(l0, l1),
1448-
Self::UpdateInvalidReceiptFees(r0, r1),
1449-
) => l0 == r0 && l1 == r1,
1450-
(Self::NewAllocationId(l0), Self::NewAllocationId(r0)) => l0 == r0,
1451-
(a, b) => match (
1452-
core::mem::discriminant(self),
1453-
core::mem::discriminant(other),
1454-
) {
1455-
(a, b) if a != b => false,
1456-
_ => unimplemented!("PartialEq not implementated for {a:?} and {b:?}"),
1457-
},
1458-
}
1459-
}
1460-
}
1461-
14621446
/// Prefix shared between tests so we don't have conflicts in the global registry
14631447
const BUFFER_DURATION: Duration = Duration::from_millis(100);
14641448
const RETRY_DURATION: Duration = Duration::from_millis(1000);

crates/tap-agent/src/agent/sender_allocation.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,14 @@ where
406406
} else {
407407
Err(anyhow!("Unaggregated fee equals zero"))
408408
};
409-
let rav_response = (
410-
state.unaggregated_fees,
411-
rav_result.map(|res| res.map(Into::into)),
412-
);
413409
state
414410
.sender_account_ref
415411
.cast(SenderAccountMessage::UpdateReceiptFees(
416412
state.allocation_id,
417-
ReceiptFees::RavRequestResponse(rav_response),
413+
ReceiptFees::RavRequestResponse(
414+
state.unaggregated_fees,
415+
rav_result.map(|res| res.map(Into::into)),
416+
),
418417
))?;
419418
}
420419
#[cfg(any(test, feature = "test"))]
@@ -1672,7 +1671,7 @@ pub mod tests {
16721671

16731672
assert!(matches!(
16741673
message_receiver.recv().await.unwrap(),
1675-
SenderAccountMessage::UpdateReceiptFees(_, ReceiptFees::RavRequestResponse(_))
1674+
SenderAccountMessage::UpdateReceiptFees(_, ReceiptFees::RavRequestResponse(_, _))
16761675
));
16771676
}
16781677

@@ -2106,9 +2105,9 @@ pub mod tests {
21062105
match rav_response_message {
21072106
SenderAccountMessage::UpdateReceiptFees(
21082107
_,
2109-
ReceiptFees::RavRequestResponse(rav_response),
2108+
ReceiptFees::RavRequestResponse(_, rav_response),
21102109
) => {
2111-
assert!(rav_response.1.is_err());
2110+
assert!(rav_response.is_err());
21122111
}
21132112
v => panic!("Expecting RavRequestResponse as last message, found: {v:?}"),
21142113
}
@@ -2207,9 +2206,9 @@ pub mod tests {
22072206
match rav_response_message {
22082207
SenderAccountMessage::UpdateReceiptFees(
22092208
_,
2210-
ReceiptFees::RavRequestResponse(rav_response),
2209+
ReceiptFees::RavRequestResponse(_, rav_response),
22112210
) => {
2212-
assert!(rav_response.1.is_err());
2211+
assert!(rav_response.is_err());
22132212
}
22142213
v => panic!("Expecting RavRequestResponse as last message, found: {v:?}"),
22152214
}

crates/tap-agent/src/test.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@ pub mod actors {
703703
unaggregated_receipts::UnaggregatedReceipts,
704704
};
705705

706+
#[cfg(test)]
707+
pub fn clone_rpc_reply<T>(_: &ractor::RpcReplyPort<T>) -> ractor::RpcReplyPort<T> {
708+
ractor::concurrency::oneshot().0.into()
709+
}
710+
706711
pub struct DummyActor;
707712

708713
impl DummyActor {
@@ -917,14 +922,14 @@ pub mod actors {
917922
);
918923
sender_account.cast(SenderAccountMessage::UpdateReceiptFees(
919924
ALLOCATION_ID_0,
920-
ReceiptFees::RavRequestResponse((
925+
ReceiptFees::RavRequestResponse(
921926
UnaggregatedReceipts {
922927
value: *self.next_unaggregated_fees_value.borrow(),
923928
last_id: 0,
924929
counter: 0,
925930
},
926931
Ok(Some(signed_rav.into())),
927-
)),
932+
),
928933
))?;
929934
}
930935
}

0 commit comments

Comments
 (0)