8000 [pull] main from miniflux:main by pull[bot] · Pull Request #352 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions internal/integration/raindrop/raindrop.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ type Client struct {
}

func NewClient(token, collectionID, tags string) *Client {
return &Client{token: token, collectionID: collectionID, tags: strings.Split(tags, ",")}
var tagList []string
for tag := range strings.SplitSeq(tags, ",") {
if trimmedTag := strings.TrimSpace(tag); trimmedTag != "" {
tagList = append(tagList, trimmedTag)
}
}
return &Client{token: token, collectionID: collectionID, tags: tagList}
}

// https://developer.raindrop.io/v1/raindrops/single#create-raindrop
Expand Down Expand Up @@ -54,7 +60,7 @@ type raindrop struct {
Link string `json:"link"`
Title string `json:"title"`
Collection collection `json:"collection"`
Tags []string `json:"tags"`
Tags []string `json:"tags,omitempty"`
}

type collection struct {
Expand Down
50 changes: 50 additions & 0 deletions internal/integration/raindrop/raindrop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package raindrop

import (
"encoding/json"
"slices"
"testing"
)

func TestNewClientTagParsing(t *testing.T) {
tests := []struct {
name string
tags string
want []string
}{
{name: "empty string produces no tags", tags: "", want: nil},
{name: "single tag", tags: "news", want: []string{"news"}},
{name: "multiple tags", tags: "news,tech", want: []string{"news", "tech"}},
{name: "whitespace is trimmed", tags: " news , tech ", want: []string{"news", "tech"}},
{name: "empty items are dropped", tags: "news,,tech,", want: []string{"news", "tech"}},
{name: "only separators produce no tags", tags: ", ,", want: nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := NewClient("token", "collection", tt.tags)
if !slices.Equal(client.tags, tt.want) {
t.Errorf("NewClient(%q) tags = %#v, want %#v", tt.tags, client.tags, tt.want)
}
})
}
}

func TestPayloadOmitsEmptyTags(t *testing.T) {
payload, err := json.Marshal(&raindrop{Link: "https://example.com", Title: "Example"})
if err != nil {
t.Fatalf("unable to marshal payload: %v", err)
}

var fields map[string]json.RawMessage
if err := json.Unmarshal(payload, &fields); err != nil {
t.Fatalf("unable to unmarshal payload: %v", err)
}

if _, found := fields["tags"]; found {
t.Errorf("payload without tags should omit the tags field, got %s", payload)
}
}
33 changes: 26 additions & 7 deletions internal/locale/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"embed"
"encoding/json"
"fmt"
"sync"
)

