Skip to content

Commit

Permalink
get worker status
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxErrorLineNULL committed Aug 26, 2024
1 parent 7424049 commit 4648606
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package worker

import (
"context"
"sync"
"testing"
"time"
wr "worker"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -116,4 +118,89 @@ func TestWorker(t *testing.T) {
// This verifies that the context does not get altered if a nil value is provided.
assert.Equal(t, ctx, worker.workerContext, "Worker context should remain unchanged when setting a nil context")
})

// GetStatusWithRun tests the behavior of a worker as it transitions through its lifecycle stages.
// The test verifies that the worker can correctly initialize, process jobs, and handle context
// cancellation. It checks that the worker starts in an idle state, transitions to running, and
// eventually stops as expected. Additionally, the test ensures that the worker's error channel
// is properly closed upon termination and that all goroutines complete without issues.
t.Run("GetStatusWithRun", func(t *testing.T) {
// Create a context with cancellation to manage the lifecycle of the worker and ensure proper cleanup.
// The context will allow the worker to be cancelled if necessary.
ctx, cancel := context.WithCancel(context.Background())
// Ensure that the context cancellation function is called at the end of the test.
// This prevents resource leaks by ensuring proper cleanup after the test completes.
defer cancel()

// Create a new Worker instance with ID 1, a timeout of 1 second, and a logger.
// This initializes the worker with specified parameters and ensures that it is properly set up.
worker := NewWorker(1)
// Assert that the worker instance is not nil.
// This checks that the worker was successfully created and is not a zero value.
assert.NotNil(t, worker, "Worker should be successfully created")

// Set the worker's context to the new background context.
// This tests the SetContext method by providing a valid context.
err := worker.SetContext(ctx)
// Assert that no error occurred when setting the context.
// This ensures that the SetContext method works as expected when a valid context is provided.
assert.NoError(t, err, "Expected no error when setting a valid context")

// Create a channel with a buffer size of 1 to receive tasks.
// This channel will be used as the job queue for the worker.
taskQueue := make(chan wr.Task, 1)

// Set the worker's queue to the open channel.
// This tests that the worker can successfully use the open channel as its job queue.
err = worker.SetQueue(taskQueue)
// Assert that no error is returned when setting an open channel.
// This confirms that setting an open channel is handled correctly by the worker.
assert.NoError(t, err, "Setting an open channel should not produce an error")

// Create a WaitGroup to manage synchronization of the worker's goroutine.
// This allows the test to wait for the worker to complete its execution.
wg := &sync.WaitGroup{}

// Increment the WaitGroup counter by 1 to account for the worker's goroutine.
// This is necessary to properly synchronize the test's main goroutine with the worker's goroutine.
wg.Add(1)
// Start the worker in a separate goroutine to allow it to run concurrently.
// The worker will begin processing tasks in this goroutine.
go worker.Start(wg)

// Sleep for a short duration to allow the worker to transition to its initial state.
// This delay simulates the time needed for the worker to initialize.
time.Sleep(30 * time.Millisecond)

// Assert that the worker's initial status is idle.
// This ensures that the worker is in the expected initial state before processing any jobs.
assert.Equal(t, wr.StatusWorkerIdle, worker.GetStatus(), "Worker should be idle after starting")

// Sleep for a short duration to allow the worker to potentially transition to a running state.
// This provides additional time for the worker to change its status if it starts processing.
time.Sleep(30 * time.Millisecond)

// Wait for the worker to stop or time out.
// This block manages the completion of the worker's lifecycle and handles any potential delays.
select {
case <-worker.Stop():
// Worker completed successfully.
// Any assertions or checks can be placed here if needed.
assert.Equal(t, wr.StatusWorkerStopped, worker.GetStatus(), "Expected worker status to be stopped")

// Attempt to read from the worker's error channel.
// This checks whether the channel is closed after the worker has stopped.
_, ok := <-worker.errCh
// Assert that the error channel is closed by verifying that the 'ok' value is false.
// If 'ok' is false, it indicates that the channel is closed, meaning no further errors can be received.
assert.False(t, ok, "Error channel should be closed after worker stops")

case <-time.After(2 * time.Second):
t.Error("Worker did not stop within the expected time.")
}

// Explicitly wait for the WaitGroup to ensure that all goroutines have finished executing.
// This ensures proper cleanup and prevents test flakiness due to unfinished goroutines.
wg.Wait()
})
}

0 comments on commit 4648606

Please sign in to comment.