Giter VIP home page Giter VIP logo

Comments (5)

mfkl avatar mfkl commented on August 25, 2024

Hi,

Thanks for the report! So we can best assist you, could you please follow the issue template with your info? Thanks!

If I call Acquire method with an already cancelled token I get a Null Reference Exception from whitin the consul library.

This would be a bug. Feel free to share a repro code (or unit test) :-)

Can you please explain why the cancellation token is not passed?

No reason, it may have been overlooked. Could you share a repro/unit test for this as well please?

Thanks.

from consuldotnet.

georgy93 avatar georgy93 commented on August 25, 2024

Hi again!
Thank you for your response!
Here are the unit tests. Please note, that if you run them all at once, one might fail because the lock entry doesn't clean up always from consul agent. However, if you make sure there is no lock session before each test runs they all pass.

Consuldotnet v 1.6.1.1
OS: Windows

namespace XUnitTestProject1
{
using Consul;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
public class LockTest
{
private readonly string key = "service/myApp/leader";
private readonly Uri _uri = new Uri("http://consul:8500");
private readonly LockOptions _lockOptions;
private readonly ConsulClient _slaveInstanceClient;
private readonly ConsulClient _masterInstanceClient;
private const int DefaultSessionTTLSeconds = 10;
private const int LockWaitTimeSeconds = 15;
public LockTest()
{
_lockOptions = new LockOptions(key)
{
SessionTTL = TimeSpan.FromSeconds(DefaultSessionTTLSeconds),
LockWaitTime = TimeSpan.FromSeconds(LockWaitTimeSeconds)
};
_slaveInstanceClient = new ConsulClient(c => c.Address = _uri);
_masterInstanceClient = new ConsulClient(c => c.Address = _uri);
}
[Fact]
public async Task Passing_Cancelled_CancellationToken_Throws_NullReferenceEx()
{
// Arrange
var distributedLock = _slaveInstanceClient.CreateLock(_lockOptions);
var cts = new CancellationTokenSource();
// Act
cts.Cancel();
// Assert
await Assert.ThrowsAsync < NullReferenceException >(async () =>
await distributedLock.Acquire(cts.Token));
}
[Fact]
public async Task Cancelling_A_Token_When_Acquiring_A_Lock_Throws_NullReferenceEx()
{
// Arrange
var distributedLock2 = _masterInstanceClient.CreateLock(_lockOptions);
var distributedLock = _slaveInstanceClient.CreateLock(_lockOptions);
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
// Act
await distributedLock2.Acquire(); // Become "Master" with another instance first
var task = distributedLock.Acquire(cts.Token);
// Assert
await Assert.ThrowsAsync < NullReferenceException >(async () => await task);
// cleanup
await distributedLock2.Release();
if (distributedLock.IsHeld)
await distributedLock2.Release();
await distributedLock2.Destroy();
}
[Fact]
public async Task Cancelling_A_Token_When_Acquiring_A_Lock_Doesnt_Respect_Token()
{
// Arrange
var distributedLock2 = _masterInstanceClient.CreateLock(_lockOptions);
var distributedLock = _slaveInstanceClient.CreateLock(_lockOptions);
var cancellationOperationTimer = new Stopwatch();
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
// Act
await distributedLock2.Acquire(); // Become "Master" with another instance first
cancellationOperationTimer.Start();
try
{
await distributedLock.Acquire(cts.Token);
}
catch (Exception) { }
cancellationOperationTimer.Stop();
// Assert
var stopTimeMs = cancellationOperationTimer.ElapsedMilliseconds;
var LockWaitTimeMs = TimeSpan.FromSeconds(LockWaitTimeSeconds).TotalMilliseconds;
Assert.True(stopTimeMs > LockWaitTimeMs);
// cleanup
await distributedLock2.Release();
if (distributedLock.IsHeld)
await distributedLock2.Release();
await distributedLock2.Destroy();
}
}
}

from consuldotnet.

mfkl avatar mfkl commented on August 25, 2024

Great, thanks.

Would you mind pushing them to a branch on your fork and sharing it here, please? This will be easier to retrieve and run locally for the maintainers :-)

from consuldotnet.

mfkl avatar mfkl commented on August 25, 2024

Please try https://www.nuget.org/packages/Consul/1.6.10.1-rc1

from consuldotnet.

georgy93 avatar georgy93 commented on August 25, 2024

Hi, Today I updated to the latest version of the package and the issues are resolved. Thanks!

from consuldotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.