@@ -697,53 +697,88 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
697697 })
698698
699699 t .Run ("TestDifferentMethodsShareSameLimiter" , func (t * testing.T ) {
700- tEnv := newTestEnv (t )
701- // Test that different simulation methods share the same rate limiter
702- numCallRequests := 10
703- numEstimateRequests := 10
700+ // Test that different simulation methods share the same rate limiter.
701+ // A single burst can occasionally avoid contention on overloaded CI workers,
702+ // so retry a synchronized burst a few times.
703+ const (
704+ numCallRequests = 20
705+ numEstimateRequests = 20
706+ maxAttempts = 5
707+ )
704708 totalRequests := numCallRequests + numEstimateRequests
705709
706- results := make (chan error , totalRequests )
707- var wg sync.WaitGroup
708- // Start mixed requests concurrently to verify they share the same limiter
709- for range numCallRequests {
710- wg .Add (1 )
711- go func () {
712- defer wg .Done ()
713- _ , err := tEnv .simAPI .Call (context .Background (), tEnv .args , nil , nil , nil )
714- results <- err
715- }()
716- }
710+ runMixedBurst := func (tEnv * testEnv ) (int , int ) {
711+ results := make (chan error , totalRequests )
712+ start := make (chan struct {})
713+ var wg sync.WaitGroup
714+
715+ // Start mixed requests and release them at once to maximize contention.
716+ for range numCallRequests {
717+ wg .Add (1 )
718+ go func () {
719+ defer wg .Done ()
720+ <- start
721+ _ , err := tEnv .simAPI .Call (context .Background (), tEnv .args , nil , nil , nil )
722+ results <- err
723+ }()
724+ }
725+ for range numEstimateRequests {
726+ wg .Add (1 )
727+ go func () {
728+ defer wg .Done ()
729+ <- start
730+ _ , err := tEnv .simAPI .EstimateGas (context .Background (), tEnv .args , nil , nil )
731+ results <- err
732+ }()
733+ }
717734
718- for range numEstimateRequests {
719- wg .Add (1 )
720- go func () {
721- defer wg .Done ()
722- _ , err := tEnv .simAPI .EstimateGas (context .Background (), tEnv .args , nil , nil )
723- results <- err
724- }()
735+ close (start )
736+ wg .Wait ()
737+ close (results )
738+
739+ successCount := 0
740+ rejectedCount := 0
741+ for err := range results {
742+ if err == nil {
743+ successCount ++
744+ } else if strings .Contains (err .Error (), "rejected due to rate limit: server busy" ) {
745+ rejectedCount ++
746+ }
747+ }
748+ return successCount , rejectedCount
725749 }
726750
727- wg .Wait ()
728- close (results )
729-
730- // Collect all results and count
731- successCount := 0
732- rejectedCount := 0
733- for err := range results {
734- if err == nil {
735- successCount ++
736- } else if strings .Contains (err .Error (), "rejected due to rate limit: server busy" ) {
737- rejectedCount ++
751+ var (
752+ lastSuccess int
753+ lastRejected int
754+ attemptsUsed int
755+ observedRejection bool
756+ )
757+ for attempt := 1 ; attempt <= maxAttempts ; attempt ++ {
758+ attemptsUsed = attempt
759+ lastSuccess , lastRejected = runMixedBurst (newTestEnv (t ))
760+ require .Equalf (t , totalRequests , lastSuccess + lastRejected , "All mixed method requests should be accounted for (attempt %d)" , attempt )
761+ if lastRejected > 0 {
762+ observedRejection = true
763+ break
738764 }
739765 }
740766
741- // Since the rate limiter allows 2 concurrent requests total, we should see some rejections
742- // when running 6 concurrent requests across different methods
743- require .Greater (t , rejectedCount , 0 , "Different methods should share the same rate limiter" )
744- require .Equal (t , totalRequests , successCount + rejectedCount , "All mixed method requests should be accounted for" )
745-
746- t .Logf ("Mixed methods rate limiting: %d successful, %d rejected out of %d total" , successCount , rejectedCount , totalRequests )
767+ require .Truef (
768+ t ,
769+ observedRejection ,
770+ "Different methods should share the same rate limiter (last burst: %d successful, %d rejected)" ,
771+ lastSuccess ,
772+ lastRejected ,
773+ )
774+ t .Logf (
775+ "Mixed methods rate limiting (attempt %d/%d): %d successful, %d rejected out of %d total" ,
776+ attemptsUsed ,
777+ maxAttempts ,
778+ lastSuccess ,
779+ lastRejected ,
780+ totalRequests ,
781+ )
747782 })
748783
749784 t .Run ("TestRateLimitErrorFormat" , func (t * testing.T ) {
0 commit comments