Skip to content

Commit

Permalink
Test com.WaitAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Jul 8, 2024
1 parent c2d7928 commit f92a240
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions com/com_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
})
}
}

0 comments on commit f92a240

Please sign in to comment.