@@ -603,6 +603,7 @@ func TestSimulateBackendBlockResolutionCoverage(t *testing.T) {
603603}
604604
605605func TestSimulationAPIRequestLimiter (t * testing.T ) {
606+ const maxConcurrentSimulationCalls = 2
606607
607608 type testEnv struct {
608609 simAPI * evmrpc.SimulationAPI
@@ -629,7 +630,7 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
629630 config := & evmrpc.SimulateConfig {
630631 GasCap : 1000000 ,
631632 EVMTimeout : 5 * time .Second ,
632- MaxConcurrentSimulationCalls : 2 , // Small limit to easily trigger rate limiting
633+ MaxConcurrentSimulationCalls : maxConcurrentSimulationCalls ,
633634 }
634635
635636 // Use the existing test app from the global setup
@@ -675,9 +676,21 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
675676 args : args ,
676677 }
677678 }
679+ assertRateLimited := func (t * testing.T , tEnv * testEnv , expected string , invoke func () error ) {
680+ t .Helper ()
681+ release , err := tEnv .simAPI .HoldSimulationSlotsForTest (t .Context (), maxConcurrentSimulationCalls )
682+ require .NoError (t , err )
683+ defer release ()
684+ require .EqualError (t , invoke (), expected )
685+ }
678686
679687 t .Run ("TestEthCallRateLimiting" , func (t * testing.T ) {
680688 tEnv := newTestEnv (t )
689+ assertRateLimited (t , tEnv , "eth_call rejected due to rate limit: server busy" , func () error {
690+ _ , err := tEnv .simAPI .Call (t .Context (), tEnv .args , nil , nil , nil )
691+ return err
692+ })
693+
681694 // Test eth_call rate limiting with concurrent requests
682695 numRequests := 10 // Much more than the limit of 2
683696 runBurst := func () []error {
@@ -718,8 +731,9 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
718731 }
719732 }
720733
721- // With only 2 concurrent slots and 10 requests, we should have rejections
722- require .Greater (t , rejectedCount , 0 , "Should have rejected requests due to rate limiting" )
734+ // Calls can serialize before another goroutine holds a slot. The saturated
735+ // call above covers rejection deterministically; this burst covers the
736+ // concurrent response outcomes.
723737 require .Greater (t , successCount , 0 , "Should have some successful requests" )
724738 require .Equal (t , numRequests , successCount + rejectedCount , "All requests should be accounted for" )
725739
@@ -890,122 +904,23 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
890904 })
891905
892906 t .Run ("TestDifferentMethodsShareSameLimiter" , func (t * testing.T ) {
893- // Test that different simulation methods share the same rate limiter.
894- // A single burst can occasionally avoid contention on overloaded CI workers,
895- // so retry a synchronized burst a few times.
896- const (
897- numCallRequests = 20
898- numEstimateRequests = 20
899- maxAttempts = 5
900- )
901- totalRequests := numCallRequests + numEstimateRequests
902-
903- runMixedBurst := func (tEnv * testEnv ) (int , int ) {
904- results := make (chan error , totalRequests )
905- start := make (chan struct {})
906- var wg sync.WaitGroup
907-
908- // Start mixed requests and release them at once to maximize contention.
909- for range numCallRequests {
910- wg .Go (func () {
911- <- start
912- _ , err := tEnv .simAPI .Call (t .Context (), tEnv .args , nil , nil , nil )
913- results <- err
914- })
915- }
916- for range numEstimateRequests {
917- wg .Go (func () {
918- <- start
919- _ , err := tEnv .simAPI .EstimateGas (t .Context (), tEnv .args , nil , nil )
920- results <- err
921- })
922- }
923-
924- close (start )
925- wg .Wait ()
926- close (results )
927-
928- successCount := 0
929- rejectedCount := 0
930- for err := range results {
931- if err == nil {
932- successCount ++
933- } else if strings .Contains (err .Error (), "rejected due to rate limit: server busy" ) {
934- rejectedCount ++
935- }
936- }
937- return successCount , rejectedCount
938- }
939-
940- var (
941- lastSuccess int
942- lastRejected int
943- attemptsUsed int
944- observedRejection bool
945- )
946- for attempt := 1 ; attempt <= maxAttempts ; attempt ++ {
947- attemptsUsed = attempt
948- lastSuccess , lastRejected = runMixedBurst (newTestEnv (t ))
949- require .Equalf (t , totalRequests , lastSuccess + lastRejected , "All mixed method requests should be accounted for (attempt %d)" , attempt )
950- if lastRejected > 0 {
951- observedRejection = true
952- break
953- }
954- }
955-
956- require .Truef (
957- t ,
958- observedRejection ,
959- "Different methods should share the same rate limiter (last burst: %d successful, %d rejected)" ,
960- lastSuccess ,
961- lastRejected ,
962- )
963- t .Logf (
964- "Mixed methods rate limiting (attempt %d/%d): %d successful, %d rejected out of %d total" ,
965- attemptsUsed ,
966- maxAttempts ,
967- lastSuccess ,
968- lastRejected ,
969- totalRequests ,
970- )
907+ tEnv := newTestEnv (t )
908+ assertRateLimited (t , tEnv , "eth_call rejected due to rate limit: server busy" , func () error {
909+ _ , err := tEnv .simAPI .Call (t .Context (), tEnv .args , nil , nil , nil )
910+ return err
911+ })
912+ assertRateLimited (t , tEnv , "eth_estimateGas rejected due to rate limit: server busy" , func () error {
913+ _ , err := tEnv .simAPI .EstimateGas (t .Context (), tEnv .args , nil , nil )
914+ return err
915+ })
971916 })
972917
973918 t .Run ("TestRateLimitErrorFormat" , func (t * testing.T ) {
974919 tEnv := newTestEnv (t )
975- // Test the error message format by overwhelming the rate limiter
976- const numRequests = 20
977- results := make (chan error , numRequests )
978- start := make (chan struct {})
979- var wg sync.WaitGroup
980-
981- // Release all requests at once to reliably saturate the limiter.
982- for range numRequests {
983- wg .Go (func () {
984- <- start
985- _ , err := tEnv .simAPI .Call (t .Context (), tEnv .args , nil , nil , nil )
986- results <- err
987- })
988- }
989- close (start )
990- wg .Wait ()
991- close (results )
992-
993- var rateLimitErrors []error
994- for err := range results {
995- if err != nil && strings .Contains (err .Error (), "rejected due to rate limit" ) {
996- rateLimitErrors = append (rateLimitErrors , err )
997- }
998- }
999-
1000- require .Greater (t , len (rateLimitErrors ), 0 , "Should have at least one rate limit error" )
1001-
1002- // Verify error message format
1003- for _ , err := range rateLimitErrors {
1004- require .Contains (t , err .Error (), "eth_call rejected due to rate limit: server busy" )
1005- require .Contains (t , err .Error (), "server busy" )
1006- }
1007-
1008- t .Logf ("Found %d rate limit errors with correct format" , len (rateLimitErrors ))
920+ assertRateLimited (t , tEnv , "eth_createAccessList rejected due to rate limit: server busy" , func () error {
921+ _ , err := tEnv .simAPI .CreateAccessList (t .Context (), tEnv .args , nil )
922+ return err
923+ })
1009924 })
1010925}
1011926
0 commit comments