Skip to content

Commit

Permalink
add test case with close channel
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxErrorLineNULL committed Sep 4, 2024
1 parent 35e3537 commit d713c35
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions worker/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package worker

import (
"context"
wr "github.com/SyntaxErrorLineNULL/worker"
"runtime"
"sync"
"testing"
Expand Down Expand Up @@ -90,6 +91,36 @@ func TestTask(t *testing.T) {
assert.Equal(t, (chan<- struct{})(doneCh), task.doneCh, "expected the task's done channel to be set correctly")
})

// SetCloseChannel tests the behavior of the `SetDoneChannel` method when a closed channel is provided.
// It ensures that the method correctly handles the scenario where an attempt is made to set a channel
// that is already closed. The test verifies that an appropriate error is returned, specifically
// checking for the `wr.ChanIsCloseError` type, which indicates that the channel is no longer usable.
t.Run("SetCloseChannel", func(t *testing.T) {
// Create a channel of type struct{} to be used as the "done" channel.
// This channel will be used to signal completion or termination.
doneCh := make(chan struct{})

// Create a new instance of Task.
// This instance will be used to test the behavior of the SetDoneChannel method.
task := &Task{}

// Close the done channel to simulate a scenario where the channel is already closed.
// This tests how the SetDoneChannel method handles the situation when an attempt is made to set a closed channel.
close(doneCh)

// Call the SetDoneChannel method on the Task instance, passing the closed done channel.
// This method is expected to handle the closed channel appropriately, typically by returning an error.
err := task.SetDoneChannel(doneCh)

// Assert that an error is returned when attempting to set a closed channel.
// This confirms that the method behaves as expected and handles the error condition properly.
assert.Error(t, err, "Expected an error when setting a closed channel")

// Assert that the returned error is of the specific type wr.ChanIsCloseError.
// This ensures that the error returned matches the expected error type for a closed channel.
assert.ErrorIs(t, err, wr.ChanIsCloseError, "Expected error to be wr.ChanIsCloseError")
})

// SetEmptyChannel tests the SetDoneChannel method of the Task struct
// when attempting to set a nil channel. This test ensures that the method
// correctly handles the case where a nil channel is provided, returning
Expand Down

0 comments on commit d713c35

Please sign in to comment.