39 lines
878 B
Go
39 lines
878 B
Go
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, nil)
|
|
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())
|
|
}
|