Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.0 ssi bmv bug #24

Merged
merged 2 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions DotNetVault/Vaults/BasicMonitorVault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ public sealed class BasicMonitorVault<[VaultSafeTypeParam] T> : MonitorVault<T>,
#region CTORS
/// <inheritdoc />
public BasicMonitorVault(TimeSpan defaultTimeout)
: base(defaultTimeout) { }
: this(defaultTimeout, default) { }

/// <summary>
/// Creates a vault. The value of <see cref="FallbackTimeout"/> will be used as default timeout.
/// </summary>
/// <param name="initialValue">initial value of protected resource</param>
public BasicMonitorVault(T initialValue) : this(initialValue, FallbackTimeout)
{

}
public BasicMonitorVault(T initialValue)
: this(initialValue, FallbackTimeout) {}

/// <summary>
/// Creates a vault using the default value of the protected resource and the value
/// specified by <see cref="FallbackTimeout"/> as the default wait period.
/// </summary>
public BasicMonitorVault() : this(default, FallbackTimeout)
{

}
public BasicMonitorVault()
: this(default, FallbackTimeout) { }

/// <summary>
/// CTOR -- vault with specified value and timeout
/// </summary>
/// <param name="initialValue">initial value of protected resource</param>
/// <param name="defaultTimeout">default timeout period</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="defaultTimeout"/> was null</exception>
public BasicMonitorVault(T initialValue, TimeSpan defaultTimeout) : base(defaultTimeout)
public BasicMonitorVault(T initialValue, TimeSpan defaultTimeout)
: this(defaultTimeout, initialValue) {}

private BasicMonitorVault(TimeSpan defaultTimeout, T initialValue)
: base(defaultTimeout)
{
Init(initialValue);
Debug.Assert(BoxPtr != null);
}
}
#endregion

#region Lock acquisition methods
Expand Down
14 changes: 9 additions & 5 deletions VaultUnitTests/BasicMonVAcqBehaviorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
using Xunit.Abstractions;
using Xunit.Sdk;
using ResourceType = System.String;

