Skip to content

Commit

Permalink
test: add missing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroara committed Jul 27, 2023
1 parent de221a7 commit a2be226
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cache/internal/behavior/behavior_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package behavior_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/hiroara/carbo/cache/internal/behavior"
)

func TestNew(t *testing.T) {
t.Parallel()

bc := behavior.New[int, *int](nil, behavior.CacheType)
bw := behavior.New[int, *int](nil, behavior.WriteOnlyType)
bb := behavior.New[int, *int](nil, behavior.BypassType)
bu := behavior.New[int, *int](nil, -1) // Unknown type

assert.NotNil(t, bc)
assert.NotNil(t, bw)
assert.NotNil(t, bb)
assert.Equal(t, bc, bu)
}
44 changes: 44 additions & 0 deletions task/emit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package task_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hiroara/carbo/task"
)

func TestEmit(t *testing.T) {
t.Run("NormalCase", func(t *testing.T) {
t.Parallel()

out := make(chan string, 1)
ctx := context.Background()
err := task.Emit(ctx, out, "test")
close(out)
require.NoError(t, err)

sl := make([]string, 0)
for el := range out {
sl = append(sl, el)
}

if assert.Len(t, sl, 1) {
assert.Equal(t, "test", sl[0])
}
})

t.Run("ContextCancelCase", func(t *testing.T) {
t.Parallel()

out := make(chan string)
ctx, cancel := context.WithCancel(context.Background())
cancel() // Context has already been canceled.

err := task.Emit(ctx, out, "test")
close(out)
require.ErrorIs(t, err, context.Canceled)
})
}

0 comments on commit a2be226

Please sign in to comment.