feat: 添加媒体资源处理的异步任务及相关逻辑
This commit is contained in:
85
backend/app/jobs/media_asset_process_test.go
Normal file
85
backend/app/jobs/media_asset_process_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
jobs_args "quyun/v2/app/jobs/args"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/riverqueue/river"
|
||||
"github.com/riverqueue/river/rivertype"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/gen/types"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type MediaAssetProcessJobSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
DB *sql.DB
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type MediaAssetProcessJobSuite struct {
|
||||
suite.Suite
|
||||
MediaAssetProcessJobSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_MediaAssetProcessJob(t *testing.T) {
|
||||
// 注意:testx.Default() 已内置一个测试用的 job worker 注册器,避免和 jobs.Provide 重复注册同 kind worker。
|
||||
providers := testx.Default().With(services.Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p MediaAssetProcessJobSuiteInjectParams) {
|
||||
suite.Run(t, &MediaAssetProcessJobSuite{MediaAssetProcessJobSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MediaAssetProcessJobSuite) Test_Work_ProcessingToReady() {
|
||||
Convey("MediaAssetProcessJobWorker processing -> ready", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB, models.TableNameMediaAsset)
|
||||
|
||||
asset := &models.MediaAsset{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Type: consts.MediaAssetTypeVideo,
|
||||
Status: consts.MediaAssetStatusProcessing,
|
||||
Provider: "test",
|
||||
Bucket: "b",
|
||||
ObjectKey: "k",
|
||||
Meta: types.JSON([]byte("{}")),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(asset.Create(ctx), ShouldBeNil)
|
||||
|
||||
worker := &MediaAssetProcessJobWorker{}
|
||||
err := worker.Work(ctx, &river.Job[jobs_args.MediaAssetProcessJob]{
|
||||
JobRow: &rivertype.JobRow{Attempt: 1},
|
||||
Args: jobs_args.MediaAssetProcessJob{TenantID: tenantID, AssetID: asset.ID},
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
tbl, query := models.MediaAssetQuery.QueryContext(ctx)
|
||||
got, err := query.Where(
|
||||
tbl.TenantID.Eq(tenantID),
|
||||
tbl.ID.Eq(asset.ID),
|
||||
).First()
|
||||
So(err, ShouldBeNil)
|
||||
So(got.Status, ShouldEqual, consts.MediaAssetStatusReady)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user