From e10db57d9ca5a70df6db3850906640f99ebffeb0 Mon Sep 17 00:00:00 2001 From: Justin Perez Date: Tue, 19 Sep 2023 11:14:00 -0700 Subject: [PATCH] refactor: remove `BroadcastChannel` (#793) --- .../Channels/BroadcastChannelService.cs | 39 ------------ .../Channels/IBroadcastChannelService.cs | 31 ---------- .../BroadcastChannelServiceTests.cs | 60 ------------------- 3 files changed, 130 deletions(-) delete mode 100644 src/Microsoft.ComponentDetection.Common/Channels/BroadcastChannelService.cs delete mode 100644 src/Microsoft.ComponentDetection.Common/Channels/IBroadcastChannelService.cs delete mode 100644 test/Microsoft.ComponentDetection.Common.Tests/BroadcastChannelServiceTests.cs diff --git a/src/Microsoft.ComponentDetection.Common/Channels/BroadcastChannelService.cs b/src/Microsoft.ComponentDetection.Common/Channels/BroadcastChannelService.cs deleted file mode 100644 index 9efdbfd19..000000000 --- a/src/Microsoft.ComponentDetection.Common/Channels/BroadcastChannelService.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace Microsoft.ComponentDetection.Common.Channels; - -using System.Collections.Concurrent; -using System.Threading; -using System.Threading.Channels; -using System.Threading.Tasks; - -/// -public class BroadcastChannelService : IBroadcastChannelService -{ - private readonly ConcurrentBag> writers = new(); - - /// - public ChannelReader CreateBroadcastChannel() - { - var channel = Channel.CreateUnbounded(); - - this.writers.Add(channel.Writer); - return channel.Reader; - } - - /// - public async Task BroadcastMessageAsync(T message, CancellationToken cancellationToken = default) - { - foreach (var writer in this.writers) - { - await writer.WriteAsync(message, cancellationToken); - } - } - - /// - public void Complete() - { - foreach (var writer in this.writers) - { - writer.Complete(); - } - } -} diff --git a/src/Microsoft.ComponentDetection.Common/Channels/IBroadcastChannelService.cs b/src/Microsoft.ComponentDetection.Common/Channels/IBroadcastChannelService.cs deleted file mode 100644 index 9ba7b1f69..000000000 --- a/src/Microsoft.ComponentDetection.Common/Channels/IBroadcastChannelService.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace Microsoft.ComponentDetection.Common.Channels; - -using System.Threading; -using System.Threading.Channels; -using System.Threading.Tasks; - -/// -/// Service for broadcasting messages to all detectors. -/// -/// The type of message to broadcast. -public interface IBroadcastChannelService -{ - /// - /// Creates a broadcast channel that can be used to send messages to all detectors. - /// - /// A channel reader that can be used to send messages to all detectors. - ChannelReader CreateBroadcastChannel(); - - /// - /// Broadcasts a message to all detectors. - /// - /// The message to broadcast. - /// The cancellation token. - /// A representing the asynchronous operation. - Task BroadcastMessageAsync(T message, CancellationToken cancellationToken); - - /// - /// Completes all broadcast channels, indicating that no more messages will be sent. - /// - void Complete(); -} diff --git a/test/Microsoft.ComponentDetection.Common.Tests/BroadcastChannelServiceTests.cs b/test/Microsoft.ComponentDetection.Common.Tests/BroadcastChannelServiceTests.cs deleted file mode 100644 index a8aa88367..000000000 --- a/test/Microsoft.ComponentDetection.Common.Tests/BroadcastChannelServiceTests.cs +++ /dev/null @@ -1,60 +0,0 @@ -namespace Microsoft.ComponentDetection.Common.Tests; - -using System; -using System.Collections.Concurrent; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using FluentAssertions; -using Microsoft.ComponentDetection.Common.Channels; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -[TestClass] -[TestCategory("Governance/All")] -[TestCategory("Governance/ComponentDetection")] -public class BroadcastChannelServiceTests -{ - [TestMethod] - [Timeout(1000)] - public async Task BroadMessage_SendsToAllConsumersAsync() - { - var bc = new BroadcastChannelService(); - - var reader1 = bc.CreateBroadcastChannel(); - var reader2 = bc.CreateBroadcastChannel(); - - var results = new ConcurrentBag(); - var task1 = Task.Factory.StartNew( - async () => - { - await foreach (var msg in reader1.ReadAllAsync()) - { - results.Add(msg); - } - }, - CancellationToken.None, - TaskCreationOptions.None, - TaskScheduler.Default); - - var task2 = Task.Factory.StartNew( - async () => - { - await foreach (var msg in reader2.ReadAllAsync()) - { - results.Add(msg); - } - }, - CancellationToken.None, - TaskCreationOptions.None, - TaskScheduler.Default); - - await bc.BroadcastMessageAsync(10); - await bc.BroadcastMessageAsync(20); - - bc.Complete(); - - await Task.WhenAll(task1, task2); - - results.Should().BeEquivalentTo(new[] { 10, 20, 10, 20 }); - } -}