|
| 1 | +package com |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/stretchr/testify/require" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCond_Broadcast(t *testing.T) { |
| 11 | + cond := NewCond(context.Background()) |
| 12 | + defer func() { _ = cond.Close() }() |
| 13 | + |
| 14 | + done := cond.Done() |
| 15 | + wait := cond.Wait() |
| 16 | + |
| 17 | + select { |
| 18 | + case <-done: |
| 19 | + require.Fail(t, "cond should not be closed, yet") |
| 20 | + case <-wait: |
| 21 | + require.Fail(t, "cond should not be ready, yet") |
| 22 | + case <-time.After(time.Second / 10): |
| 23 | + } |
| 24 | + |
| 25 | + cond.Broadcast() |
| 26 | + |
| 27 | + select { |
| 28 | + case <-done: |
| 29 | + require.Fail(t, "cond should still not be closed") |
| 30 | + case <-cond.Done(): |
| 31 | + require.Fail(t, "cond should not be closed for round 2, yet") |
| 32 | + case <-cond.Wait(): |
| 33 | + require.Fail(t, "cond should not be ready for round 2") |
| 34 | + case <-time.After(time.Second / 10): |
| 35 | + } |
| 36 | + |
| 37 | + select { |
| 38 | + case _, ok := <-wait: |
| 39 | + if ok { |
| 40 | + require.Fail(t, "cond ready channel should be closed") |
| 41 | + } |
| 42 | + case <-time.After(time.Second / 10): |
| 43 | + require.Fail(t, "cond should be ready") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestCond_Close(t *testing.T) { |
| 48 | + cond := NewCond(context.Background()) |
| 49 | + done := cond.Done() |
| 50 | + wait := cond.Wait() |
| 51 | + |
| 52 | + require.NoError(t, cond.Close()) |
| 53 | + |
| 54 | + select { |
| 55 | + case _, ok := <-done: |
| 56 | + if ok { |
| 57 | + require.Fail(t, "existing cond-closed channel should be closed") |
| 58 | + } |
| 59 | + case <-time.After(time.Second / 10): |
| 60 | + require.Fail(t, "cond should be closed") |
| 61 | + } |
| 62 | + |
| 63 | + select { |
| 64 | + case _, ok := <-cond.Done(): |
| 65 | + if ok { |
| 66 | + require.Fail(t, "new cond-closed channel should be closed") |
| 67 | + } |
| 68 | + case <-time.After(time.Second / 10): |
| 69 | + require.Fail(t, "cond should be still closed") |
| 70 | + } |
| 71 | + |
| 72 | + select { |
| 73 | + case <-wait: |
| 74 | + require.Fail(t, "cond should not be ready") |
| 75 | + case <-time.After(time.Second / 10): |
| 76 | + } |
| 77 | + |
| 78 | + require.Panics(t, func() { cond.Wait() }, "cond should panic on Wait after Close") |
| 79 | + require.Panics(t, func() { cond.Broadcast() }, "cond should panic on Broadcast after Close") |
| 80 | +} |
0 commit comments