Files
kacerrandClaude Fable 5 7219eb5a23 Initial commit: Go SRE interview exercises
Includes solved 02-worker-pool and 09-safe-counter (mutex, atomic, sharded).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GbnJj9ZqEx9nUvzkjdwfMq
2026-08-28 22:18:09 +02:00

109 lines
2.8 KiB
Go

package main
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
func TestAllURLsChecked(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
urls := []string{srv.URL, srv.URL + "/a", srv.URL + "/b"}
res := CheckAll(context.Background(), urls, 2)
if len(res) != len(urls) {
t.Fatalf("want %d results, got %d", len(urls), len(res))
}
for i, r := range res {
if r.URL != urls[i] {
t.Errorf("result %d: URL order not preserved: want %s got %s", i, urls[i], r.URL)
}
if r.Err != nil {
t.Errorf("result %d: unexpected error: %v", i, r.Err)
}
if r.Status != http.StatusOK {
t.Errorf("result %d: want status 200, got %d", i, r.Status)
}
}
}
func TestErrorsAreReportedNotFatal(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer srv.Close()
urls := []string{srv.URL, "http://127.0.0.1:1/unreachable"}
res := CheckAll(context.Background(), urls, 2)
if res[0].Status != http.StatusServiceUnavailable {
t.Errorf("want 503 for first url, got %d (a 5xx is a RESULT, not an error)", res[0].Status)
}
if res[1].Err == nil {
t.Errorf("want transport error for unreachable url, got nil")
}
}
func TestConcurrencyLimitRespected(t *testing.T) {
var inFlight, peak atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cur := inFlight.Add(1)
for {
p := peak.Load()
if cur <= p || peak.CompareAndSwap(p, cur) {
break
}
}
time.Sleep(50 * time.Millisecond)
inFlight.Add(-1)
}))
defer srv.Close()
urls := make([]string, 10)
for i := range urls {
urls[i] = srv.URL
}
const limit = 3
CheckAll(context.Background(), urls, limit)
if p := peak.Load(); p > limit {
t.Errorf("concurrency limit violated: peak in-flight = %d, limit = %d", p, limit)
} else if p == 0 {
t.Errorf("no requests observed — is anything running?")
}
}
func TestContextCancellation(t *testing.T) {
block := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-block
}))
defer srv.Close()
defer close(block)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
urls := []string{srv.URL, srv.URL, srv.URL}
done := make(chan []Result, 1)
go func() { done <- CheckAll(ctx, urls, 1) }()
select {
case res := <-done:
for i, r := range res {
if r.Err == nil {
t.Errorf("result %d: want error after ctx timeout, got nil", i)
}
}
case <-time.After(2 * time.Second):
t.Fatal("CheckAll did not return after context cancellation — goroutines stuck?")
}
}