mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-01 12:36:50 +00:00
chore: stabilize traffic wait timing in tlsmirror test
This commit is contained in:
1 parent
ee80e8d641
commit
cf8545dbd1
2 files changed
+45
-23
No files matched your search
@@ -359,24 +359,35 @@ func TestTLSMirrorTrafficGenerator(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("wait time includes request time", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
err := runTrafficStep(context.Background(), roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
now := time.Unix(0, 0)
|
||||
requestTime := 120 * time.Millisecond
|
||||
waitTime := 200 * time.Millisecond
|
||||
fakeNow := func() time.Time {
|
||||
return now
|
||||
}
|
||||
var waited time.Duration
|
||||
fakeWait := func(ctx context.Context, delay time.Duration) error {
|
||||
waited = delay
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
err := runTrafficStepWithClock(context.Background(), roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusNoContent,
|
||||
Body: delayedEOFBody{delay: 120 * time.Millisecond},
|
||||
Body: &readHookEOFBody{read: func() {
|
||||
now = now.Add(requestTime)
|
||||
}},
|
||||
}, nil
|
||||
}), TrafficStep{
|
||||
Host: "example.com",
|
||||
Path: "/",
|
||||
Method: http.MethodGet,
|
||||
WaitTime: TimeSpec{
|
||||
BaseNanoseconds: uint64((200 * time.Millisecond).Nanoseconds()),
|
||||
BaseNanoseconds: uint64(waitTime.Nanoseconds()),
|
||||
},
|
||||
}, "http/1.1")
|
||||
}, "http/1.1", fakeNow, fakeWait)
|
||||
require.NoError(t, err)
|
||||
elapsed := time.Since(start)
|
||||
require.GreaterOrEqual(t, elapsed, 200*time.Millisecond)
|
||||
require.Less(t, elapsed, 300*time.Millisecond)
|
||||
require.Equal(t, waitTime-requestTime, waited)
|
||||
})
|
||||
|
||||
t.Run("uses v2ray host semantics", func(t *testing.T) {
|
||||
@@ -576,20 +587,22 @@ func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return f(req)
|
||||
}
|
||||
|
||||
type delayedEOFBody struct {
|
||||
delay time.Duration
|
||||
done bool
|
||||
type readHookEOFBody struct {
|
||||
read func()
|
||||
done bool
|
||||
}
|
||||
|
||||
func (b delayedEOFBody) Read([]byte) (int, error) {
|
||||
func (b *readHookEOFBody) Read([]byte) (int, error) {
|
||||
if !b.done {
|
||||
time.Sleep(b.delay)
|
||||
b.done = true
|
||||
if b.read != nil {
|
||||
b.read()
|
||||
}
|
||||
}
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
func (b delayedEOFBody) Close() error {
|
||||
func (b *readHookEOFBody) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -138,6 +138,10 @@ func trafficGeneratorWaitsForReady(cfg *TrafficGenerator) bool {
|
||||
}
|
||||
|
||||
func runTrafficStep(ctx context.Context, transport trafficHTTPTransport, step TrafficStep, alpn string) error {
|
||||
return runTrafficStepWithClock(ctx, transport, step, alpn, time.Now, waitTrafficStep)
|
||||
}
|
||||
|
||||
func runTrafficStepWithClock(ctx context.Context, transport trafficHTTPTransport, step TrafficStep, alpn string, now func() time.Time, wait func(context.Context, time.Duration) error) error {
|
||||
requestURL := &url.URL{
|
||||
Scheme: "https",
|
||||
Host: step.Host,
|
||||
@@ -163,7 +167,7 @@ func runTrafficStep(ctx context.Context, transport trafficHTTPTransport, step Tr
|
||||
}
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
start := now()
|
||||
resp, err := transport.RoundTrip(req)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -182,21 +186,26 @@ func runTrafficStep(ctx context.Context, transport trafficHTTPTransport, step Tr
|
||||
} else if err := finishRequest(); err != nil {
|
||||
return err
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
elapsed := now().Sub(start)
|
||||
if delay, err := step.WaitTime.Duration(); err != nil {
|
||||
return err
|
||||
} else if delay > elapsed {
|
||||
timer := time.NewTimer(delay - elapsed)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
return wait(ctx, delay-elapsed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func waitTrafficStep(ctx context.Context, delay time.Duration) error {
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func chooseNextTrafficStep(step TrafficStep, current int) (int, bool, error) {
|
||||
if len(step.NextStep) == 0 {
|
||||
return 0, false, nil
|
||||
|
||||
Reference in new issue
Block a user