feat(model): enhance LoadFeed with timeout support and add tests

This commit is contained in:
2025-04-11 17:05:55 +08:00
parent 5fd117b1d1
commit 4cc6941d95
5 changed files with 114 additions and 11 deletions

5
go.mod
View File

@@ -5,6 +5,7 @@ go 1.23
require ( require (
github.com/mmcdole/gofeed v1.3.0 github.com/mmcdole/gofeed v1.3.0
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
go.uber.org/ratelimit v0.3.1 go.uber.org/ratelimit v0.3.1
) )
@@ -16,11 +17,13 @@ require (
require ( require (
github.com/PuerkitoBio/goquery v1.8.0 // indirect github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/andybalholm/cascadia v1.3.1 // 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/json-iterator/go v1.1.12 // indirect
github.com/mmcdole/goxpp v1.1.1-0.20240225020742-a0c311522b23 // 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/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // 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/net v0.4.0 // indirect
golang.org/x/text v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

1
go.sum
View File

@@ -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 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 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= 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/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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"time"
"gitea.malaihome.work/ans/podcast-search-go/model" "gitea.malaihome.work/ans/podcast-search-go/model"
"gitea.malaihome.work/ans/podcast-search-go/search" "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) fmt.Printf("name: %s, feedUrl: %s\n", item.CollectionName, item.FeedURL)
feedUrl := "http://www.ximalaya.com/album/19206382.xml" feedUrl := "http://www.ximalaya.com/album/19206382.xml"
pod, err := model.LoadFeed(feedUrl) pod, err := model.LoadFeed(feedUrl, 5*time.Second)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@@ -2,8 +2,9 @@
package model package model
import ( import (
"errors" "context"
"fmt" "fmt"
"time"
"github.com/mmcdole/gofeed" "github.com/mmcdole/gofeed"
) )
@@ -59,16 +60,19 @@ type Podcast struct {
Medium Medium `json:"medium,omitempty"` Medium Medium `json:"medium,omitempty"`
} }
// LoadFeed 从URL加载播客RSS订阅 // LoadFeed 从URL加载播客RSS订阅timeout为超时时间如果timeout<=0则使用默认值30秒
func LoadFeed(url string) (*Podcast, error) { func LoadFeed(url string, timeout time.Duration) (*Podcast, error) {
fp := gofeed.NewParser() if timeout <= 0 {
feed, err := fp.ParseURL(url) timeout = 30 * time.Second
if err != nil {
return nil, fmt.Errorf("解析播客订阅失败: %w", err)
} }
if feed == nil { ctx, cancel := context.WithTimeout(context.Background(), timeout)
return nil, errors.New("解析播客订阅失败: 空订阅") defer cancel()
fp := gofeed.NewParser()
feed, err := fp.ParseURLWithContext(url, ctx)
if err != nil {
return nil, fmt.Errorf("解析播客订阅失败: %w", err)
} }
imageURL := "" imageURL := ""

94
model/podcast_test.go Normal file
View File

@@ -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 := `
<rss version="2.0">
<channel>
<title>Test Podcast</title>
<description>This is a test podcast</description>
<link>http://example.com</link>
<image>
<url>http://example.com/image.jpg</url>
</image>
<copyright>Test Copyright</copyright>
<language>en</language>
<item>
<title>Episode 1</title>
<link>http://example.com/episode1</link>
<description>Episode 1 description</description>
</item>
</channel>
</rss>
`
// 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("<rss></rss>"))
}))
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(), "解析播客订阅失败")
}