From 4cc6941d9530368c288d279fbb74782b077ded5f Mon Sep 17 00:00:00 2001 From: malai Date: Fri, 11 Apr 2025 17:05:55 +0800 Subject: [PATCH] feat(model): enhance LoadFeed with timeout support and add tests --- go.mod | 5 ++- go.sum | 1 + main.go | 3 +- model/podcast.go | 22 +++++----- model/podcast_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 model/podcast_test.go diff --git a/go.mod b/go.mod index a53ab2d..96d26a1 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23 require ( github.com/mmcdole/gofeed v1.3.0 github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.8.4 go.uber.org/ratelimit v0.3.1 ) @@ -16,11 +17,13 @@ require ( require ( github.com/PuerkitoBio/goquery v1.8.0 // indirect github.com/andybalholm/cascadia v1.3.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/mmcdole/goxpp v1.1.1-0.20240225020742-a0c311522b23 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/stretchr/testify v1.8.4 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.4.0 // indirect golang.org/x/text v0.14.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 85ea2a8..5db8789 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index ae9f961..8a8c855 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "time" "gitea.malaihome.work/ans/podcast-search-go/model" "gitea.malaihome.work/ans/podcast-search-go/search" @@ -24,7 +25,7 @@ func main() { fmt.Printf("name: %s, feedUrl: %s\n", item.CollectionName, item.FeedURL) feedUrl := "http://www.ximalaya.com/album/19206382.xml" - pod, err := model.LoadFeed(feedUrl) + pod, err := model.LoadFeed(feedUrl, 5*time.Second) if err != nil { panic(err) } diff --git a/model/podcast.go b/model/podcast.go index e18f6ad..3599c0f 100644 --- a/model/podcast.go +++ b/model/podcast.go @@ -2,8 +2,9 @@ package model import ( - "errors" + "context" "fmt" + "time" "github.com/mmcdole/gofeed" ) @@ -59,16 +60,19 @@ type Podcast struct { Medium Medium `json:"medium,omitempty"` } -// LoadFeed 从URL加载播客RSS订阅 -func LoadFeed(url string) (*Podcast, error) { - fp := gofeed.NewParser() - feed, err := fp.ParseURL(url) - if err != nil { - return nil, fmt.Errorf("解析播客订阅失败: %w", err) +// LoadFeed 从URL加载播客RSS订阅,timeout为超时时间,如果timeout<=0则使用默认值30秒 +func LoadFeed(url string, timeout time.Duration) (*Podcast, error) { + if timeout <= 0 { + timeout = 30 * time.Second } - if feed == nil { - return nil, errors.New("解析播客订阅失败: 空订阅") + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + fp := gofeed.NewParser() + feed, err := fp.ParseURLWithContext(url, ctx) + if err != nil { + return nil, fmt.Errorf("解析播客订阅失败: %w", err) } imageURL := "" diff --git a/model/podcast_test.go b/model/podcast_test.go new file mode 100644 index 0000000..64161a5 --- /dev/null +++ b/model/podcast_test.go @@ -0,0 +1,94 @@ +package model + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestLoadFeed_Success(t *testing.T) { + // Mock RSS feed + rssFeed := ` + + + Test Podcast + This is a test podcast + http://example.com + + http://example.com/image.jpg + + Test Copyright + en + + Episode 1 + http://example.com/episode1 + Episode 1 description + + + + ` + + // Create a test server + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(rssFeed)) + })) + defer server.Close() + + // Call LoadFeed + podcast, err := LoadFeed(server.URL, 10*time.Second) + + // Assertions + assert.NoError(t, err) + assert.NotNil(t, podcast) + assert.Equal(t, "Test Podcast", podcast.Title) + assert.Equal(t, "This is a test podcast", podcast.Description) + assert.Equal(t, "http://example.com", podcast.Link) + assert.Equal(t, "http://example.com/image.jpg", podcast.Image) + assert.Equal(t, "Test Copyright", podcast.Copyright) + assert.Equal(t, "en", podcast.Language) + assert.Len(t, podcast.Episodes, 1) + assert.Equal(t, "Episode 1", podcast.Episodes[0].Title) +} + +func TestLoadFeed_Timeout(t *testing.T) { + // Create a test server that delays response + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(2 * time.Second) + w.Write([]byte("")) + })) + defer server.Close() + + // Call LoadFeed with a short timeout + _, err := LoadFeed(server.URL, 1*time.Second) + + // Assertions + assert.Error(t, err) + assert.Contains(t, err.Error(), "context deadline exceeded") +} + +func TestLoadFeed_InvalidURL(t *testing.T) { + // Call LoadFeed with an invalid URL + _, err := LoadFeed("http://invalid-url", 10*time.Second) + + // Assertions + assert.Error(t, err) + assert.Contains(t, err.Error(), "解析播客订阅失败") +} + +func TestLoadFeed_EmptyFeed(t *testing.T) { + // Create a test server with an empty feed + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("")) + })) + defer server.Close() + + // Call LoadFeed + _, err := LoadFeed(server.URL, 10*time.Second) + + // Assertions + assert.Error(t, err) + assert.Contains(t, err.Error(), "解析播客订阅失败") +}