From f92a240c17b898f76c813765499ef4b6240b9616 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 8 Jul 2024 17:50:26 +0200 Subject: [PATCH] Test com.WaitAsync() --- com/com_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 com/com_test.go diff --git a/com/com_test.go b/com/com_test.go new file mode 100644 index 00000000..6fc2f648 --- /dev/null +++ b/com/com_test.go @@ -0,0 +1,50 @@ +package com + +import ( + "github.com/stretchr/testify/require" + "io" + "testing" + "time" +) + +func TestWaitAsync(t *testing.T) { + subtests := []struct { + name string + input WaiterFunc + error error + }{ + {"no_error", func() error { return nil }, nil}, + {"error", func() error { return io.EOF }, io.EOF}, + {"sleep_no_error", func() error { time.Sleep(time.Second / 2); return nil }, nil}, + {"sleep_error", func() error { time.Sleep(time.Second / 2); return io.EOF }, io.EOF}, + } + + for _, st := range subtests { + t.Run(st.name, func(t *testing.T) { + errs := WaitAsync(st.input) + require.NotNil(t, errs) + + if st.error != nil { + select { + case e, ok := <-errs: + if !ok { + require.Fail(t, "channel should not be closed, yet") + } + + require.Equal(t, st.error, e) + case <-time.After(time.Second): + require.Fail(t, "channel should not block") + } + } + + select { + case _, ok := <-errs: + if ok { + require.Fail(t, "channel should be closed") + } + case <-time.After(time.Second): + require.Fail(t, "channel should not block") + } + }) + } +}