using NullVtType = System.Nullable<System.UInt64>;
using VtType = System.UInt64;
namespace VaultUnitTests
{
using VaultType = DotNetVault.Vaults.BasicMonitorVault<ResourceType>;
using NullVtVault = DotNetVault.Vaults.BasicMonitorVault<NullVtType>;
using VtVault = DotNetVault.Vaults.BasicMonitorVault<VtType>;

public delegate VaultType MonVaultCreationMethod(TimeSpan timeout, Func<ResourceType> ctor = null);
public sealed class BasicMonVaultAcqBehaviorTests : VaultAcqBehaviorTest
Expand All @@ -20,7 +23,8 @@ public BasicMonVaultAcqBehaviorTests([NotNull] ITestOutputHelper helper, [NotNul
_meth = (ts, ctor) => Fixture.CreateBasicMonitorVault<ResourceType>();
}




[Fact]
public void TestThrowsAlready()
{
Expand Down Expand Up @@ -229,7 +233,7 @@ static void DoThreadTwo(VaultType bv, StartToken tkn, ExceptionReceptor receptor
public void TestSeqBlockAcqs()
{
string finalResult;
using (var vault = _meth(TimeSpan.FromMilliseconds(250)))
using (VaultType vault = _meth(TimeSpan.FromMilliseconds(250)))
{
vault.SetCurrentValue(TimeSpan.FromMilliseconds(10), string.Empty);

Expand All @@ -254,8 +258,8 @@ public void TestSeqBlockAcqs()
}
Assert.True(finalResult == vault.CopyCurrentValue(TimeSpan.FromMilliseconds(10)));
}
Helper.WriteLine(finalResult);
}
Helper.WriteLine(finalResult);
}

[Fact]
public void TestThrowsOperationCancelledWhenCancelBeforeTimeout()
Expand Down
146 changes: 146 additions & 0 deletions VaultUnitTests/Issue22Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Xunit;
using Xunit.Abstractions;
using ResourceType = System.String;
using NullVtType = System.Nullable<System.UInt64>;
using VtType = System.UInt64;
namespace VaultUnitTests
{
using VaultType = DotNetVault.Vaults.BasicMonitorVault<ResourceType>;
using NullVtVault = DotNetVault.Vaults.BasicMonitorVault<NullVtType>;
using VtVault = DotNetVault.Vaults.BasicMonitorVault<VtType>;
public class Issue22Tests : NullableAcqTests
{
/// <inheritdoc />
public Issue22Tests([NotNull] ITestOutputHelper helper,
[NotNull] NullableVtVaultFactoryFixture fixture) : base(helper, fixture) {}

[Fact]
public void TestGetLockUnsetNullableVt()
{
using (var vault = Fixture.CreateUnsetNullableVtVault())
{
{
using var lck = vault.Lock();
Helper.WriteLine($"Value now: {lck.Value?.ToString() ?? "NULL"}.");
}
}
}

[Fact]
public void TestGetLockPresetNullableVt()
{
const ulong targetVal = 24;
using (var vault = Fixture.CreateUnsetNullableVtVault())
{
vault.SetCurrentValue(TimeSpan.FromMilliseconds(250), targetVal);
{
using var lck = vault.Lock();
Assert.True(lck.Value == targetVal);
}
}
}

[Fact]
public void TestGetLockConstructedNullableVt()
{
const ulong targetVal = 66;
using (var vault = Fixture.CreateSetNullVtVault(targetVal))
{
using var lck = vault.Lock();
Assert.True(lck.Value == targetVal);
}
}

[Fact]
public void TestGetLockUnsetVt()
{
using (var vault = Fixture.CreateUnsetVtVault())
{
{
using var lck = vault.Lock();
Helper.WriteLine($"Value now: {lck.Value.ToString()}.");
}
}
}

[Fact]
public void TestGetLockPresetVt()
{
const ulong targetVal = 24;
using (var vault = Fixture.CreateUnsetVtVault())
{
vault.SetCurrentValue(TimeSpan.FromMilliseconds(250), targetVal);
{
using var lck = vault.Lock();
Assert.True(lck.Value == targetVal);
}
}
}

[Fact]
public void TestGetLockConstructedVt()
{
const ulong targetVal = 66;
using (var vault = Fixture.CreateSetVtVault(targetVal))
{
using var lck = vault.Lock();
Assert.True(lck.Value == targetVal);
}
}

[Fact]
public void TestGetLockUnsetRt()
{
using (var vault = Fixture.CreateUnsetVault())
{
{
using var lck = vault.Lock();
Helper.WriteLine($"Value now: {lck.Value ?? "NULL"}.");
}
}
}

[Fact]
public void TestGetLockPreset()
{
const string targetVal = "FooBar";
using (var vault = Fixture.CreateUnsetVault())
{
vault.SetCurrentValue(TimeSpan.FromMilliseconds(250), targetVal);
{
using var lck = vault.Lock();
Assert.True(lck.Value == targetVal);
}
}
}

[Fact]
public void TestGetLockConstructedNullRt()
{
const string targetVal = null;
using (var vault = Fixture.CreateSetVault(targetVal))
{
using var lck = vault.Lock();
Assert.True(lck.Value == targetVal);
}
}

[Fact]
public void TestGetLockConstructedNotNullRtVault()
{
const string targetVal = "frobnication";
using (var vault = Fixture.CreateSetVault(targetVal))
{
using var lck = vault.Lock();
Assert.True(lck.Value == targetVal);
}
}

}
}
46 changes: 45 additions & 1 deletion VaultUnitTests/VaultAcqBehaviorTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using System;
using DotNetVault.Exceptions;
using DotNetVault.Vaults;
using JetBrains.Annotations;
using Xunit;
using Xunit.Abstractions;

using ResourceType = System.String;
using NullVtType = System.Nullable<System.UInt64>;
using VtType = System.UInt64;
namespace VaultUnitTests
{
using VaultType = DotNetVault.Vaults.BasicMonitorVault<ResourceType>;
using NullVtVault = DotNetVault.Vaults.BasicMonitorVault<NullVtType>;
using VtVault = DotNetVault.Vaults.BasicMonitorVault<VtType>;
public abstract class VaultAcqBehaviorTest : TestBase<VaultFactoryFixture>
{


protected VaultAcqBehaviorTest([NotNull] ITestOutputHelper helper, [NotNull] VaultFactoryFixture fixture) : base(helper, fixture)
{
}
Expand All @@ -23,4 +32,39 @@ protected TestBase([NotNull] ITestOutputHelper helper, [NotNull] TFixture fixtur
Fixture = fixture ?? throw new ArgumentNullException(nameof(fixture));
}
}

public class NullableVtVaultFactoryFixture : VaultFactoryFixture
{
public TimeSpan VaultTimeout
{
get => _timeout;
init => _timeout = value > TimeSpan.Zero
? value
: throw new ArgumentNotPositiveException<TimeSpan>(nameof(value), value);
}

public NullVtVault CreateSetNullVtVault(NullVtType initial) => new(initial, _timeout);

public NullVtVault CreateUnsetNullableVtVault() => new (_timeout);

public VtVault CreateUnsetVtVault() => new(_timeout);

public VtVault CreateSetVtVault(VtType value) => new(value, _timeout);

public VaultType CreateUnsetVault() => new(_timeout);

public VaultType CreateSetVault(ResourceType rt) => new(rt, _timeout);


private readonly TimeSpan _timeout = TimeSpan.FromMilliseconds(500);
}

public abstract class NullableAcqTests : TestBase<NullableVtVaultFactoryFixture>
{
/// <inheritdoc />
protected NullableAcqTests([NotNull] ITestOutputHelper helper,
[NotNull] NullableVtVaultFactoryFixture fixture) : base(helper, fixture) { }
}


}
6 changes: 3 additions & 3 deletions VaultUnitTests/VaultUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -26,10 +26,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HighPrecisionTimeStamps" Version="1.0.0.1" />
<PackageReference Include="HighPrecisionTimeStamps" Version="1.0.0.6" />
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" />
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.1.20200127.214830" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down