|
| 1 | +package com |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/require" |
| 5 | + "io" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestWaitAsync(t *testing.T) { |
| 11 | + subtests := []struct { |
| 12 | + name string |
| 13 | + input WaiterFunc |
| 14 | + error error |
| 15 | + }{ |
| 16 | + {"no_error", func() error { return nil }, nil}, |
| 17 | + {"error", func() error { return io.EOF }, io.EOF}, |
| 18 | + {"sleep_no_error", func() error { time.Sleep(time.Second / 2); return nil }, nil}, |
| 19 | + {"sleep_error", func() error { time.Sleep(time.Second / 2); return io.EOF }, io.EOF}, |
| 20 | + } |
| 21 | + |
| 22 | + for _, st := range subtests { |
| 23 | + t.Run(st.name, func(t *testing.T) { |
| 24 | + errs := WaitAsync(st.input) |
| 25 | + require.NotNil(t, errs) |
| 26 | + |
| 27 | + if st.error != nil { |
| 28 | + select { |
| 29 | + case e, ok := <-errs: |
| 30 | + if !ok { |
| 31 | + require.Fail(t, "channel should not be closed, yet") |
| 32 | + } |
| 33 | + |
| 34 | + require.Equal(t, st.error, e) |
| 35 | + case <-time.After(time.Second): |
| 36 | + require.Fail(t, "channel should not block") |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + select { |
| 41 | + case _, ok := <-errs: |
| 42 | + if ok { |
| 43 | + require.Fail(t, "channel should be closed") |
| 44 | + } |
| 45 | + case <-time.After(time.Second): |
| 46 | + require.Fail(t, "channel should not block") |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
0 commit comments