From 3fe9d36ccc0550102570cc8990ff2f14c524fd69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sun, 5 Jan 2025 16:00:49 +0800 Subject: [PATCH] feat: add job test --- queue/job_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 queue/job_test.go diff --git a/queue/job_test.go b/queue/job_test.go new file mode 100644 index 000000000..091403074 --- /dev/null +++ b/queue/job_test.go @@ -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 +}