|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "golang.org/x/sync/errgroup" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAddedChannelOutput(t *testing.T) { |
| 11 | + multiplexChannel := make(chan int) |
| 12 | + outputChannel1 := make(chan int) |
| 13 | + outputChannel2 := make(chan int) |
| 14 | + outputChannel3 := make(chan int) |
| 15 | + |
| 16 | + multiplexer := NewChannelSpreader[int](multiplexChannel) |
| 17 | + |
| 18 | + multiplexer.AddChannel(outputChannel1) |
| 19 | + multiplexer.AddChannel(outputChannel2) |
| 20 | + multiplexer.AddChannel(outputChannel3) |
| 21 | + |
| 22 | + g, ctx := errgroup.WithContext(context.Background()) |
| 23 | + |
| 24 | + g.Go(func() error { |
| 25 | + return multiplexer.Run(ctx) |
| 26 | + }) |
| 27 | + |
| 28 | + want := 10 |
| 29 | + |
| 30 | + multiplexChannel <- want |
| 31 | + |
| 32 | + if got := <-outputChannel1; got != want { |
| 33 | + t.Errorf("got '%d' for 1st test channel, wanted '%d'", got, want) |
| 34 | + } |
| 35 | + if got := <-outputChannel2; got != want { |
| 36 | + t.Errorf("got '%d' for 2nd test channel, wanted '%d'", got, want) |
| 37 | + } |
| 38 | + if got := <-outputChannel3; got != want { |
| 39 | + t.Errorf("got '%d' for 3rd test channel, wanted '%d'", got, want) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestCreatedChannelOutput(t *testing.T) { |
| 44 | + multiplexChannel := make(chan int) |
| 45 | + |
| 46 | + multiplexer := NewChannelSpreader[int](multiplexChannel) |
| 47 | + |
| 48 | + outputChannel1 := multiplexer.NewChannel() |
| 49 | + outputChannel2 := multiplexer.NewChannel() |
| 50 | + outputChannel3 := multiplexer.NewChannel() |
| 51 | + |
| 52 | + g, ctx := errgroup.WithContext(context.Background()) |
| 53 | + |
| 54 | + g.Go(func() error { |
| 55 | + return multiplexer.Run(ctx) |
| 56 | + }) |
| 57 | + |
| 58 | + want := 10 |
| 59 | + |
| 60 | + multiplexChannel <- want |
| 61 | + |
| 62 | + if got := <-outputChannel1; got != want { |
| 63 | + t.Errorf("got '%d' for 1st test channel, wanted '%d'", got, want) |
| 64 | + } |
| 65 | + if got := <-outputChannel2; got != want { |
| 66 | + t.Errorf("got '%d' for 2nd test channel, wanted '%d'", got, want) |
| 67 | + } |
| 68 | + if got := <-outputChannel3; got != want { |
| 69 | + t.Errorf("got '%d' for 3rd test channel, wanted '%d'", got, want) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestClosedChannels(t *testing.T) { |
| 74 | + multiplexChannel := make(chan int) |
| 75 | + |
| 76 | + multiplexer := NewChannelSpreader[int](multiplexChannel) |
| 77 | + |
| 78 | + outputChannel1 := multiplexer.NewChannel() |
| 79 | + outputChannel2 := multiplexer.NewChannel() |
| 80 | + outputChannel3 := multiplexer.NewChannel() |
| 81 | + |
| 82 | + ctx, cancel := context.WithCancel(context.Background()) |
| 83 | + g, ctx := errgroup.WithContext(ctx) |
| 84 | + |
| 85 | + g.Go(func() error { |
| 86 | + return multiplexer.Run(ctx) |
| 87 | + }) |
| 88 | + |
| 89 | + cancel() |
| 90 | + |
| 91 | + select { |
| 92 | + case <-outputChannel1: |
| 93 | + case <-time.After(time.Second): |
| 94 | + t.Error("1st channel is still open, should be closed") |
| 95 | + } |
| 96 | + |
| 97 | + select { |
| 98 | + case <-outputChannel2: |
| 99 | + case <-time.After(time.Second): |
| 100 | + t.Error("2nd channel is still open, should be closed") |
| 101 | + } |
| 102 | + |
| 103 | + select { |
| 104 | + case <-outputChannel3: |
| 105 | + case <-time.After(time.Second): |
| 106 | + t.Error("3rd channel is still open, should be closed") |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments