feat(updater): FIFO queue with single worker and per-job results
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package updater_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shcizo/package-updater/internal/discovery"
|
||||
"github.com/shcizo/package-updater/internal/updater"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type counterExec struct{ n atomic.Int32 }
|
||||
|
||||
func (c *counterExec) Execute(_ context.Context, _ discovery.Job) error {
|
||||
c.n.Add(1)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestWorker_RunsExactlyOneAtATime(t *testing.T) {
|
||||
exec := &counterExec{}
|
||||
q := updater.NewQueue(exec)
|
||||
q.Start(context.Background())
|
||||
defer q.Stop()
|
||||
|
||||
jobs := make([]discovery.Job, 10)
|
||||
for i := range jobs {
|
||||
jobs[i] = discovery.Job{Project: "p", Service: "svc"}
|
||||
}
|
||||
start := time.Now()
|
||||
q.Submit(context.Background(), jobs)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
require.GreaterOrEqual(t, elapsed, 90*time.Millisecond)
|
||||
require.Equal(t, int32(10), exec.n.Load())
|
||||
}
|
||||
Reference in New Issue
Block a user