type translationDict struct {
Expand All @@ -15,19 +16,37 @@ type translationDict struct {
}
type catalog map[string]translationDict

var defaultCatalog = make(catalog, len(AvailableLanguages))
// defaultCatalog is populated lazily by getTranslationDict, which runs on
// concurrent request goroutines, so every access must hold defaultCatalogMutex.
var (
defaultCatalog = make(catalog, len(AvailableLanguages))
defaultCatalogMutex sync.RWMutex
)

//go:embed translations/*.json
var translationFiles embed.FS

func getTranslationDict(language string) (translationDict, error) {
if _, ok := defaultCatalog[language]; !ok {
var err error
if defaultCatalog[language], err = loadTranslationFile(language); err != nil {
return translationDict{}, err
}
defaultCatalogMutex.RLock()
dict, found := defaultCatalog[language]
defaultCatalogMutex.RUnlock()
if found {
return dict, nil
}

defaultCatalogMutex.Lock()
defer defaultCatalogMutex.Unlock()

if dict, found := defaultCatalog[language]; found {
return dict, nil
}

dict, err := loadTranslationFile(language)
if err != nil {
return translationDict{}, err
}
return defaultCatalog[language], nil
defaultCatalog[language] = dict
return dict, nil
}

func loadTranslationFile(language string) (translationDict, error) {
Expand Down
30 changes: 30 additions & 0 deletions internal/locale/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package locale // import "miniflux.app/v2/internal/locale"

import (
"sync"
"testing"
)

Expand All @@ -30,6 +31,35 @@ func TestParser(t *testing.T) {
}
}

// TestGetTranslationDictConcurrency exercises the lazy population of the
// catalog from concurrent goroutines, as HTTP request handlers do. It must be
// run with the race detector enabled to catch unsynchronized catalog access.
func TestGetTranslationDictConcurrency(t *testing.T) {
defaultCatalog = make(catalog, len(AvailableLanguages))

const iterations = 10

var wg sync.WaitGroup
for i := 0; i < iterations; i++ {
for language := range AvailableLanguages {
wg.Add(1)
go func(language string) {
defer wg.Done()

dict, err := getTranslationDict(language)
if err != nil {
t.Errorf(`Unable to get translation dictionary for language %q: %v`, language, err)
return
}
if len(dict.singulars) == 0 {
t.Errorf(`The translation dictionary for language %q should not be empty`, language)
}
}(language)
}
}
wg.Wait()
}

func TestLoadCatalog(t *testing.T) {
for language := range AvailableLanguages {
_, err := loadTranslationFile(language)
Expand Down
1 change: 1 addition & 0 deletions internal/ui/subscription_choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (h *handler) showChooseSubscriptionPage(w http.ResponseWriter, r *http.Requ
view.Set("countUnread", navMetadata.CountUnread)
view.Set("countErrorFeeds", navMetadata.CountErrorFeeds)
view.Set("defaultUserAgent", config.Opts.HTTPClientUserAgent())
view.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyURLConfigured())

subscriptionForm := form.NewSubscriptionForm(r)
if validationErr := subscriptionForm.Validate(); validationErr != nil {
Expand Down
24 changes: 17 additions & 7 deletions internal/worker/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,43 @@ import (

// Pool manages a set of background workers that process feed refresh jobs.
type Pool struct {
queue chan model.Job
wg sync.WaitGroup
queue chan model.Job
shutdown chan struct{}
shutdownOnce sync.Once
wg sync.WaitGroup
}

// Push sends a list of jobs to the queue.
// Jobs pushed after Shutdown are discarded.
func (p *Pool) Push(jobs model.JobList) {
for _, job := range jobs {
p.queue <- job
select {
case p.queue <- job:
case <-p.shutdown:
return
}
}
}

// Shutdown closes the job queue and waits for all workers to finish their current jobs.
// Shutdown stops accepting new jobs and waits for all workers to finish their current jobs.
func (p *Pool) Shutdown() {
close(p.queue)
p.shutdownOnce.Do(func() {
close(p.shutdown)
})
p.wg.Wait()
}

// NewPool creates a pool of background workers.
func NewPool(store *storage.Storage, nbWorkers int) *Pool {
workerPool := &Pool{
queue: make(chan model.Job),
queue: make(chan model.Job),
shutdown: make(chan struct{}),
}

for i := range nbWorkers {
workerPool.wg.Add(1)
worker := &worker{id: i, store: store}
go worker.Run(workerPool.queue, &workerPool.wg)
go worker.Run(workerPool.queue, workerPool.shutdown, &workerPool.wg)
}

return workerPool
Expand Down
65 changes: 65 additions & 0 deletions internal/worker/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package worker // import "miniflux.app/v2/internal/worker"

import (
"testing"
"time"

"miniflux.app/v2/internal/model"
)

func TestPushAfterShutdownDiscardsJobs(t *testing.T) {
pool := NewPool(nil, 2)
pool.Shutdown()

done := make(chan struct{})
go func() {
defer close(done)
pool.Push(model.JobList{{FeedID: 1}, {FeedID: 2}})
}()

select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("Push blocked after Shutdown instead of discarding jobs")
}
}

func TestShutdownUnblocksPendingPush(t *testing.T) {
pool := NewPool(nil, 0)

pushed := make(chan struct{})
go func() {
defer close(pushed)
pool.Push(model.JobList{{FeedID: 1}})
}()

// Give Push time to block on the unbuffered queue before shutting down.
time.Sleep(10 * time.Millisecond)

done := make(chan struct{})
go func() {
defer close(done)
pool.Shutdown()
}()

select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("Shutdown deadlocked while a Push was pending")
}

select {
case <-pushed:
case <-time.After(5 * time.Second):
t.Fatal("Push remained blocked after Shutdown")
}
}

func TestShutdownIsIdempotent(t *testing.T) {
pool := NewPool(nil, 1)
pool.Shutdown()
pool.Shutdown()
}
13 changes: 10 additions & 3 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ type worker struct {
store *storage.Storage
}

// Run processes feed refresh jobs from the channel until it is closed.
func (w *worker) Run(c <-chan model.Job, wg *sync.WaitGroup) {
// Run processes feed refresh jobs from the channel until the pool is shut down.
func (w *worker) Run(c <-chan model.Job, shutdown <-chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()

slog.Debug("Worker started",
slog.Int("worker_id", w.id),
)

for job := range c {
for {
var job model.Job
select {
case <-shutdown:
return
case job = <-c:
}

slog.Debug("Job received by worker",
slog.Int("worker_id", w.id),
slog.Int64("user_id", job.UserID),
Expand Down
0