Skip to content

Commit

Permalink
faucet: remove test for now which cant test it fully
Browse files Browse the repository at this point in the history
  • Loading branch information
emailtovamos committed Jul 26, 2024
1 parent 45c683f commit 75c18c9
Showing 1 changed file with 0 additions and 93 deletions.
93 changes: 0 additions & 93 deletions cmd/faucet/faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,9 @@
package main

import (
"net/http"
"testing"

"net/http/httptest"
"sync"
"time"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/time/rate"

"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
)

func TestFacebook(t *testing.T) {
Expand All @@ -53,86 +43,3 @@ func TestFacebook(t *testing.T) {
}
}
}

func TestFaucetRateLimiting(t *testing.T) {
// Create a minimal faucet instance for testing
privateKey, _ := crypto.GenerateKey()
faucetAddr := crypto.PubkeyToAddress(privateKey.PublicKey)

config := &core.Genesis{
Alloc: core.GenesisAlloc{
faucetAddr: {Balance: common.Big1},
},
}

// Create a faucet with rate limiting (1 request per second, burst of 2)
ks := keystore.NewKeyStore(t.TempDir(), keystore.LightScryptN, keystore.LightScryptP)
_, err := ks.NewAccount("password")
if err != nil {
t.Fatal(err)
}
if len(ks.Accounts()) == 0 {
t.Fatalf("No accounts %v", ks)
}
f, err := newFaucet(config, "http://localhost:8545", ks, []byte{}, nil)
if err != nil {
t.Fatalf("Failed to create faucet: %v", err)
}
f.limiter, err = NewIPRateLimiter(rate.Limit(1.0), 1, 2)
if err != nil {
t.Fatalf("Failed to create NewIPRateLimiter: %v", err)
}

// Create a test server
server := httptest.NewServer(http.HandlerFunc(f.apiHandler))
defer server.Close()

// Helper function to make a request
makeRequest := func() int {
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("Failed to send request: %v", err)
}
defer resp.Body.Close()
return resp.StatusCode
}

// Test rapid requests
var wg sync.WaitGroup
results := make([]int, 5)

for i := 0; i < 5; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
results[index] = makeRequest()
}(i)
}

wg.Wait()

// Check results
successCount := 0
rateLimitCount := 0
for _, status := range results {
if status == http.StatusOK {
successCount++
} else if status == http.StatusTooManyRequests {
rateLimitCount++
}
}

// We expect 2 successful requests (due to burst) and 3 rate-limited requests
if successCount != 2 || rateLimitCount != 3 {
t.Errorf("Expected 2 successful and 3 rate-limited requests, got %d successful and %d rate-limited", successCount, rateLimitCount)
}

// Wait for rate limit to reset
time.Sleep(2 * time.Second)

// Make another request, it should succeed
status := makeRequest()
if status != http.StatusOK {
t.Errorf("Expected success after rate limit reset, got status %d", status)
}
}

0 comments on commit 75c18c9

Please sign in to comment.