public async Task Lock_AbortAction() { var client = new ConsulClient(); const string keyName = "test/lock/abort"; using (var cts = new CancellationTokenSource()) { try { string lockSession = (await client.Session.Create(new SessionEntry() { TTL = TimeSpan.FromSeconds(10) })).Response; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.Session.RenewPeriodic(TimeSpan.FromSeconds(10), lockSession, cts.Token); Task.Delay(1000).ContinueWith((w) => { client.Session.Destroy(lockSession); }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.ExecuteAbortableLocked(new LockOptions(keyName) { Session = lockSession }, CancellationToken.None, () => { Thread.Sleep(60000); }); } catch (TimeoutException ex) { Assert.IsType <TimeoutException>(ex); } cts.Cancel(); } using (var cts = new CancellationTokenSource()) { string lockSession = (await client.Session.Create(new SessionEntry() { TTL = TimeSpan.FromSeconds(10) })).Response; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.Session.RenewPeriodic(TimeSpan.FromSeconds(10), lockSession, cts.Token); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.ExecuteAbortableLocked(new LockOptions(keyName) { Session = lockSession }, CancellationToken.None, () => { Task.Delay(1000).ContinueWith((w) => { Assert.True(true); }); }); cts.Cancel(); } }
public async Task Lock_AbortAction() { var client = new ConsulClient(); const string keyName = "test/lock/abort"; using (var cts = new CancellationTokenSource()) { try { string lockSession = (await client.Session.Create(new SessionEntry() { TTL = TimeSpan.FromSeconds(10) })).Response; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.Session.RenewPeriodic(TimeSpan.FromSeconds(10), lockSession, cts.Token); Task.Delay(1000).ContinueWith((w) => { client.Session.Destroy(lockSession); }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.ExecuteAbortableLocked(new LockOptions(keyName) { Session = lockSession }, CancellationToken.None, () => { Thread.Sleep(60000); }); } catch (TimeoutException ex) { Assert.IsType<TimeoutException>(ex); } cts.Cancel(); } using (var cts = new CancellationTokenSource()) { string lockSession = (await client.Session.Create(new SessionEntry() { TTL = TimeSpan.FromSeconds(10) })).Response; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.Session.RenewPeriodic(TimeSpan.FromSeconds(10), lockSession, cts.Token); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed client.ExecuteAbortableLocked(new LockOptions(keyName) { Session = lockSession }, CancellationToken.None, () => { Task.Delay(1000).ContinueWith((w) => { Assert.True(true); }); }); cts.Cancel(); } }