20,000 listings to a newcomer: 120 s (unfinished) -> 24 s. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestASyncThatKeepsMakingProgressIsNotCutOff(t *testing.T) {
|
|
ctx, cancel := withIdleTimeout(context.Background(), 150*time.Millisecond, time.Minute)
|
|
defer cancel()
|
|
for i := 0; i < 10; i++ { // 500 ms in all, well past the idle limit
|
|
time.Sleep(50 * time.Millisecond)
|
|
progress(ctx)
|
|
if ctx.Err() != nil {
|
|
t.Fatalf("cut off after %d steps although it made progress", i)
|
|
}
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("not cut off after it stopped making progress")
|
|
}
|
|
}
|
|
|
|
func TestASyncWithoutProgressIsCutOff(t *testing.T) {
|
|
ctx, cancel := withIdleTimeout(context.Background(), 100*time.Millisecond, time.Minute)
|
|
defer cancel()
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("an idle sync was not cut off")
|
|
}
|
|
}
|
|
|
|
func TestASyncIsCutOffAtTheLatestAfterTheMaximum(t *testing.T) {
|
|
ctx, cancel := withIdleTimeout(context.Background(), time.Minute, 200*time.Millisecond)
|
|
defer cancel()
|
|
for end := time.Now().Add(2 * time.Second); time.Now().Before(end); {
|
|
progress(ctx)
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
t.Fatal("progress kept a sync alive past the maximum")
|
|
}
|
|
|
|
func TestProgressOnAPlainContextDoesNothing(t *testing.T) {
|
|
progress(context.Background()) // must not panic
|
|
}
|