-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
253 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
//go:build integration && solver | ||
|
||
package solver_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/hashicorp/go-retryablehttp" | ||
httputil "github.com/lilypad-tech/lilypad/pkg/http" | ||
) | ||
|
||
type anuraRateResult struct { | ||
path string | ||
okCount int | ||
limitedCount int | ||
unauthorizedCount int | ||
} | ||
|
||
type anuraTestCase struct { | ||
name string | ||
privateKey string | ||
expectedOK int | ||
expectedLimit int | ||
expectedUnauth int | ||
} | ||
|
||
// This test suite sends 10 requests to Anura endpoints. We test both valid and invalid | ||
// signatures. Invalid signatures should be rate limited after 5 attempts. | ||
func TestAnuraRateLimiter(t *testing.T) { | ||
paths := []string{ | ||
"/api/v1/anura/job_offers", | ||
} | ||
|
||
validKey := "b3994e7660abe5f65f729bb64163c6cd6b7d0b1a8c67881a7346e3e8c7f026f5" | ||
invalidKey := "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" | ||
|
||
t.Run("valid anura signature passes", func(t *testing.T) { | ||
tc := anuraTestCase{ | ||
name: "valid signature should pass", | ||
privateKey: validKey, | ||
expectedOK: 10, // All 10 requests should succeed | ||
expectedLimit: 0, // No rate limiting | ||
expectedUnauth: 0, // No unauthorized responses | ||
} | ||
runAnuraTest(t, paths, tc) | ||
}) | ||
|
||
// Wait a bit between tests to let rate limiter reset | ||
time.Sleep(1 * time.Second) | ||
|
||
t.Run("invalid anura signature gets rate limited", func(t *testing.T) { | ||
tc := anuraTestCase{ | ||
name: "invalid signature should be rate limited", | ||
privateKey: invalidKey, | ||
expectedOK: 0, // No requests should succeed | ||
expectedLimit: 5, // 5 requests should be rate limited | ||
expectedUnauth: 5, // First 5 should be unauthorized | ||
} | ||
runAnuraTest(t, paths, tc) | ||
}) | ||
} | ||
|
||
func runAnuraTest(t *testing.T, paths []string, tc anuraTestCase) { | ||
var wg sync.WaitGroup | ||
ch := make(chan anuraRateResult, len(paths)) | ||
|
||
for _, path := range paths { | ||
wg.Add(1) | ||
go func(path string) { | ||
defer wg.Done() | ||
makeAnuraCalls(t, path, ch, tc) | ||
}(path) | ||
} | ||
|
||
wg.Wait() | ||
close(ch) | ||
|
||
for result := range ch { | ||
if result.okCount != tc.expectedOK { | ||
t.Errorf("%s: Expected %d successful requests, got %d", | ||
result.path, tc.expectedOK, result.okCount) | ||
} | ||
if result.limitedCount != tc.expectedLimit { | ||
t.Errorf("%s: Expected %d rate limited requests, got %d", | ||
result.path, tc.expectedLimit, result.limitedCount) | ||
} | ||
if result.unauthorizedCount != tc.expectedUnauth { | ||
t.Errorf("%s: Expected %d unauthorized requests, got %d", | ||
result.path, tc.expectedUnauth, result.unauthorizedCount) | ||
} | ||
} | ||
} | ||
|
||
func makeAnuraCalls(t *testing.T, path string, ch chan anuraRateResult, tc anuraTestCase) { | ||
var okCount, limitedCount, unauthorizedCount int | ||
|
||
// Configure client with no retries and silent logger | ||
client := retryablehttp.NewClient() | ||
client.RetryMax = 0 | ||
client.Logger = nil // Disable logging | ||
client.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) { | ||
return false, nil // Never retry | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
req, err := retryablehttp.NewRequest("GET", fmt.Sprintf("http://localhost:%d%s", 8081, path), nil) | ||
if err != nil { | ||
t.Errorf("Failed to create request: %s\n", err) | ||
return | ||
} | ||
|
||
privateKey, err := crypto.HexToECDSA(tc.privateKey) | ||
if err != nil { | ||
t.Errorf("Failed to parse private key: %s\n", err) | ||
return | ||
} | ||
|
||
err = httputil.AddAnuraHeaders(req, privateKey, crypto.PubkeyToAddress(privateKey.PublicKey).String()) | ||
if err != nil { | ||
t.Errorf("Failed to add Anura headers: %s\n", err) | ||
return | ||
} | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
t.Errorf("Request failed on %s: %s\n", path, err) | ||
return | ||
} | ||
|
||
switch res.StatusCode { | ||
case http.StatusOK: | ||
okCount++ | ||
case http.StatusTooManyRequests: | ||
limitedCount++ | ||
case http.StatusUnauthorized: | ||
unauthorizedCount++ | ||
default: | ||
t.Errorf("Expected 200, 401, or 429 status code, but received %d\n", res.StatusCode) | ||
} | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
} | ||
|
||
ch <- anuraRateResult{ | ||
path: path, | ||
okCount: okCount, | ||
limitedCount: limitedCount, | ||
unauthorizedCount: unauthorizedCount, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters