Skip to content

Commit

Permalink
feat: add job test
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jan 5, 2025
1 parent 9ef22f0 commit 3fe9d36
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions queue/job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package queue

import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/goravel/framework/contracts/queue"
)

type JobTestSuite struct {
suite.Suite
jobManager *JobImpl
}

func TestJobTestSuite(t *testing.T) {
suite.Run(t, new(JobTestSuite))
}

func (s *JobTestSuite) SetupTest() {
s.jobManager = NewJobImpl()
}

func (s *JobTestSuite) RegisterJobsSuccessfully() {
jobs := []queue.Job{
&MockJob{signature: "job1"},
&MockJob{signature: "job2"},
}

err := s.jobManager.Register(jobs)
s.NoError(err)

registeredJobs := s.jobManager.GetJobs()
s.Len(registeredJobs, 2)
}

func (s *JobTestSuite) CallRegisteredJobSuccessfully() {
job := &MockJob{signature: "job1"}
s.NoError(s.jobManager.Register([]queue.Job{job}))

err := s.jobManager.Call("job1", []any{"arg1"})
s.NoError(err)
s.True(job.called)
}

func (s *JobTestSuite) CallUnregisteredJobFails() {
err := s.jobManager.Call("nonexistent", []any{"arg1"})
s.Error(err)
}

func (s *JobTestSuite) GetRegisteredJobSuccessfully() {
job := &MockJob{signature: "job1"}
s.NoError(s.jobManager.Register([]queue.Job{job}))

retrievedJob, err := s.jobManager.Get("job1")
s.NoError(err)
s.Equal(job, retrievedJob)
}

func (s *JobTestSuite) GetUnregisteredJobFails() {
_, err := s.jobManager.Get("nonexistent")
s.Error(err)
}

type MockJob struct {
signature string
called bool
}

func (m *MockJob) Signature() string {
return m.signature
}

func (m *MockJob) Handle(args ...any) error {
m.called = true
return nil
}

0 comments on commit 3fe9d36

Please sign in to comment.