test_: flaky router tests fixed

Closes https://github.com/status-im/status-go/issues/5601
This commit is contained in:
Sale Djenic 2024-07-29 12:54:59 +02:00 committed by saledjenic
parent 65334d42ed
commit 18885b3663
3 changed files with 48 additions and 18 deletions

View File

@ -189,6 +189,14 @@ func newSuggestedRoutesV2(
allRoutes := node.buildAllRoutesV2()
allRoutes = filterRoutesV2(allRoutes, amountIn, fromLockedAmount)
if len(allRoutes) > 0 {
sort.Slice(allRoutes, func(i, j int) bool {
iRoute := getRoutePriority(allRoutes[i])
jRoute := getRoutePriority(allRoutes[j])
return iRoute <= jRoute
})
}
return suggestedRoutes, allRoutes
}
@ -1003,6 +1011,12 @@ func (r *Router) resolveCandidates(ctx context.Context, input *RouteInputParams,
})
}
sort.Slice(candidates, func(i, j int) bool {
iChain := getChainPriority(candidates[i].FromChain.ChainID)
jChain := getChainPriority(candidates[j].FromChain.ChainID)
return iChain <= jChain
})
group.Wait()
return candidates, processorErrors, nil
}
@ -1079,6 +1093,27 @@ func removeBestRouteFromAllRouters(allRoutes [][]*PathV2, best []*PathV2) [][]*P
return nil
}
func getChainPriority(chainID uint64) int {
switch chainID {
case walletCommon.EthereumMainnet, walletCommon.EthereumSepolia:
return 1
case walletCommon.OptimismMainnet, walletCommon.OptimismSepolia:
return 2
case walletCommon.ArbitrumMainnet, walletCommon.ArbitrumSepolia:
return 3
default:
return 0
}
}
func getRoutePriority(route []*PathV2) int {
priority := 0
for _, path := range route {
priority += getChainPriority(path.FromChain.ChainID)
}
return priority
}
func (r *Router) resolveRoutes(ctx context.Context, input *RouteInputParams, candidates []*PathV2, balanceMap map[string]*big.Int) (suggestedRoutes *SuggestedRoutesV2, err error) {
var prices map[string]float64
if input.testsMode {
@ -1096,6 +1131,16 @@ func (r *Router) resolveRoutes(ctx context.Context, input *RouteInputParams, can
var allRoutes [][]*PathV2
suggestedRoutes, allRoutes = newSuggestedRoutesV2(input.Uuid, input.AmountIn.ToInt(), candidates, input.FromLockedAmount, tokenPrice, nativeTokenPrice)
defer func() {
if suggestedRoutes.Best != nil && len(suggestedRoutes.Best) > 0 {
sort.Slice(suggestedRoutes.Best, func(i, j int) bool {
iChain := getChainPriority(suggestedRoutes.Best[i].FromChain.ChainID)
jChain := getChainPriority(suggestedRoutes.Best[j].FromChain.ChainID)
return iChain <= jChain
})
}
}()
for len(allRoutes) > 0 {
best := findBestV2(allRoutes, tokenPrice, nativeTokenPrice)
@ -1106,22 +1151,16 @@ func (r *Router) resolveRoutes(ctx context.Context, input *RouteInputParams, can
if (input.SendType == Transfer ||
input.SendType == Bridge) &&
len(allRoutes) > 1 {
allRoutes = removeBestRouteFromAllRouters(allRoutes, best)
continue
} else {
sort.Slice(best, func(i, j int) bool {
return best[i].AmountInLocked
})
suggestedRoutes.Best = best
return suggestedRoutes, errors.CreateErrorResponseFromError(err)
}
}
if len(best) > 0 {
sort.Slice(best, func(i, j int) bool {
return best[i].AmountInLocked
})
// At this point we have to do the final check and update the amountIn (subtracting fees) if complete balance is going to be sent for native token (ETH)
for _, path := range best {
if path.subtractFees && path.FromToken.IsNative() {

View File

@ -210,9 +210,6 @@ func TestNoBalanceForTheBestRouteRouterV2(t *testing.T) {
// Test blocking endpoints
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.flaky {
t.Skip("Flaky test")
}
routes, err := router.SuggestedRoutesV2(context.Background(), tt.input)
@ -234,9 +231,6 @@ func TestNoBalanceForTheBestRouteRouterV2(t *testing.T) {
// Test async endpoints
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.flaky {
t.Skip("Flaky test")
}
router.SuggestedRoutesV2Async(tt.input)

View File

@ -2594,7 +2594,6 @@ type noBalanceTestParams struct {
expectedCandidates []*PathV2
expectedBest []*PathV2
expectedError *errors.ErrorResponse
flaky bool
}
func getNoBalanceTestParamsList() []noBalanceTestParams {
@ -2727,10 +2726,9 @@ func getNoBalanceTestParamsList() []noBalanceTestParams {
approvalL1Fee: testApprovalL1Fee,
},
},
flaky: true,
expectedError: &errors.ErrorResponse{
Code: ErrNotEnoughTokenBalance.Code,
Details: fmt.Sprintf(ErrNotEnoughTokenBalance.Details, pathprocessor.UsdcSymbol, walletCommon.EthereumMainnet),
Details: fmt.Sprintf(ErrNotEnoughTokenBalance.Details, pathprocessor.UsdcSymbol, walletCommon.ArbitrumMainnet),
},
expectedCandidates: []*PathV2{
{
@ -2788,10 +2786,9 @@ func getNoBalanceTestParamsList() []noBalanceTestParams {
approvalL1Fee: testApprovalL1Fee,
},
},
flaky: true,
expectedError: &errors.ErrorResponse{
Code: ErrNotEnoughNativeBalance.Code,
Details: fmt.Sprintf(ErrNotEnoughNativeBalance.Details, pathprocessor.EthSymbol, walletCommon.EthereumMainnet),
Details: fmt.Sprintf(ErrNotEnoughNativeBalance.Details, pathprocessor.EthSymbol, walletCommon.ArbitrumMainnet),
},
expectedCandidates: []*PathV2{
{