From 06dadac3eef009c4e0fda258db4aaa06b64b15e3 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Tue, 4 Oct 2022 18:00:39 -0700 Subject: [PATCH 1/4] infer --- .github/workflows/infer.yml | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/infer.yml diff --git a/.github/workflows/infer.yml b/.github/workflows/infer.yml new file mode 100644 index 00000000..c080a380 --- /dev/null +++ b/.github/workflows/infer.yml @@ -0,0 +1,36 @@ +name: Infer + +on: + push: + paths-ignore: [ '**.md' ] + branches: [ main ] + pull_request: + paths-ignore: [ '**.md' ] + branches: [ main ] + +jobs: + bench: + + runs-on: windows-latest + + steps: + - uses: actions/checkout@v3 + - name: Setup .NET Core + uses: actions/setup-dotnet@v2 + with: + dotnet-version: 6.0.x + - name: Install dependencies + run: dotnet restore + - name: Build + run: dotnet build --configuration Release --no-restore + + - name: Run Infer# + uses: microsoft/infersharpaction@v1.4 + id: runinfersharp + with: + binary-path: BitFaster.Caching/bin + + - name: Upload SARIF output to GitHub Security Center + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: infer-out/report.sarif \ No newline at end of file From 07fbb6f6627f31ec3cb9f3fdd9ec6e9b75b91d68 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Tue, 4 Oct 2022 18:04:47 -0700 Subject: [PATCH 2/4] ubuntu --- .github/workflows/infer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/infer.yml b/.github/workflows/infer.yml index c080a380..d7bf7c22 100644 --- a/.github/workflows/infer.yml +++ b/.github/workflows/infer.yml @@ -11,7 +11,7 @@ on: jobs: bench: - runs-on: windows-latest + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 From 51d61386e45c2a43a6245357cc5f889dce93cb92 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Tue, 4 Oct 2022 18:07:26 -0700 Subject: [PATCH 3/4] rename --- .github/workflows/infer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/infer.yml b/.github/workflows/infer.yml index d7bf7c22..b9fc9b92 100644 --- a/.github/workflows/infer.yml +++ b/.github/workflows/infer.yml @@ -9,7 +9,7 @@ on: branches: [ main ] jobs: - bench: + infer: runs-on: ubuntu-latest From 2414b5abdbb6336fd83a09257b41c67053e88e01 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Sat, 29 Oct 2022 12:59:53 -0700 Subject: [PATCH 4/4] verify dispose --- .../Atomic/AtomicFactoryScopedAsyncCacheTests.cs | 16 ++++++++++++++++ .../Atomic/AtomicFactoryScopedCacheTests.cs | 16 ++++++++++++++++ BitFaster.Caching.UnitTests/ScopedTests.cs | 10 ++++++++++ 3 files changed, 42 insertions(+) diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs index 81d9cae5..e9244c6c 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs @@ -67,5 +67,21 @@ public void WhenNoInnerEventsNoOuterEvents() cache.Events.HasValue.Should().BeFalse(); } + + // Infer identified AddOrUpdate and TryUpdate as resource leaks. This test verifies correct disposal. + [Fact] + public void WhenEntryIsUpdatedOldEntryIsDisposed() + { + var disposable1 = new Disposable(); + var disposable2 = new Disposable(); + + this.cache.AddOrUpdate(1, disposable1); + + this.cache.TryUpdate(1, disposable2).Should().BeTrue(); + disposable1.IsDisposed.Should().BeTrue(); + + this.cache.TryUpdate(1, new Disposable()).Should().BeTrue(); + disposable2.IsDisposed.Should().BeTrue(); + } } } diff --git a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs index 1786b7dd..0c6b6b6a 100644 --- a/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs +++ b/BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs @@ -67,5 +67,21 @@ public void WhenNoInnerEventsNoOuterEvents() cache.Events.HasValue.Should().BeFalse(); } + + // Infer identified AddOrUpdate and TryUpdate as resource leaks. This test verifies correct disposal. + [Fact] + public void WhenEntryIsUpdatedOldEntryIsDisposed() + { + var disposable1 = new Disposable(); + var disposable2 = new Disposable(); + + this.cache.AddOrUpdate(1, disposable1); + + this.cache.TryUpdate(1, disposable2).Should().BeTrue(); + disposable1.IsDisposed.Should().BeTrue(); + + this.cache.TryUpdate(1, new Disposable()).Should().BeTrue(); + disposable2.IsDisposed.Should().BeTrue(); + } } } diff --git a/BitFaster.Caching.UnitTests/ScopedTests.cs b/BitFaster.Caching.UnitTests/ScopedTests.cs index 3bfb08ce..d2f1618b 100644 --- a/BitFaster.Caching.UnitTests/ScopedTests.cs +++ b/BitFaster.Caching.UnitTests/ScopedTests.cs @@ -9,6 +9,16 @@ namespace BitFaster.Caching.UnitTests { public class ScopedTests { + [Fact] + public void WhenScopeIsCreatedThenScopeDisposedValueIsDisposed() + { + var disposable = new Disposable(); + var scope = new Scoped(disposable); + + scope.Dispose(); + disposable.IsDisposed.Should().BeTrue(); + } + [Fact] public void WhenScopeIsCreatedThenScopeDisposedLifetimeDisposesValue() {