|
| 1 | +// Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | +// |
| 3 | +// This source code is licensed under the MIT license found in the |
| 4 | +// LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +package signaller |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "sync" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +var unitTestCustomSignal0 = fmt.Errorf("unit-test custom signal #0") |
| 18 | +var unitTestCustomSignal1 = fmt.Errorf("unit-test custom signal #1") |
| 19 | +var unitTestCustomSignal2 = fmt.Errorf("unit-test custom signal #2") |
| 20 | + |
| 21 | +func TestBackgroundContext(t *testing.T) { |
| 22 | + ctx := context.Background() |
| 23 | + |
| 24 | + var blocked bool |
| 25 | + select { |
| 26 | + case <-Until(ctx, nil): |
| 27 | + default: |
| 28 | + blocked = true |
| 29 | + } |
| 30 | + |
| 31 | + require.True(t, blocked) |
| 32 | + require.Nil(t, ReceivedSignals(ctx)) |
| 33 | +} |
| 34 | + |
| 35 | +func TestRace(t *testing.T) { |
| 36 | + ctx, sendSignal := WithSignal(context.Background(), unitTestCustomSignal0) |
| 37 | + var wg sync.WaitGroup |
| 38 | + readyToSelect := make(chan struct{}) |
| 39 | + startSelect := make(chan struct{}) |
| 40 | + wg.Add(1) |
| 41 | + go func() { |
| 42 | + defer wg.Done() |
| 43 | + ch := Until(ctx, nil) |
| 44 | + select { |
| 45 | + case <-ch: |
| 46 | + t.Fail() |
| 47 | + default: |
| 48 | + } |
| 49 | + close(readyToSelect) |
| 50 | + <-startSelect |
| 51 | + <-ch |
| 52 | + }() |
| 53 | + <-readyToSelect |
| 54 | + sendSignal() |
| 55 | + close(startSelect) |
| 56 | + wg.Wait() |
| 57 | +} |
| 58 | + |
| 59 | +func TestSendSignal(t *testing.T) { |
| 60 | + ctx, pauseFunc := WithSignal(context.Background(), unitTestCustomSignal0) |
| 61 | + require.NotNil(t, ctx) |
| 62 | + |
| 63 | + pauseFunc() |
| 64 | + |
| 65 | + <-Until(ctx, unitTestCustomSignal0) |
| 66 | + <-Until(ctx, nil) |
| 67 | + |
| 68 | + require.Nil(t, ctx.Err()) |
| 69 | + require.Equal(t, []error{unitTestCustomSignal0}, ReceivedSignals(ctx)) |
| 70 | + |
| 71 | + var canceled bool |
| 72 | + select { |
| 73 | + case <-ctx.Done(): |
| 74 | + canceled = true |
| 75 | + default: |
| 76 | + } |
| 77 | + require.False(t, canceled) |
| 78 | +} |
| 79 | + |
| 80 | +func TestSendMultipleSignals(t *testing.T) { |
| 81 | + ctx := context.Background() |
| 82 | + ctx, sendSignal0 := WithSignal(ctx, unitTestCustomSignal0) |
| 83 | + ctx, sendSignal1 := WithSignal(ctx, unitTestCustomSignal1) |
| 84 | + require.NotNil(t, ctx) |
| 85 | + |
| 86 | + sendSignal1() |
| 87 | + sendSignal0() |
| 88 | + sendSignal1() |
| 89 | + sendSignal0() |
| 90 | + |
| 91 | + <-Until(ctx, nil) |
| 92 | + <-Until(ctx, unitTestCustomSignal0) |
| 93 | + <-Until(ctx, unitTestCustomSignal1) |
| 94 | + |
| 95 | + require.Equal(t, []error{unitTestCustomSignal1, unitTestCustomSignal0, unitTestCustomSignal1, unitTestCustomSignal0}, ReceivedSignals(ctx)) |
| 96 | +} |
| 97 | + |
| 98 | +func TestGrandGrandGrandChild(t *testing.T) { |
| 99 | + type myUniqueType string |
| 100 | + ctx0, sendSignal0 := WithSignal(context.Background(), unitTestCustomSignal0) |
| 101 | + ctx1, _ := WithSignal(context.WithValue(ctx0, myUniqueType("someKey1"), "someValue1"), unitTestCustomSignal1) |
| 102 | + ctx2, _ := WithSignal(context.WithValue(ctx1, myUniqueType("someKey2"), "someValue2"), unitTestCustomSignal2) |
| 103 | + |
| 104 | + require.False(t, IsSignaledWith(ctx2, unitTestCustomSignal0)) |
| 105 | + sendSignal0() |
| 106 | + <-Until(ctx2, unitTestCustomSignal0) |
| 107 | + require.True(t, IsSignaledWith(ctx2, unitTestCustomSignal0)) |
| 108 | + require.False(t, IsSignaledWith(ctx2, unitTestCustomSignal1)) |
| 109 | + |
| 110 | + select { |
| 111 | + case <-Until(ctx2, unitTestCustomSignal1): |
| 112 | + require.FailNow(t, "unexpected closed chan for signal1") |
| 113 | + case <-Until(ctx2, unitTestCustomSignal2): |
| 114 | + require.FailNow(t, "unexpected closed chan for signal2") |
| 115 | + default: |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments