├── .github ├── FUNDING.yml ├── renovate.json ├── release.yml └── workflows │ └── build.yml ├── images └── icon.png ├── tests ├── TurnerSoftware.Aqueduct.Tests │ ├── Usings.cs │ ├── TurnerSoftware.Aqueduct.Tests.csproj │ └── PipeBifurcationTests.cs └── Directory.Build.props ├── src ├── TurnerSoftware.Aqueduct │ ├── BifurcationException.cs │ ├── TurnerSoftware.Aqueduct.csproj │ ├── BifurcationSourceConfig.cs │ ├── BifurcationExtensionMethods.cs │ ├── PipeBifurcation.cs │ └── BifurcationTargetConfig.cs └── Directory.Build.props ├── CodeCoverage.runsettings ├── LICENSE.txt ├── README.md ├── .gitattributes ├── TurnerSoftware.Aqueduct.sln ├── .editorconfig └── .gitignore /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: Turnerj -------------------------------------------------------------------------------- /images/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TurnerSoftware/Aqueduct/HEAD/images/icon.png -------------------------------------------------------------------------------- /tests/TurnerSoftware.Aqueduct.Tests/Usings.cs: -------------------------------------------------------------------------------- 1 | global using FluentAssertions; 2 | global using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | -------------------------------------------------------------------------------- /tests/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Latest 5 | 6 | 7 | -------------------------------------------------------------------------------- /.github/renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "github>TurnerSoftware/.github:renovate-shared" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.github/release.yml: -------------------------------------------------------------------------------- 1 | changelog: 2 | exclude: 3 | labels: 4 | - ignore-for-release 5 | categories: 6 | - title: ⚠ Breaking Changes 7 | labels: 8 | - breaking-change 9 | - title: Features and Improvements 10 | labels: 11 | - enhancement 12 | - title: Bug Fixes 13 | labels: 14 | - bug 15 | - title: Dependency Updates 16 | labels: 17 | - dependencies 18 | - title: Other Changes 19 | labels: 20 | - "*" -------------------------------------------------------------------------------- /src/TurnerSoftware.Aqueduct/BifurcationException.cs: -------------------------------------------------------------------------------- 1 | namespace TurnerSoftware.Aqueduct; 2 | 3 | /// 4 | /// An exception specifically for capturing errors that arise from bifurcation. 5 | /// 6 | public class BifurcationException : Exception 7 | { 8 | internal BifurcationException() : base() { } 9 | internal BifurcationException(string? message) : base(message) { } 10 | internal BifurcationException(string? message, Exception? innerException) : base(message, innerException) { } 11 | } 12 | -------------------------------------------------------------------------------- /CodeCoverage.runsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | cobertura 8 | [TurnerSoftware.Aqueduct.Tests]* 9 | [TurnerSoftware.Aqueduct]*,[TurnerSoftware.Aqueduct.*]* 10 | Obsolete,GeneratedCodeAttribute 11 | true 12 | true 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/TurnerSoftware.Aqueduct/TurnerSoftware.Aqueduct.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | TurnerSoftware.Aqueduct 6 | TurnerSoftware.Aqueduct 7 | Utilities and extension methods for working with streams and pipes 8 | $(PackageBaseTags) 9 | James Turner 10 | 11 | enable 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /tests/TurnerSoftware.Aqueduct.Tests/TurnerSoftware.Aqueduct.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Turner Software 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | TurnerSoftware.Aqueduct 5 | 6 | Turner Software 7 | 8 | $(AssemblyName) 9 | true 10 | MIT 11 | icon.png 12 | https://github.com/TurnerSoftware/Aqueduct 13 | stream;pipe;reader 14 | 15 | 16 | true 17 | true 18 | embedded 19 | 20 | Latest 21 | enable 22 | true 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | ![Icon](images/icon.png) 4 | # Aqueduct 5 | Utilities and extension methods for working with streams and pipes 6 | 7 | ![Build](https://img.shields.io/github/actions/workflow/status/TurnerSoftware/aqueduct/build.yml?branch=main) 8 | [![Codecov](https://img.shields.io/codecov/c/github/turnersoftware/aqueduct/main.svg)](https://codecov.io/gh/TurnerSoftware/Aqueduct) 9 | [![NuGet](https://img.shields.io/nuget/v/TurnerSoftware.Aqueduct.svg)](https://www.nuget.org/packages/TurnerSoftware.Aqueduct/) 10 |
11 | 12 | ## Overview 13 | Aqueduct provides some useful, albeit niche, utilities for working with streams and pipes. 14 | 15 | ### Pipe/Stream Bifurcation 16 | 17 | Allows you to read from a single pipe or stream into multiple targets. 18 | This is useful for cases where you can't buffer the original stream into memory or you're working with a source you can't seek. 19 | Internally it uses individual pipes per target to allow independent processing and minimal memory overhead. 20 | 21 | Each bifurcation target has individual options for: 22 | - The reader which processes the data 23 | - An exception handler triggered on failure of _any_ target 24 | - Control of the number of bytes for blocking/resuming writes to the target 25 | - The maximum number of bytes to write to the specific target 26 | 27 | Additionally, the bifurcation process overall has options for: 28 | - The minimum read buffer size of the source pipe or stream 29 | - Whether to leave the stream open after bifurcation 30 | - Allow exceptions from readers to bubble out to the calling code 31 | - Cancellation token for reading/writing process 32 | 33 | Example usage of pipe/stream bifurcation: 34 | 35 | ```csharp 36 | await myStream.BifurcatedReadAsync( 37 | new BifurcationTargetConfig( 38 | async (Stream stream, CancellationToken cancellationToken) => 39 | { 40 | using var fileStream = File.OpenWrite("some-file-path.bin"); 41 | await stream.CopyToAsync(fileStream); 42 | }, 43 | maxTotalBytes: 1024 44 | ), 45 | new BifurcationTargetConfig( 46 | async (PipeReader reader, CancellationToken cancellationToken) => 47 | { 48 | await someService.ProcessData(reader); 49 | } 50 | ) 51 | ); 52 | ``` 53 | 54 | ## Licensing and Support 55 | 56 | Aqueduct is licensed under the MIT license. It is free to use in personal and commercial projects. 57 | 58 | There are [support plans](https://turnersoftware.com.au/support-plans) available that cover all active [Turner Software OSS projects](https://github.com/TurnerSoftware). 59 | Support plans provide private email support, expert usage advice for our projects, priority bug fixes and more. 60 | These support plans help fund our OSS commitments to provide better software for everyone. 61 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /TurnerSoftware.Aqueduct.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.5.33209.295 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TurnerSoftware.Aqueduct", "src\TurnerSoftware.Aqueduct\TurnerSoftware.Aqueduct.csproj", "{8BB5B939-1425-4AEA-9438-29E20C5C5309}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2AA4A059-93AC-4F2B-A62F-6F3C935A3203}" 9 | ProjectSection(SolutionItems) = preProject 10 | src\Directory.Build.props = src\Directory.Build.props 11 | EndProjectSection 12 | EndProject 13 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{A67C346D-6E1C-4186-90D1-94FF299B495B}" 14 | EndProject 15 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TurnerSoftware.Aqueduct.Tests", "tests\TurnerSoftware.Aqueduct.Tests\TurnerSoftware.Aqueduct.Tests.csproj", "{D4A86C4F-779E-4E70-AC8A-45BA2D419AEE}" 16 | EndProject 17 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "global", "global", "{6BA89924-AC44-48A7-9625-B31BE5A0AB3F}" 18 | ProjectSection(SolutionItems) = preProject 19 | .editorconfig = .editorconfig 20 | .gitignore = .gitignore 21 | CodeCoverage.runsettings = CodeCoverage.runsettings 22 | License.txt = License.txt 23 | README.md = README.md 24 | EndProjectSection 25 | EndProject 26 | Global 27 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 28 | Debug|Any CPU = Debug|Any CPU 29 | Release|Any CPU = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 32 | {8BB5B939-1425-4AEA-9438-29E20C5C5309}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {8BB5B939-1425-4AEA-9438-29E20C5C5309}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {8BB5B939-1425-4AEA-9438-29E20C5C5309}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {8BB5B939-1425-4AEA-9438-29E20C5C5309}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {D4A86C4F-779E-4E70-AC8A-45BA2D419AEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {D4A86C4F-779E-4E70-AC8A-45BA2D419AEE}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {D4A86C4F-779E-4E70-AC8A-45BA2D419AEE}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {D4A86C4F-779E-4E70-AC8A-45BA2D419AEE}.Release|Any CPU.Build.0 = Release|Any CPU 40 | EndGlobalSection 41 | GlobalSection(SolutionProperties) = preSolution 42 | HideSolutionNode = FALSE 43 | EndGlobalSection 44 | GlobalSection(NestedProjects) = preSolution 45 | {8BB5B939-1425-4AEA-9438-29E20C5C5309} = {2AA4A059-93AC-4F2B-A62F-6F3C935A3203} 46 | {D4A86C4F-779E-4E70-AC8A-45BA2D419AEE} = {A67C346D-6E1C-4186-90D1-94FF299B495B} 47 | EndGlobalSection 48 | GlobalSection(ExtensibilityGlobals) = postSolution 49 | SolutionGuid = {57E7526E-4A07-4AE4-8E56-5D577F4AD7AB} 50 | EndGlobalSection 51 | EndGlobal 52 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Based on the EditorConfig from Roslyn 2 | # top-most EditorConfig file 3 | root = true 4 | 5 | [*.cs] 6 | indent_style = tab 7 | 8 | # Sort using and Import directives with System.* appearing first 9 | dotnet_sort_system_directives_first = true 10 | # Avoid "this." and "Me." if not necessary 11 | dotnet_style_qualification_for_field = false:suggestion 12 | dotnet_style_qualification_for_property = false:suggestion 13 | dotnet_style_qualification_for_method = false:suggestion 14 | dotnet_style_qualification_for_event = false:suggestion 15 | 16 | # Use language keywords instead of framework type names for type references 17 | dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion 18 | dotnet_style_predefined_type_for_member_access = true:suggestion 19 | 20 | # Suggest more modern language features when available 21 | dotnet_style_object_initializer = true:suggestion 22 | dotnet_style_collection_initializer = true:suggestion 23 | dotnet_style_coalesce_expression = true:suggestion 24 | dotnet_style_null_propagation = true:suggestion 25 | dotnet_style_explicit_tuple_names = true:suggestion 26 | 27 | # Prefer "var" everywhere 28 | csharp_style_var_for_built_in_types = true:suggestion 29 | csharp_style_var_when_type_is_apparent = true:suggestion 30 | csharp_style_var_elsewhere = true:suggestion 31 | 32 | # Prefer method-like constructs to have a block body 33 | csharp_style_expression_bodied_methods = false:none 34 | csharp_style_expression_bodied_constructors = false:none 35 | csharp_style_expression_bodied_operators = false:none 36 | 37 | # Prefer property-like constructs to have an expression-body 38 | csharp_style_expression_bodied_properties = when_on_single_line:suggestion 39 | csharp_style_expression_bodied_indexers = true:none 40 | csharp_style_expression_bodied_accessors = when_on_single_line:suggestion 41 | 42 | # Suggest more modern language features when available 43 | csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion 44 | csharp_style_pattern_matching_over_as_with_null_check = true:suggestion 45 | csharp_style_inlined_variable_declaration = true:suggestion 46 | csharp_style_throw_expression = true:suggestion 47 | csharp_style_conditional_delegate_call = true:suggestion 48 | 49 | # Newline settings 50 | csharp_new_line_before_open_brace = all 51 | csharp_new_line_before_else = true 52 | csharp_new_line_before_catch = true 53 | csharp_new_line_before_finally = true 54 | csharp_new_line_before_members_in_object_initializers = true 55 | csharp_new_line_before_members_in_anonymous_types = true 56 | 57 | # Misc 58 | csharp_space_after_keywords_in_control_flow_statements = true 59 | csharp_space_between_method_declaration_parameter_list_parentheses = false 60 | csharp_space_between_method_call_parameter_list_parentheses = false 61 | csharp_space_between_parentheses = false 62 | csharp_preserve_single_line_statements = false 63 | csharp_preserve_single_line_blocks = true 64 | csharp_indent_case_contents = true 65 | csharp_indent_switch_labels = true 66 | csharp_indent_labels = no_change 67 | 68 | # Custom naming conventions 69 | dotnet_naming_rule.non_field_members_must_be_capitalized.symbols = non_field_member_symbols 70 | dotnet_naming_symbols.non_field_member_symbols.applicable_kinds = property,method,event,delegate 71 | dotnet_naming_symbols.non_field_member_symbols.applicable_accessibilities = * 72 | 73 | dotnet_naming_rule.non_field_members_must_be_capitalized.style = pascal_case_style 74 | dotnet_naming_style.pascal_case_style.capitalization = pascal_case 75 | 76 | dotnet_naming_rule.non_field_members_must_be_capitalized.severity = suggestion -------------------------------------------------------------------------------- /src/TurnerSoftware.Aqueduct/BifurcationSourceConfig.cs: -------------------------------------------------------------------------------- 1 | namespace TurnerSoftware.Aqueduct; 2 | 3 | /// 4 | /// Manages the configuration for bifurcation. 5 | /// 6 | public class BifurcationSourceConfig 7 | { 8 | internal const int DefaultMinReadBufferSize = 4096; 9 | 10 | /// 11 | /// The default configuration for . 12 | /// 13 | public static readonly BifurcationSourceConfig DefaultConfig = new(); 14 | 15 | /// 16 | /// The minimum read buffer size before writing data to the targets. When -1 is set, there is no minimum read buffer size. 17 | /// 18 | public int MinReadBufferSize { get; } 19 | /// 20 | /// Whether to bubble exceptions during bifurcation to the calling code. 21 | /// 22 | public bool BubbleExceptions { get; } 23 | /// 24 | /// The token to monitor for cancellation requests. 25 | /// 26 | public CancellationToken CancellationToken { get; } 27 | 28 | /// 29 | /// Creates a new . 30 | /// 31 | /// The minimum read buffer size before writing data to the targets. Use -1 to specify no minimum read buffer size. 32 | /// Whether to bubble exceptions during bifurcation to the calling code. 33 | /// The token to monitor for cancellation requests. 34 | /// 35 | public BifurcationSourceConfig( 36 | int minReadBufferSize = DefaultMinReadBufferSize, 37 | bool bubbleExceptions = true, 38 | CancellationToken cancellationToken = default 39 | ) 40 | { 41 | if (minReadBufferSize != -1 && minReadBufferSize <= 0) 42 | { 43 | throw new ArgumentException($"Invalid value for {nameof(MinReadBufferSize)}. Must be a value greater than 0, or if there is no restriction, -1.", nameof(minReadBufferSize)); 44 | } 45 | 46 | MinReadBufferSize = minReadBufferSize; 47 | BubbleExceptions = bubbleExceptions; 48 | CancellationToken = cancellationToken; 49 | } 50 | } 51 | 52 | /// 53 | /// Manages the configuration for stream bifurcation. 54 | /// 55 | public class StreamBifurcationSourceConfig : BifurcationSourceConfig 56 | { 57 | /// 58 | /// The default configuration for . 59 | /// 60 | public static readonly StreamBifurcationSourceConfig DefaultStreamConfig = new(); 61 | 62 | /// 63 | /// Whether to leave the stream open after reading has completed. 64 | /// 65 | public bool LeaveOpen { get; } 66 | 67 | /// 68 | /// Creates a new . 69 | /// 70 | /// Whether to leave the stream open after reading has completed. 71 | /// The minimum read buffer size before writing data to the targets. Use -1 to specify no minimum read buffer size. 72 | /// Whether to bubble exceptions during bifurcation to the calling code. 73 | /// The token to monitor for cancellation requests. 74 | /// 75 | public StreamBifurcationSourceConfig( 76 | bool leaveOpen = false, 77 | int minReadBufferSize = DefaultMinReadBufferSize, 78 | bool bubbleExceptions = true, 79 | CancellationToken cancellationToken = default 80 | ) : base(minReadBufferSize, bubbleExceptions, cancellationToken) 81 | { 82 | LeaveOpen = leaveOpen; 83 | } 84 | } -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | release: 8 | types: [ published ] 9 | 10 | env: 11 | # Disable the .NET logo in the console output. 12 | DOTNET_NOLOGO: true 13 | # Disable the .NET first time experience to skip caching NuGet packages and speed up the build. 14 | DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true 15 | # Disable sending .NET CLI telemetry to Microsoft. 16 | DOTNET_CLI_TELEMETRY_OPTOUT: true 17 | 18 | BUILD_ARTIFACT_PATH: ${{github.workspace}}/build-artifacts 19 | 20 | jobs: 21 | build: 22 | name: Build ${{matrix.os}} 23 | runs-on: ${{matrix.os}} 24 | strategy: 25 | matrix: 26 | os: [ubuntu-latest, windows-latest, macOS-latest] 27 | steps: 28 | - name: Checkout 29 | uses: actions/checkout@v4 30 | - name: Setup dotnet SDK 31 | uses: actions/setup-dotnet@v4 32 | with: 33 | dotnet-version: | 34 | 8.0.x 35 | - name: Install dependencies 36 | run: dotnet restore 37 | - name: Build 38 | run: dotnet build --no-restore -c Release 39 | - name: Test with Coverage 40 | run: dotnet test --no-restore --logger trx --results-directory ${{env.BUILD_ARTIFACT_PATH}}/coverage --collect "XPlat Code Coverage" --settings CodeCoverage.runsettings /p:SkipBuildVersioning=true 41 | - name: Pack 42 | run: dotnet pack --no-build -c Release /p:PackageOutputPath=${{env.BUILD_ARTIFACT_PATH}} 43 | - name: Publish artifacts 44 | uses: actions/upload-artifact@v4 45 | with: 46 | name: ${{matrix.os}} 47 | path: ${{env.BUILD_ARTIFACT_PATH}} 48 | 49 | coverage: 50 | name: Process code coverage 51 | runs-on: ubuntu-latest 52 | needs: build 53 | steps: 54 | - name: Checkout 55 | uses: actions/checkout@v4 56 | - name: Download coverage reports 57 | uses: actions/download-artifact@v4 58 | - name: Install ReportGenerator tool 59 | run: dotnet tool install -g dotnet-reportgenerator-globaltool 60 | - name: Prepare coverage reports 61 | run: reportgenerator -reports:*/coverage/*/coverage.cobertura.xml -targetdir:./ -reporttypes:Cobertura 62 | - name: Upload coverage report 63 | uses: codecov/codecov-action@v5.3.1 64 | with: 65 | file: Cobertura.xml 66 | fail_ci_if_error: false 67 | - name: Save combined coverage report as artifact 68 | uses: actions/upload-artifact@v4 69 | with: 70 | name: coverage-report 71 | path: Cobertura.xml 72 | 73 | push-to-github-packages: 74 | name: 'Push GitHub Packages' 75 | needs: build 76 | if: github.ref == 'refs/heads/main' || github.event_name == 'release' 77 | environment: 78 | name: 'GitHub Packages' 79 | url: https://github.com/TurnerSoftware/Aqueduct/packages 80 | permissions: 81 | packages: write 82 | runs-on: ubuntu-latest 83 | steps: 84 | - name: 'Download build' 85 | uses: actions/download-artifact@v4 86 | with: 87 | name: 'ubuntu-latest' 88 | - name: 'Add NuGet source' 89 | run: dotnet nuget add source https://nuget.pkg.github.com/TurnerSoftware/index.json --name GitHub --username Turnerj --password ${{secrets.GITHUB_TOKEN}} --store-password-in-clear-text 90 | - name: 'Upload NuGet package' 91 | run: dotnet nuget push *.nupkg --api-key ${{secrets.GH_PACKAGE_REGISTRY_API_KEY}} --source GitHub --skip-duplicate 92 | 93 | push-to-nuget: 94 | name: 'Push NuGet Packages' 95 | needs: build 96 | if: github.event_name == 'release' 97 | environment: 98 | name: 'NuGet' 99 | url: https://www.nuget.org/packages/TurnerSoftware.Aqueduct 100 | runs-on: ubuntu-latest 101 | steps: 102 | - name: 'Download build' 103 | uses: actions/download-artifact@v4 104 | with: 105 | name: 'ubuntu-latest' 106 | - name: 'Upload NuGet package' 107 | run: dotnet nuget push *.nupkg --source https://api.nuget.org/v3/index.json --skip-duplicate --api-key ${{secrets.NUGET_API_KEY}} 108 | -------------------------------------------------------------------------------- /src/TurnerSoftware.Aqueduct/BifurcationExtensionMethods.cs: -------------------------------------------------------------------------------- 1 | using System.IO.Pipelines; 2 | 3 | namespace TurnerSoftware.Aqueduct; 4 | 5 | /// 6 | /// Extension methods specific for bifurcation. 7 | /// 8 | public static class BifurcationExtensionMethods 9 | { 10 | /// 11 | /// Performs bifurcation with , splitting the resulting data into multiple . 12 | /// 13 | /// The source to read from. 14 | /// The targets to provide the bifurcated data to. 15 | /// A list of results from the targets, in the order the targets were provided. 16 | public static Task> BifurcatedReadAsync(this PipeReader sourceReader, params BifurcationTargetConfig[] targetConfigs) 17 | => BifurcatedReadAsync(sourceReader, BifurcationSourceConfig.DefaultConfig, targetConfigs); 18 | /// 19 | /// Performs bifurcation with , splitting the resulting data into multiple . 20 | /// 21 | /// The source to read from. 22 | /// Source-specific configuration for reading. 23 | /// The targets to provide the bifurcated data to. 24 | /// A list of results from the targets, in the order the targets were provided. 25 | public static Task> BifurcatedReadAsync(this PipeReader sourceReader, BifurcationSourceConfig sourceConfig, params BifurcationTargetConfig[] targetConfigs) 26 | => PipeBifurcation.BifurcatedReadAsync(sourceReader, sourceConfig, targetConfigs); 27 | 28 | /// 29 | /// Performs bifurcation with , splitting the resulting data into multiple . 30 | /// 31 | /// The source to read from. 32 | /// The targets to provide the bifurcated data to. 33 | /// A list of results from the targets, in the order the targets were provided. 34 | public static Task> BifurcatedReadAsync(this Stream sourceStream, params BifurcationTargetConfig[] targetConfigs) 35 | => BifurcatedReadAsync(sourceStream, StreamBifurcationSourceConfig.DefaultStreamConfig, targetConfigs); 36 | /// 37 | /// Performs bifurcation with , splitting the resulting data into multiple . 38 | /// 39 | /// The source to read from. 40 | /// Source-specific configuration for reading. 41 | /// The targets to provide the bifurcated data to. 42 | /// A list of results from the targets, in the order the targets were provided. 43 | public static Task> BifurcatedReadAsync(this Stream sourceStream, StreamBifurcationSourceConfig sourceConfig, params BifurcationTargetConfig[] targetConfigs) 44 | { 45 | var sourceReader = PipeReader.Create(sourceStream, new StreamPipeReaderOptions(leaveOpen: sourceConfig.LeaveOpen)); 46 | return PipeBifurcation.BifurcatedReadAsync(sourceReader, sourceConfig, targetConfigs); 47 | } 48 | 49 | /// 50 | /// Performs bifurcation with , splitting the resulting data into multiple . 51 | /// 52 | /// The source to read from. 53 | /// The targets to provide the bifurcated data to. 54 | /// 55 | public static Task BifurcatedReadAsync(this PipeReader sourceReader, params BifurcationTargetConfig[] targetConfigs) 56 | => BifurcatedReadAsync(sourceReader, BifurcationSourceConfig.DefaultConfig, targetConfigs); 57 | /// 58 | /// Performs bifurcation with , splitting the resulting data into multiple . 59 | /// 60 | /// The source to read from. 61 | /// Source-specific configuration for reading. 62 | /// The targets to provide the bifurcated data to. 63 | /// 64 | public static Task BifurcatedReadAsync(this PipeReader sourceReader, BifurcationSourceConfig sourceConfig, params BifurcationTargetConfig[] targetConfigs) 65 | => PipeBifurcation.BifurcatedReadAsync(sourceReader, sourceConfig, targetConfigs); 66 | 67 | /// 68 | /// Performs bifurcation with , splitting the resulting data into multiple . 69 | /// 70 | /// The source to read from. 71 | /// The targets to provide the bifurcated data to. 72 | /// 73 | public static Task BifurcatedReadAsync(this Stream sourceStream, params BifurcationTargetConfig[] targetConfigs) 74 | => BifurcatedReadAsync(sourceStream, StreamBifurcationSourceConfig.DefaultStreamConfig, targetConfigs); 75 | /// 76 | /// Performs bifurcation with , splitting the resulting data into multiple . 77 | /// 78 | /// The source to read from. 79 | /// Source-specific configuration for reading. 80 | /// The targets to provide the bifurcated data to. 81 | /// 82 | public static Task BifurcatedReadAsync(this Stream sourceStream, StreamBifurcationSourceConfig sourceConfig, params BifurcationTargetConfig[] targetConfigs) 83 | { 84 | var sourceReader = PipeReader.Create(sourceStream, new StreamPipeReaderOptions(leaveOpen: sourceConfig.LeaveOpen)); 85 | return PipeBifurcation.BifurcatedReadAsync(sourceReader, sourceConfig, targetConfigs); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/TurnerSoftware.Aqueduct/PipeBifurcation.cs: -------------------------------------------------------------------------------- 1 | using System.Buffers; 2 | using System.IO.Pipelines; 3 | 4 | namespace TurnerSoftware.Aqueduct; 5 | 6 | internal static class PipeBifurcation 7 | { 8 | private class BifurcationState 9 | { 10 | private readonly Pipe Pipe; 11 | 12 | private Task? ReaderTask; 13 | private int RemainingBytes; 14 | 15 | public readonly BifurcationTargetConfig Config; 16 | 17 | public TResult? Result { get; private set; } 18 | 19 | public BifurcationState(BifurcationTargetConfig config) 20 | { 21 | Config = config; 22 | 23 | Pipe = new Pipe(new PipeOptions( 24 | pauseWriterThreshold: config.BlockAfter, 25 | resumeWriterThreshold: config.ResumeAfter 26 | )); 27 | 28 | RemainingBytes = config.MaxTotalBytes; 29 | } 30 | 31 | public bool IsCompleted { get; private set; } 32 | 33 | private async Task RunReaderWithCleanup(CancellationToken cancellationToken) 34 | { 35 | try 36 | { 37 | var result = await Config.Reader(Pipe.Reader, cancellationToken); 38 | await Pipe.Reader.CompleteAsync(); 39 | return result; 40 | } 41 | catch (Exception ex) 42 | { 43 | await Pipe.Reader.CompleteAsync(ex); 44 | throw; 45 | } 46 | } 47 | 48 | public void StartReader(CancellationToken cancellationToken) 49 | { 50 | try 51 | { 52 | ReaderTask = RunReaderWithCleanup(cancellationToken); 53 | } 54 | catch (Exception ex) 55 | { 56 | ReaderTask = Task.FromException(ex); 57 | } 58 | } 59 | 60 | /// 61 | /// Using the , writes as much as configured to the bifurcation target. 62 | /// 63 | /// 64 | /// 65 | /// Whether the target can still be written to. 66 | public async ValueTask WriteAsync(ReadOnlySequence buffer, CancellationToken cancellationToken) 67 | { 68 | if (IsCompleted) 69 | { 70 | return false; 71 | } 72 | 73 | if (ReaderTask is not null) 74 | { 75 | //Await faulted readers to correctly bubble exceptions 76 | if (ReaderTask.IsFaulted) 77 | { 78 | await ReaderTask; 79 | } 80 | 81 | //If the reader task finishes early for some other reason 82 | if (ReaderTask.IsCompleted) 83 | { 84 | return false; 85 | } 86 | } 87 | 88 | var bytesToRead = (int)buffer.Length; 89 | if (RemainingBytes != -1) 90 | { 91 | bytesToRead = Math.Min(RemainingBytes, bytesToRead); 92 | } 93 | 94 | var destination = Pipe.Writer.GetMemory(bytesToRead); 95 | buffer.Slice(0, bytesToRead).CopyTo(destination.Span); 96 | 97 | Pipe.Writer.Advance(bytesToRead); 98 | 99 | var flushResult = await Pipe.Writer.FlushAsync(cancellationToken); 100 | 101 | if (RemainingBytes != -1) 102 | { 103 | RemainingBytes -= bytesToRead; 104 | if (RemainingBytes == 0) 105 | { 106 | return false; 107 | } 108 | } 109 | 110 | return !flushResult.IsCompleted; 111 | } 112 | 113 | /// 114 | /// Completes the bifurcation target and awaits the reader. Any exceptions the reader throws will bubble out. 115 | /// 116 | /// 117 | public async Task CompleteAsync() 118 | { 119 | if (IsCompleted) 120 | { 121 | return Result; 122 | } 123 | 124 | IsCompleted = true; 125 | await Pipe.Writer.CompleteAsync(); 126 | 127 | //Run the task to completion 128 | if (ReaderTask is not null) 129 | { 130 | Result = await ReaderTask; 131 | } 132 | 133 | return Result; 134 | } 135 | 136 | /// 137 | /// Completes the bifurcation target in a faulted state, awaiting the reader and exception handler. 138 | /// No exceptions from either the reader or exception handler will bubble. 139 | /// 140 | /// The exception used to trigger the faulted state. 141 | /// 142 | public async Task CompleteWithExceptionAsync(Exception exception) 143 | { 144 | await Pipe.Writer.CompleteAsync(exception); 145 | if (ReaderTask is not null) 146 | { 147 | //Ensure that the reader task has completed execution (faulted or not) 148 | if (!ReaderTask.IsFaulted) 149 | { 150 | try 151 | { 152 | Result = await ReaderTask; 153 | } 154 | catch 155 | { 156 | //Ignore any exceptions 157 | } 158 | } 159 | 160 | //Trigger any custom exception handler 161 | if (Config.ExceptionHandler is not null) 162 | { 163 | try 164 | { 165 | await Config.ExceptionHandler(exception); 166 | } 167 | catch 168 | { 169 | //Ignore any exceptions 170 | } 171 | } 172 | } 173 | 174 | return Result; 175 | } 176 | } 177 | 178 | public static async Task> BifurcatedReadAsync(PipeReader sourceReader, BifurcationSourceConfig sourceConfig, params BifurcationTargetConfig[] targetConfigs) 179 | { 180 | if (targetConfigs.Length == 0) 181 | { 182 | throw new ArgumentException("No target configurations to bifurcate the source reader to", nameof(targetConfigs)); 183 | } 184 | 185 | var earlyCompletedTargets = 0; 186 | var targets = new BifurcationState[targetConfigs.Length]; 187 | var results = new TResult?[targetConfigs.Length]; 188 | 189 | for (var i = 0; i < targetConfigs.Length; i++) 190 | { 191 | targets[i] = new(targetConfigs[i]); 192 | targets[i].StartReader(sourceConfig.CancellationToken); 193 | } 194 | 195 | try 196 | { 197 | while (true) 198 | { 199 | var result = await sourceReader.ReadAsync(sourceConfig.CancellationToken); 200 | var buffer = result.Buffer; 201 | 202 | if (buffer.IsEmpty && result.IsCompleted) 203 | { 204 | break; 205 | } 206 | 207 | //Ensure a minimum buffer size (if configured) 208 | if (!result.IsCompleted && sourceConfig.MinReadBufferSize != -1 && buffer.Length < sourceConfig.MinReadBufferSize) 209 | { 210 | sourceReader.AdvanceTo(buffer.Start, buffer.End); 211 | continue; 212 | } 213 | 214 | for (var i = 0; i < targets.Length; i++) 215 | { 216 | var target = targets[i]; 217 | if (target.IsCompleted) 218 | { 219 | continue; 220 | } 221 | 222 | var canKeepWriting = await target.WriteAsync(buffer, sourceConfig.CancellationToken); 223 | if (!canKeepWriting) 224 | { 225 | await target.CompleteAsync(); 226 | earlyCompletedTargets++; 227 | } 228 | } 229 | 230 | //Exit reading early if all targets have completed 231 | if (earlyCompletedTargets == targets.Length) 232 | { 233 | break; 234 | } 235 | 236 | sourceReader.AdvanceTo(buffer.End); 237 | } 238 | 239 | //Complete reader and all branch writers 240 | await sourceReader.CompleteAsync(); 241 | for (var i = 0; i < targets.Length; i++) 242 | { 243 | var target = targets[i]; 244 | results[i] = await target.CompleteAsync(); 245 | } 246 | 247 | return results; 248 | } 249 | catch (Exception innerException) 250 | { 251 | var exception = new BifurcationException("An exception occurred during bifurcation", innerException); 252 | 253 | await sourceReader.CompleteAsync(exception); 254 | for (var i = 0; i < targets.Length; i++) 255 | { 256 | var target = targets[i]; 257 | results[i] = await target.CompleteWithExceptionAsync(exception); 258 | } 259 | 260 | if (sourceConfig.BubbleExceptions) 261 | { 262 | throw exception; 263 | } 264 | 265 | return results; 266 | } 267 | } 268 | } -------------------------------------------------------------------------------- /src/TurnerSoftware.Aqueduct/BifurcationTargetConfig.cs: -------------------------------------------------------------------------------- 1 | using System.IO.Pipelines; 2 | 3 | namespace TurnerSoftware.Aqueduct; 4 | 5 | /// 6 | /// Manages the configuration for a specific bifurcation target. 7 | /// 8 | public class BifurcationTargetConfig 9 | { 10 | internal const int DefaultBlockAfter = 32768; 11 | internal const int DefaultResumeAfter = 16384; 12 | internal const int DefaultMaxTotalBytes = -1; 13 | 14 | /// 15 | /// The reader function that will handle this specific bifurcation target. 16 | /// 17 | public Func> Reader { get; } 18 | /// 19 | /// The individual exception handler for this bifurcation target when exceptions occur in any target during bifurcation. 20 | /// 21 | public Func? ExceptionHandler { get; } 22 | /// 23 | /// The number of unread bytes before writing will block to the bifurcation target. 24 | /// 25 | public int BlockAfter { get; } 26 | /// 27 | /// The number of unread bytes before resuming writing to the bifurcation target. 28 | /// 29 | public int ResumeAfter { get; } 30 | /// 31 | /// The max number of bytes to write to the bifurcation target. 32 | /// 33 | public int MaxTotalBytes { get; } 34 | 35 | /// 36 | /// Creates a new for -based readers. 37 | /// 38 | /// The reader function that will handle this specific bifurcation target. 39 | /// The individual exception handler for this bifurcation target when exceptions occur in any target during bifurcation. 40 | /// The number of unread bytes before writing will block to the bifurcation target. This must be the same or greater than . 41 | /// The number of unread bytes before resuming writing to the bifurcation target. This must be the same or lower than . 42 | /// The max number of bytes to write to the bifurcation target. Use -1 to specify no limit. 43 | /// 44 | public BifurcationTargetConfig( 45 | Func> reader, 46 | Func? exceptionHandler = null, 47 | int blockAfter = DefaultBlockAfter, 48 | int resumeAfter = DefaultResumeAfter, 49 | int maxTotalBytes = DefaultMaxTotalBytes 50 | ) : this( 51 | (pipeReader, cancellationToken) => reader(pipeReader.AsStream(), cancellationToken), 52 | exceptionHandler, 53 | blockAfter, 54 | resumeAfter, 55 | maxTotalBytes 56 | ) 57 | { } 58 | 59 | /// 60 | /// Creates a new for -based readers. 61 | /// 62 | /// The reader function that will handle this specific bifurcation target. 63 | /// The individual exception handler for this bifurcation target when exceptions occur during bifurcation. 64 | /// The number of unread bytes before writing will block to the bifurcation target. This must be the same or greater than . 65 | /// The number of unread bytes before resuming writing to the bifurcation target. This must be the same or lower than . 66 | /// The max number of bytes to write to the bifurcation target. Use -1 to specify no limit. 67 | /// 68 | public BifurcationTargetConfig( 69 | Func> reader, 70 | Func? exceptionHandler = null, 71 | int blockAfter = DefaultBlockAfter, 72 | int resumeAfter = DefaultResumeAfter, 73 | int maxTotalBytes = DefaultMaxTotalBytes 74 | ) 75 | { 76 | if (blockAfter < resumeAfter) 77 | { 78 | throw new ArgumentException($"{nameof(BlockAfter)} must be equal to or greater than {nameof(ResumeAfter)}", nameof(blockAfter)); 79 | } 80 | 81 | if (maxTotalBytes != -1 && maxTotalBytes <= 0) 82 | { 83 | throw new ArgumentException($"Invalid value for {nameof(MaxTotalBytes)}. Must be a value greater than 0, or if there is no limit, -1.", nameof(maxTotalBytes)); 84 | } 85 | 86 | Reader = reader; 87 | ExceptionHandler = exceptionHandler; 88 | BlockAfter = blockAfter; 89 | ResumeAfter = resumeAfter; 90 | MaxTotalBytes = maxTotalBytes; 91 | } 92 | } 93 | 94 | /// 95 | /// Manages the configuration for a specific bifurcation target. 96 | /// 97 | public class BifurcationTargetConfig : BifurcationTargetConfig 98 | { 99 | /// 100 | /// Creates a new for -based readers. 101 | /// 102 | /// The reader function that will handle this specific bifurcation target. 103 | /// The individual exception handler for this bifurcation target when exceptions occur in any target during bifurcation. 104 | /// The number of unread bytes before writing will block to the bifurcation target. This must be the same or greater than . 105 | /// The number of unread bytes before resuming writing to the bifurcation target. This must be the same or lower than . 106 | /// The max number of bytes to write to the bifurcation target. Use -1 to specify no limit. 107 | /// 108 | public BifurcationTargetConfig( 109 | Func reader, 110 | Func? exceptionHandler = null, 111 | int blockAfter = DefaultBlockAfter, 112 | int resumeAfter = DefaultResumeAfter, 113 | int maxTotalBytes = DefaultMaxTotalBytes 114 | ) : base( 115 | async (pipeReader, cancellationToken) => 116 | { 117 | await reader(pipeReader.AsStream(), cancellationToken); 118 | return null; 119 | }, 120 | exceptionHandler, 121 | blockAfter, 122 | resumeAfter, 123 | maxTotalBytes 124 | ) 125 | { } 126 | 127 | /// 128 | /// Creates a new for -based readers. 129 | /// 130 | /// The reader function that will handle this specific bifurcation target. 131 | /// The individual exception handler for this bifurcation target when exceptions occur during bifurcation. 132 | /// The number of unread bytes before writing will block to the bifurcation target. This must be the same or greater than . 133 | /// The number of unread bytes before resuming writing to the bifurcation target. This must be the same or lower than . 134 | /// The max number of bytes to write to the bifurcation target. Use -1 to specify no limit. 135 | /// 136 | public BifurcationTargetConfig( 137 | Func reader, 138 | Func? exceptionHandler = null, 139 | int blockAfter = DefaultBlockAfter, 140 | int resumeAfter = DefaultResumeAfter, 141 | int maxTotalBytes = DefaultMaxTotalBytes 142 | ) : base( 143 | async (pipeReader, cancellationToken) => 144 | { 145 | await reader(pipeReader, cancellationToken); 146 | return null; 147 | }, 148 | exceptionHandler, 149 | blockAfter, 150 | resumeAfter, 151 | maxTotalBytes 152 | ) 153 | { } 154 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /tests/TurnerSoftware.Aqueduct.Tests/PipeBifurcationTests.cs: -------------------------------------------------------------------------------- 1 | using System.IO.Pipelines; 2 | using System.Text; 3 | 4 | namespace TurnerSoftware.Aqueduct.Tests; 5 | 6 | [TestClass] 7 | public class PipeBifurcationTests 8 | { 9 | private static PipeReader CreateSource(string value) => PipeReader.Create(new(Encoding.ASCII.GetBytes(value))); 10 | 11 | private static Func CreateStringTarget(Func, Task> assertion) 12 | { 13 | return (reader, cancellationToken) => 14 | { 15 | static async Task ReadFullAsync(PipeReader reader, CancellationToken cancellationToken) 16 | { 17 | using var stream = reader.AsStream(); 18 | using var a = new StreamReader(stream); 19 | return await a.ReadToEndAsync(); 20 | } 21 | return assertion(ReadFullAsync(reader, cancellationToken)); 22 | }; 23 | } 24 | 25 | [TestMethod] 26 | public async Task NoTargets_ThrowsException() 27 | { 28 | var source = CreateSource("Test Value"); 29 | 30 | await FluentActions.Awaiting(() => PipeBifurcation.BifurcatedReadAsync(source, BifurcationSourceConfig.DefaultConfig)) 31 | .Should().ThrowAsync() 32 | .WithMessage("No target configurations*"); 33 | } 34 | 35 | [TestMethod] 36 | public async Task SingleTarget_TargetExceptionBubbles() 37 | { 38 | var source = CreateSource("Test Value"); 39 | 40 | await FluentActions.Awaiting(() => PipeBifurcation.BifurcatedReadAsync(source, BifurcationSourceConfig.DefaultConfig, new BifurcationTargetConfig( 41 | CreateStringTarget(async resultTask => 42 | { 43 | await resultTask; 44 | throw new ApplicationException("My processing exception"); 45 | }) 46 | ))) 47 | .Should() 48 | .ThrowAsync() 49 | .WithInnerException() 50 | .WithMessage("My processing exception"); 51 | } 52 | 53 | [TestMethod] 54 | public async Task SingleTarget_DefaultConfig_Success() 55 | { 56 | var source = CreateSource("Test Value"); 57 | var targetReaderHasCompleted = false; 58 | 59 | await PipeBifurcation.BifurcatedReadAsync( 60 | source, 61 | BifurcationSourceConfig.DefaultConfig, 62 | new BifurcationTargetConfig( 63 | CreateStringTarget(async resultTask => 64 | { 65 | var result = await resultTask; 66 | result.Should().Be("Test Value"); 67 | targetReaderHasCompleted = true; 68 | }) 69 | ) 70 | ); 71 | 72 | targetReaderHasCompleted.Should().BeTrue(); 73 | } 74 | 75 | [TestMethod] 76 | public async Task SingleTarget_ReaderCompletesEarly_Success() 77 | { 78 | var source = new Pipe(); 79 | var buffer = new byte[16]; 80 | await source.Writer.WriteAsync(buffer); 81 | var targetReaderHasCompleted = false; 82 | 83 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 84 | source.Reader, 85 | new BifurcationSourceConfig(minReadBufferSize: -1), 86 | new BifurcationTargetConfig( 87 | async (Stream reader, CancellationToken cancellationToken) => 88 | { 89 | var buffer = new byte[1]; 90 | await reader.ReadAsync(buffer, cancellationToken); 91 | targetReaderHasCompleted = true; 92 | }, 93 | //These are set to exaggerate the problem where exiting early 94 | //still has the pipe being fed bytes till the point it blocks 95 | blockAfter: 16, 96 | resumeAfter: 8 97 | ) 98 | ); 99 | 100 | await source.Writer.WriteAsync(buffer); 101 | await source.Writer.CompleteAsync(); 102 | await bifurcationTask; 103 | targetReaderHasCompleted.Should().BeTrue(); 104 | } 105 | 106 | [TestMethod] 107 | public async Task MultiTarget_DefaultConfig_Success() 108 | { 109 | var source = CreateSource("Test Value"); 110 | var completedTargetReaders = 0; 111 | 112 | await PipeBifurcation.BifurcatedReadAsync(source, 113 | BifurcationSourceConfig.DefaultConfig, 114 | new BifurcationTargetConfig( 115 | CreateStringTarget(async resultTask => 116 | { 117 | var result = await resultTask; 118 | result.Should().Be("Test Value"); 119 | Interlocked.Increment(ref completedTargetReaders); 120 | }) 121 | ), 122 | new BifurcationTargetConfig( 123 | CreateStringTarget(async resultTask => 124 | { 125 | var result = await resultTask; 126 | result.Should().Be("Test Value"); 127 | Interlocked.Increment(ref completedTargetReaders); 128 | }) 129 | ) 130 | ); 131 | 132 | completedTargetReaders.Should().Be(2); 133 | } 134 | 135 | [TestMethod] 136 | public async Task SingleTarget_ConfiguredMaxTotalBytes_LimitsBytes() 137 | { 138 | var source = CreateSource("Test Value"); 139 | 140 | await PipeBifurcation.BifurcatedReadAsync( 141 | source, 142 | BifurcationSourceConfig.DefaultConfig, 143 | new BifurcationTargetConfig( 144 | CreateStringTarget(async resultTask => 145 | { 146 | var result = await resultTask; 147 | result.Should().Be("Test"); 148 | }), 149 | maxTotalBytes: 4 150 | ) 151 | ); 152 | } 153 | 154 | [TestMethod] 155 | public async Task MultiTarget_ConfiguredMaxTotalBytesForOne_LimitsBytesOnlyForConfigured() 156 | { 157 | var source = CreateSource("Test Value"); 158 | 159 | await PipeBifurcation.BifurcatedReadAsync( 160 | source, 161 | BifurcationSourceConfig.DefaultConfig, 162 | new BifurcationTargetConfig( 163 | CreateStringTarget(async resultTask => 164 | { 165 | var result = await resultTask; 166 | result.Should().Be("Test"); 167 | }), 168 | maxTotalBytes: 4 169 | ), 170 | new BifurcationTargetConfig( 171 | CreateStringTarget(async resultTask => 172 | { 173 | var result = await resultTask; 174 | result.Should().Be("Test Value"); 175 | }) 176 | ) 177 | ); 178 | } 179 | 180 | [TestMethod] 181 | public async Task MultiTarget_CompletedTargets_AreNotWrittenToFurther() 182 | { 183 | var sourcePipe = new Pipe(); 184 | 185 | var firstTargetReadBufferLength = -1L; 186 | var firstTargetReaderIsComplete = false; 187 | var secondTargetReadBufferLength = -1L; 188 | 189 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 190 | sourcePipe.Reader, 191 | new BifurcationSourceConfig(minReadBufferSize: 4), 192 | new BifurcationTargetConfig( 193 | async (reader, cancellationToken) => 194 | { 195 | var firstResult = await reader.ReadAsync(cancellationToken); 196 | firstTargetReadBufferLength = firstResult.Buffer.Length; 197 | reader.AdvanceTo(firstResult.Buffer.End); 198 | var secondResult = await reader.ReadAsync(cancellationToken); 199 | firstTargetReadBufferLength += secondResult.Buffer.Length; 200 | reader.AdvanceTo(secondResult.Buffer.End); 201 | var thirdResult = await reader.ReadAsync(cancellationToken); 202 | firstTargetReaderIsComplete = thirdResult.IsCompleted; 203 | }, 204 | maxTotalBytes: 6 205 | ), 206 | new BifurcationTargetConfig( 207 | async (reader, cancellationToken) => 208 | { 209 | var firstResult = await reader.ReadAsync(cancellationToken); 210 | secondTargetReadBufferLength = firstResult.Buffer.Length; 211 | reader.AdvanceTo(firstResult.Buffer.End); 212 | var secondResult = await reader.ReadAsync(cancellationToken); 213 | secondTargetReadBufferLength += secondResult.Buffer.Length; 214 | } 215 | ) 216 | ); 217 | 218 | var action = async () => 219 | { 220 | await sourcePipe.Writer.WriteAsync(new byte[2]); 221 | await Task.Delay(100); 222 | await sourcePipe.Writer.WriteAsync(new byte[2]); 223 | await Task.Delay(100); 224 | await sourcePipe.Writer.WriteAsync(new byte[2]); 225 | await Task.Delay(100); 226 | await sourcePipe.Writer.WriteAsync(new byte[2]); 227 | await Task.Delay(100); 228 | await sourcePipe.Writer.CompleteAsync(); 229 | await bifurcationTask; 230 | }; 231 | 232 | await action.Should().CompleteWithinAsync(TimeSpan.FromSeconds(1)); 233 | firstTargetReadBufferLength.Should().Be(6); 234 | firstTargetReaderIsComplete.Should().BeTrue(); 235 | secondTargetReadBufferLength.Should().Be(8); 236 | } 237 | 238 | private record TestResultData(long TargetOneValue, long TargetTwoValue); 239 | 240 | [TestMethod] 241 | public async Task MultiTarget_ResultsAreReturnedInOrderFromTargets() 242 | { 243 | var source = CreateSource("Test Value"); 244 | 245 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 246 | source, 247 | new BifurcationSourceConfig(), 248 | new BifurcationTargetConfig( 249 | async (reader, cancellationToken) => 250 | { 251 | var result = await reader.ReadAsync(cancellationToken); 252 | return new(result.Buffer.Length, 0); 253 | } 254 | ), 255 | new BifurcationTargetConfig( 256 | async (reader, cancellationToken) => 257 | { 258 | var result = await reader.ReadAsync(cancellationToken); 259 | return new(0, result.Buffer.Length); 260 | } 261 | ) 262 | ); 263 | 264 | var result = await bifurcationTask; 265 | result.Should().NotBeNull().And.HaveCount(2); 266 | result[0].Should().BeEquivalentTo(new TestResultData(10, 0)); 267 | result[1].Should().BeEquivalentTo(new TestResultData(0, 10)); 268 | } 269 | 270 | [TestMethod] 271 | public async Task MultiTarget_NonBubblingExceptionsStillReturnResultsThatCompleted() 272 | { 273 | var source = CreateSource("Test Value"); 274 | 275 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 276 | source, 277 | new BifurcationSourceConfig(bubbleExceptions: false), 278 | new BifurcationTargetConfig( 279 | (PipeReader reader, CancellationToken cancellationToken) => 280 | { 281 | throw new Exception("Whoops"); 282 | } 283 | ), 284 | new BifurcationTargetConfig( 285 | (PipeReader reader, CancellationToken cancellationToken) => Task.FromResult(true) 286 | ) 287 | ); 288 | 289 | var result = await bifurcationTask; 290 | result.Should().NotBeNull().And.HaveCount(2); 291 | result[0].Should().BeFalse(); 292 | result[1].Should().BeTrue(); 293 | } 294 | 295 | [TestMethod] 296 | public async Task MultiTarget_ExceptionsFromOtherTargets_PushesExceptionsToTargets() 297 | { 298 | var sourcePipe = new Pipe(); 299 | 300 | Exception targetReaderException = null!; 301 | 302 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 303 | sourcePipe.Reader, 304 | new BifurcationSourceConfig(minReadBufferSize: 4, bubbleExceptions: false), 305 | new BifurcationTargetConfig( 306 | async (reader, cancellationToken) => 307 | { 308 | await reader.ReadAsync(cancellationToken); 309 | throw new Exception("TargetException"); 310 | } 311 | ), 312 | new BifurcationTargetConfig( 313 | async (reader, cancellationToken) => 314 | { 315 | try 316 | { 317 | var readResult = await reader.ReadAsync(cancellationToken); 318 | reader.AdvanceTo(readResult.Buffer.End); 319 | await reader.ReadAsync(cancellationToken); 320 | } 321 | catch (Exception ex) 322 | { 323 | targetReaderException = ex; 324 | } 325 | } 326 | ) 327 | ); 328 | 329 | var action = async () => 330 | { 331 | await sourcePipe.Writer.WriteAsync(new byte[4]); 332 | await sourcePipe.Writer.WriteAsync(new byte[4]); 333 | await sourcePipe.Writer.CompleteAsync(); 334 | await bifurcationTask; 335 | }; 336 | 337 | await action.Should() 338 | .NotThrowAsync(); 339 | 340 | targetReaderException.Should().BeOfType() 341 | .Subject.InnerException!.Message.Should().Be("TargetException"); 342 | } 343 | 344 | [TestMethod] 345 | public async Task MultiTarget_ExceptionsFromOtherTargets_ExceptionHandlerIsTriggered() 346 | { 347 | var sourcePipe = new Pipe(); 348 | 349 | Exception targetReaderException = null!; 350 | 351 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 352 | sourcePipe.Reader, 353 | new BifurcationSourceConfig(minReadBufferSize: 4, bubbleExceptions: false), 354 | new BifurcationTargetConfig( 355 | async (reader, cancellationToken) => 356 | { 357 | await reader.ReadAsync(cancellationToken); 358 | throw new Exception("TargetException"); 359 | } 360 | ), 361 | new BifurcationTargetConfig( 362 | async (reader, cancellationToken) => 363 | { 364 | var readResult = await reader.ReadAsync(cancellationToken); 365 | reader.AdvanceTo(readResult.Buffer.End); 366 | await reader.ReadAsync(cancellationToken); 367 | }, 368 | exception => 369 | { 370 | targetReaderException = exception; 371 | return Task.CompletedTask; 372 | } 373 | ) 374 | ); 375 | 376 | var action = async () => 377 | { 378 | await sourcePipe.Writer.WriteAsync(new byte[4]); 379 | await sourcePipe.Writer.WriteAsync(new byte[4]); 380 | await sourcePipe.Writer.CompleteAsync(); 381 | await bifurcationTask; 382 | }; 383 | 384 | await action.Should() 385 | .NotThrowAsync(); 386 | 387 | targetReaderException.Should().BeOfType() 388 | .Subject.InnerException!.Message.Should().Be("TargetException"); 389 | } 390 | 391 | [TestMethod] 392 | public async Task SingleTarget_ExceptionsFromSelf_ExceptionHandlerIsTriggered() 393 | { 394 | var sourcePipe = new Pipe(); 395 | 396 | Exception targetReaderException = null!; 397 | 398 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 399 | sourcePipe.Reader, 400 | new BifurcationSourceConfig(minReadBufferSize: 4, bubbleExceptions: false), 401 | new BifurcationTargetConfig( 402 | async (reader, cancellationToken) => 403 | { 404 | await reader.ReadAsync(cancellationToken); 405 | throw new Exception("TargetException"); 406 | }, 407 | exception => 408 | { 409 | targetReaderException = exception; 410 | return Task.CompletedTask; 411 | } 412 | ) 413 | ); 414 | 415 | var action = async () => 416 | { 417 | await sourcePipe.Writer.WriteAsync(new byte[4]); 418 | await sourcePipe.Writer.WriteAsync(new byte[4]); 419 | await sourcePipe.Writer.CompleteAsync(); 420 | await bifurcationTask; 421 | }; 422 | 423 | await action.Should() 424 | .NotThrowAsync(); 425 | 426 | targetReaderException.Should().BeOfType() 427 | .Subject.InnerException!.Message.Should().Be("TargetException"); 428 | } 429 | 430 | [TestMethod] 431 | public async Task SourceConfig_MinimumBufferSize_BufferIsAtLeastMinimum() 432 | { 433 | var sourcePipe = new Pipe(); 434 | var readBufferLength = -1L; 435 | 436 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 437 | sourcePipe.Reader, 438 | new BifurcationSourceConfig(minReadBufferSize: 4), 439 | new BifurcationTargetConfig( 440 | async (reader, cancellationToken) => 441 | { 442 | var result = await reader.ReadAsync(cancellationToken); 443 | readBufferLength = result.Buffer.Length; 444 | await reader.CompleteAsync(); 445 | } 446 | ) 447 | ); 448 | 449 | var action = async () => 450 | { 451 | await sourcePipe.Writer.WriteAsync(new byte[2]); 452 | await Task.Delay(100); 453 | await sourcePipe.Writer.WriteAsync(new byte[2]); 454 | await Task.Delay(100); 455 | await sourcePipe.Writer.WriteAsync(new byte[2]); 456 | await Task.Delay(100); 457 | await sourcePipe.Writer.CompleteAsync(); 458 | await bifurcationTask; 459 | }; 460 | 461 | await action.Should().CompleteWithinAsync(TimeSpan.FromSeconds(1)); 462 | readBufferLength.Should().Be(4); 463 | } 464 | 465 | [TestMethod] 466 | public async Task SourceConfig_MinimumBufferSize_MinimumDoesNotImpactLargerSize() 467 | { 468 | var sourcePipe = new Pipe(); 469 | var readBufferLength = -1L; 470 | 471 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 472 | sourcePipe.Reader, 473 | new BifurcationSourceConfig(minReadBufferSize: 4), 474 | new BifurcationTargetConfig( 475 | async (reader, cancellationToken) => 476 | { 477 | var result = await reader.ReadAsync(cancellationToken); 478 | readBufferLength = result.Buffer.Length; 479 | await reader.CompleteAsync(); 480 | } 481 | ) 482 | ); 483 | 484 | var action = async () => 485 | { 486 | await sourcePipe.Writer.WriteAsync(new byte[6]); 487 | await Task.Delay(100); 488 | await sourcePipe.Writer.WriteAsync(new byte[2]); 489 | await Task.Delay(100); 490 | await sourcePipe.Writer.CompleteAsync(); 491 | await bifurcationTask; 492 | }; 493 | 494 | await action.Should().CompleteWithinAsync(TimeSpan.FromSeconds(1)); 495 | readBufferLength.Should().Be(6); 496 | } 497 | 498 | [TestMethod] 499 | public async Task SourceConfig_BubbleExceptions_Enabled() 500 | { 501 | var sourcePipe = new Pipe(); 502 | 503 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 504 | sourcePipe.Reader, 505 | new BifurcationSourceConfig(minReadBufferSize: 4), 506 | new BifurcationTargetConfig( 507 | async (reader, cancellationToken) => 508 | { 509 | await reader.ReadAsync(cancellationToken); 510 | throw new Exception("TargetException"); 511 | } 512 | ) 513 | ); 514 | 515 | var action = async () => 516 | { 517 | await sourcePipe.Writer.WriteAsync(new byte[4]); 518 | await sourcePipe.Writer.CompleteAsync(); 519 | await bifurcationTask; 520 | }; 521 | 522 | await action.Should() 523 | .ThrowAsync() 524 | .WithInnerException() 525 | .WithMessage("TargetException"); 526 | } 527 | 528 | [TestMethod] 529 | public async Task SourceConfig_BubbleExceptions_Disabled() 530 | { 531 | var sourcePipe = new Pipe(); 532 | 533 | var bifurcationTask = PipeBifurcation.BifurcatedReadAsync( 534 | sourcePipe.Reader, 535 | new BifurcationSourceConfig(minReadBufferSize: 4, bubbleExceptions: false), 536 | new BifurcationTargetConfig( 537 | async (reader, cancellationToken) => 538 | { 539 | await reader.ReadAsync(cancellationToken); 540 | throw new Exception("TargetException"); 541 | } 542 | ) 543 | ); 544 | 545 | var action = async () => 546 | { 547 | await sourcePipe.Writer.WriteAsync(new byte[4]); 548 | await sourcePipe.Writer.CompleteAsync(); 549 | await bifurcationTask; 550 | }; 551 | 552 | await action.Should() 553 | .NotThrowAsync(); 554 | } 555 | } --------------------------------------------------------------------------------