github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/task/worker_test.go (about)

     1  package task
     2  
     3  import (
     4  	"testing"
     5  
     6  	model "github.com/cloudreve/Cloudreve/v3/models"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  type MockJob struct {
    11  	Err    *JobError
    12  	Status int
    13  	DoFunc func()
    14  }
    15  
    16  func (job *MockJob) Type() int {
    17  	panic("implement me")
    18  }
    19  
    20  func (job *MockJob) Creator() uint {
    21  	panic("implement me")
    22  }
    23  
    24  func (job *MockJob) Props() string {
    25  	panic("implement me")
    26  }
    27  
    28  func (job *MockJob) Model() *model.Task {
    29  	panic("implement me")
    30  }
    31  
    32  func (job *MockJob) SetStatus(status int) {
    33  	job.Status = status
    34  }
    35  
    36  func (job *MockJob) Do() {
    37  	job.DoFunc()
    38  }
    39  
    40  func (job *MockJob) SetError(*JobError) {
    41  }
    42  
    43  func (job *MockJob) GetError() *JobError {
    44  	return job.Err
    45  }
    46  
    47  func TestGeneralWorker_Do(t *testing.T) {
    48  	asserts := assert.New(t)
    49  	worker := &GeneralWorker{}
    50  	job := &MockJob{}
    51  
    52  	// 正常
    53  	{
    54  		job.DoFunc = func() {
    55  		}
    56  		worker.Do(job)
    57  		asserts.Equal(Complete, job.Status)
    58  	}
    59  
    60  	// 有错误
    61  	{
    62  		job.DoFunc = func() {
    63  		}
    64  		job.Status = Queued
    65  		job.Err = &JobError{Msg: "error"}
    66  		worker.Do(job)
    67  		asserts.Equal(Error, job.Status)
    68  	}
    69  
    70  	// 有致命错误
    71  	{
    72  		job.DoFunc = func() {
    73  			panic("mock fatal error")
    74  		}
    75  		job.Status = Queued
    76  		job.Err = nil
    77  		worker.Do(job)
    78  		asserts.Equal(Error, job.Status)
    79  	}
    80  
    81  }