diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:40:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:40:59 +0000 |
commit | bc4e624732bd51c0dd1e9529cf228e8c23127732 (patch) | |
tree | d95dab8960e9d02d3b95f8653074ad2e54ca207c /pkg/utils/utils_test.go | |
parent | Initial commit. (diff) | |
download | icingadb-bc4e624732bd51c0dd1e9529cf228e8c23127732.tar.xz icingadb-bc4e624732bd51c0dd1e9529cf228e8c23127732.zip |
Adding upstream version 1.1.1.upstream/1.1.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pkg/utils/utils_test.go')
-rw-r--r-- | pkg/utils/utils_test.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 0000000..b0ea54b --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,54 @@ +package utils + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestChanFromSlice(t *testing.T) { + t.Run("Nil", func(t *testing.T) { + ch := ChanFromSlice[int](nil) + require.NotNil(t, ch) + requireClosedEmpty(t, ch) + }) + + t.Run("Empty", func(t *testing.T) { + ch := ChanFromSlice([]int{}) + require.NotNil(t, ch) + requireClosedEmpty(t, ch) + }) + + t.Run("NonEmpty", func(t *testing.T) { + ch := ChanFromSlice([]int{42, 23, 1337}) + require.NotNil(t, ch) + requireReceive(t, ch, 42) + requireReceive(t, ch, 23) + requireReceive(t, ch, 1337) + requireClosedEmpty(t, ch) + }) +} + +// requireReceive is a helper function to check if a value can immediately be received from a channel. +func requireReceive(t *testing.T, ch <-chan int, expected int) { + t.Helper() + + select { + case v, ok := <-ch: + require.True(t, ok, "receiving should return a value") + require.Equal(t, expected, v) + default: + require.Fail(t, "receiving should not block") + } +} + +// requireReceive is a helper function to check if the channel is closed and empty. +func requireClosedEmpty(t *testing.T, ch <-chan int) { + t.Helper() + + select { + case _, ok := <-ch: + require.False(t, ok, "receiving from channel should not return anything") + default: + require.Fail(t, "receiving should not block") + } +} |