* feat: added worker for executing and scheduling parallel tasks * feat: implemented worker based execution over cron handlers * feat: added router to package to run go fiber for dns validation * chore: added router package to go workspace * feat: added dns route controller * feat: added dns validation handler for route * feat: added healthcheck as a core function * feat: implemented worker for scheduling and running multiple tasks over goroutines * feat: added cron and http start handlers * feat: added prime scheduler handler to comply with prime scheduler * feat: implemented worker in cli start command * feat: added tests for dns validation handler * feat: added run test script to run test inside monitor * feat: added test for background worker * feat: implemented graceful shutdown for cancellation of jobs * feat: added readme for core package * fix: worker cancellation of tasks with ctx * fix: added closing channels in worker * fix: handled case for repeated keys in dns validation endpoint
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/makeplane/plane-ee/monitor/lib/logger"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPrimeWorker(t *testing.T) {
|
|
log := logger.NewHandler(nil)
|
|
worker := NewPrimeWorker(log)
|
|
|
|
// Test RegisterJob
|
|
job := func(ctx context.Context) {
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
|
|
worker.RegisterJob("job1", job)
|
|
worker.RegisterJob("job2", job)
|
|
|
|
assert.Equal(t, 2, worker.GetJobCount())
|
|
|
|
// Test StartJobsInBackground
|
|
worker.StartJobsInBackground()
|
|
// Running again to check the running status
|
|
worker.StartJobsInBackground()
|
|
time.Sleep(4 * time.Second) // Wait for the job to complete
|
|
assert.Equal(t, 0, worker.GetJobCount())
|
|
|
|
completedJobs := worker.GetCompletedJobs()
|
|
assert.True(t, slices.Contains(completedJobs, "job1"))
|
|
assert.True(t, slices.Contains(completedJobs, "job2"))
|
|
|
|
// Test Shutdown
|
|
worker.RegisterJob("job3", job)
|
|
worker.StartJobsInBackground()
|
|
time.Sleep(1 * time.Second) // Let the job start
|
|
|
|
worker.Shutdown()
|
|
assert.Equal(t, 0, worker.GetJobCount())
|
|
assert.Contains(t, worker.GetCompletedJobs(), "job2")
|
|
}
|