|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/langgenius/dify-plugin-daemon/internal/types/app" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func newInstantServer(statusCode int) *httptest.Server { |
| 15 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 16 | + w.WriteHeader(statusCode) |
| 17 | + })) |
| 18 | +} |
| 19 | + |
| 20 | +func newSlowServer(delay time.Duration, statusCode int) *httptest.Server { |
| 21 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | + time.Sleep(delay) |
| 23 | + w.WriteHeader(statusCode) |
| 24 | + })) |
| 25 | +} |
| 26 | + |
| 27 | +func TestDetectAndApplyPipMirrorDisabled(t *testing.T) { |
| 28 | + config := app.Config{PipMirrorAutoDetect: false} |
| 29 | + mirror := detectAndApplyPipMirror(&config, &http.Client{Timeout: time.Second}, nil, "http://unused", time.Second) |
| 30 | + assert.Empty(t, mirror) |
| 31 | + assert.Empty(t, config.PipMirrorUrl) |
| 32 | +} |
| 33 | + |
| 34 | +func TestDetectAndApplyPipMirrorExplicitURLPreserved(t *testing.T) { |
| 35 | + config := app.Config{ |
| 36 | + PipMirrorAutoDetect: true, |
| 37 | + PipMirrorUrl: "https://mirror.example/simple", |
| 38 | + } |
| 39 | + mirror := detectAndApplyPipMirror(&config, &http.Client{Timeout: time.Second}, nil, "http://unused", time.Second) |
| 40 | + assert.Empty(t, mirror) |
| 41 | + assert.Equal(t, "https://mirror.example/simple", config.PipMirrorUrl) |
| 42 | +} |
| 43 | + |
| 44 | +func TestDetectAndApplyPipMirrorCandidateFasterThanOfficial(t *testing.T) { |
| 45 | + official := newSlowServer(80*time.Millisecond, http.StatusOK) |
| 46 | + defer official.Close() |
| 47 | + candidate := newInstantServer(http.StatusOK) |
| 48 | + defer candidate.Close() |
| 49 | + |
| 50 | + config := app.Config{PipMirrorAutoDetect: true} |
| 51 | + mirror := detectAndApplyPipMirror(&config, &http.Client{Timeout: 2 * time.Second}, []string{candidate.URL}, official.URL, 2*time.Second) |
| 52 | + assert.Equal(t, candidate.URL, mirror) |
| 53 | + assert.Equal(t, candidate.URL, config.PipMirrorUrl) |
| 54 | +} |
| 55 | + |
| 56 | +func TestDetectAndApplyPipMirrorOfficialFasterNoCandidateSelected(t *testing.T) { |
| 57 | + official := newInstantServer(http.StatusOK) |
| 58 | + defer official.Close() |
| 59 | + candidate := newSlowServer(80*time.Millisecond, http.StatusOK) |
| 60 | + defer candidate.Close() |
| 61 | + |
| 62 | + config := app.Config{PipMirrorAutoDetect: true} |
| 63 | + mirror := detectAndApplyPipMirror(&config, &http.Client{Timeout: 2 * time.Second}, []string{candidate.URL}, official.URL, 2*time.Second) |
| 64 | + assert.Empty(t, mirror) |
| 65 | + assert.Empty(t, config.PipMirrorUrl) |
| 66 | +} |
| 67 | + |
| 68 | +func TestDetectAndApplyPipMirrorOfficialUnreachableCandidateSelected(t *testing.T) { |
| 69 | + candidate := newInstantServer(http.StatusOK) |
| 70 | + defer candidate.Close() |
| 71 | + |
| 72 | + config := app.Config{PipMirrorAutoDetect: true} |
| 73 | + mirror := detectAndApplyPipMirror( |
| 74 | + &config, |
| 75 | + &http.Client{Timeout: 200 * time.Millisecond}, |
| 76 | + []string{candidate.URL}, |
| 77 | + "http://127.0.0.1:1", |
| 78 | + 200*time.Millisecond, |
| 79 | + ) |
| 80 | + assert.Equal(t, candidate.URL, mirror) |
| 81 | + assert.Equal(t, candidate.URL, config.PipMirrorUrl) |
| 82 | +} |
| 83 | + |
| 84 | +func TestDetectAndApplyPipMirrorPicksFastestCandidate(t *testing.T) { |
| 85 | + official := newSlowServer(120*time.Millisecond, http.StatusOK) |
| 86 | + defer official.Close() |
| 87 | + slow := newSlowServer(80*time.Millisecond, http.StatusOK) |
| 88 | + defer slow.Close() |
| 89 | + fast := newInstantServer(http.StatusOK) |
| 90 | + defer fast.Close() |
| 91 | + |
| 92 | + config := app.Config{PipMirrorAutoDetect: true} |
| 93 | + mirror := detectAndApplyPipMirror(&config, &http.Client{Timeout: 2 * time.Second}, []string{slow.URL, fast.URL}, official.URL, 2*time.Second) |
| 94 | + assert.Equal(t, fast.URL, mirror) |
| 95 | +} |
| 96 | + |
| 97 | +func TestDetectAndApplyPipMirrorAllUnreachableNoMirrorSet(t *testing.T) { |
| 98 | + config := app.Config{PipMirrorAutoDetect: true} |
| 99 | + mirror := detectAndApplyPipMirror( |
| 100 | + &config, |
| 101 | + &http.Client{Timeout: 200 * time.Millisecond}, |
| 102 | + []string{"http://127.0.0.1:1"}, |
| 103 | + "http://127.0.0.1:2", |
| 104 | + 200*time.Millisecond, |
| 105 | + ) |
| 106 | + assert.Empty(t, mirror) |
| 107 | + assert.Empty(t, config.PipMirrorUrl) |
| 108 | +} |
| 109 | + |
| 110 | +func TestDetectAndApplyPipMirrorCandidateErrorStatusIgnored(t *testing.T) { |
| 111 | + official := newInstantServer(http.StatusOK) |
| 112 | + defer official.Close() |
| 113 | + candidate := newInstantServer(http.StatusServiceUnavailable) |
| 114 | + defer candidate.Close() |
| 115 | + |
| 116 | + config := app.Config{PipMirrorAutoDetect: true} |
| 117 | + mirror := detectAndApplyPipMirror(&config, &http.Client{Timeout: time.Second}, []string{candidate.URL}, official.URL, time.Second) |
| 118 | + assert.Empty(t, mirror) |
| 119 | + assert.Empty(t, config.PipMirrorUrl) |
| 120 | +} |
| 121 | + |
| 122 | +func TestSelectFastestMirrorEmptyCandidates(t *testing.T) { |
| 123 | + official := newInstantServer(http.StatusOK) |
| 124 | + defer official.Close() |
| 125 | + |
| 126 | + ctx := context.Background() |
| 127 | + result := selectFastestMirror(ctx, &http.Client{Timeout: time.Second}, nil, official.URL) |
| 128 | + assert.Empty(t, result) |
| 129 | +} |
| 130 | + |
| 131 | +func TestApplyPipMirrorAutoDetectDoesNotBlockStartupOnAllFailures(t *testing.T) { |
| 132 | + config := app.Config{ |
| 133 | + PipMirrorAutoDetect: true, |
| 134 | + PipMirrorCandidates: []string{"http://127.0.0.1:1"}, |
| 135 | + } |
| 136 | + applyPipMirrorAutoDetect(&config) |
| 137 | + assert.Empty(t, config.PipMirrorUrl) |
| 138 | +} |
0 commit comments