package rproxy import ( "net/http" "net/http/httptrace" "sync/atomic" "testing" "testing/synctest" "time" ) // The tests below run timeLimits in a synctest bubble, where time is a clock of // the test's own: time.Sleep moves it on at once, and synctest.Wait returns once // a limit that ran out has refused the request. const ( testRequestTime = 3 * time.Second testResponseTime = 5 * time.Second ) // limitsSeen is what a timeLimits under test did to its request. type limitsSeen struct { // refused is the status of the last refusal, or zero. refused atomic.Int64 // readEnded is true once the read of the visitor's body was ended. readEnded atomic.Bool // waiting is what waitingForVisitor reports. waiting atomic.Bool } // newTestLimits returns time limits that started now, with what they do noted // in seen. func newTestLimits(seen *limitsSeen) *timeLimits { return &timeLimits{ maxRequestTime: testRequestTime, maxResponseTime: testResponseTime, start: time.Now(), refuse: func(status int) { seen.refused.Store(int64(status)) }, waitingForVisitor: seen.waiting.Load, endVisitorRead: func() { seen.readEnded.Store(true) }, } } // sleepThenWait moves the bubble's clock on by d and waits for what is then // due. func sleepThenWait(d time.Duration) { time.Sleep(d) synctest.Wait() } func TestRequestTimeRunningOutWhileTheVisitorIsWaitedOnRefuses408( t *testing.T, ) { t.Parallel() synctest.Test(t, func(t *testing.T) { seen := &limitsSeen{} seen.waiting.Store(true) limits := newTestLimits(seen) limits.startRequestTimer() sleepThenWait(testRequestTime - time.Millisecond) if got := seen.refused.Load(); got != 0 { t.Fatalf( "refused %d before the limit ran out, want nothing", got, ) } sleepThenWait(time.Millisecond) if got := seen.refused.Load(); got != http.StatusRequestTimeout { t.Errorf("refused %d, want 408", got) } if !seen.readEnded.Load() { t.Error("the read of the visitor's body goes on, want it ended") } }) } func TestRequestTimeRunningOutWhileTheAppIsWaitedOnRefuses504( t *testing.T, ) { t.Parallel() synctest.Test(t, func(t *testing.T) { seen := &limitsSeen{} limits := newTestLimits(seen) limits.startRequestTimer() sleepThenWait(testRequestTime) if got := seen.refused.Load(); got != http.StatusGatewayTimeout { t.Errorf("refused %d, want 504", got) } if seen.readEnded.Load() { t.Error("the read of the visitor's body was ended, want it not") } }) } func TestResponseTimeRunsFromTheEndOfTheRequest(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { seen := &limitsSeen{} limits := newTestLimits(seen) limits.startRequestTimer() sleepThenWait(time.Second) end := time.Now() limits.wroteRequest(httptrace.WroteRequestInfo{}) if got := limits.endRequest(); !got.Equal(end) { t.Errorf("the request ended at %v, want %v", got, end) } // Past SWW_RPROXY_MAX_REQUEST_TIME_MS, which the end of the request // stopped, and just short of SWW_RPROXY_MAX_RESPONSE_TIME_MS. sleepThenWait(testResponseTime - time.Millisecond) if got := seen.refused.Load(); got != 0 { t.Fatalf( "refused %d before the limit ran out, want nothing", got, ) } sleepThenWait(time.Millisecond) if got := seen.refused.Load(); got != http.StatusGatewayTimeout { t.Errorf("refused %d, want 504", got) } }) } func TestStoppedLimitsRefuseNothing(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { seen := &limitsSeen{} seen.waiting.Store(true) limits := newTestLimits(seen) limits.startRequestTimer() limits.stop() limits.startResponseTimer(limits.endRequest()) sleepThenWait(testRequestTime + testResponseTime) if got := seen.refused.Load(); got != 0 { t.Errorf("refused %d, want nothing", got) } if seen.readEnded.Load() { t.Error("the read of the visitor's body was ended, want it not") } }) }