예제 #1
0
        public async Task AsyncLockWithValue_GetLockOrValueAsync_Canceled([Values(true, false)] bool async)
        {
            var alwv      = new AsyncLockWithValue <int>();
            var asyncLock = await alwv.GetLockOrValueAsync(async).ConfigureAwait(false);

            var cts  = new CancellationTokenSource();
            var task = Task.Run(async() =>
            {
                using var secondLock = await alwv.GetLockOrValueAsync(async, cts.Token);
            }, default);
        public async Task AsyncLockWithValue_SetValueInCtor([Values(true, false)] bool async)
        {
            var alwv = new AsyncLockWithValue <int>(42);

            Assert.IsTrue(alwv.HasValue);
            Assert.IsTrue(alwv.TryGetValue(out var value));
            Assert.AreEqual(42, value);

            using var asyncLock = await alwv.GetLockOrValueAsync(async).ConfigureAwait(false);

            Assert.IsTrue(asyncLock.HasValue);
            Assert.AreEqual(42, asyncLock.Value);
        }
예제 #3
0
        public async Task AsyncLockWithValue_GetLockOrValueAsync([Values(true, false)] bool async)
        {
            var alwv = new AsyncLockWithValue <int>();

            AsyncLockWithValue <int> .Lock asyncLock;
            using (asyncLock = await alwv.GetLockOrValueAsync(async).ConfigureAwait(false))
            {
                Assert.IsFalse(asyncLock.HasValue);
                asyncLock.SetValue(42);
            }

            using (asyncLock = await alwv.GetLockOrValueAsync(async).ConfigureAwait(false))
            {
                Assert.IsTrue(asyncLock.HasValue);
                Assert.AreEqual(42, asyncLock.Value);
            }
        }
        public async Task AsyncLockWithValue_ThrowOnValueOverride([Values(true, false)] bool async)
        {
            var alwv = new AsyncLockWithValue <int>();

            AsyncLockWithValue <int> .LockOrValue asyncLock;

            Assert.IsFalse(alwv.HasValue);
            Assert.IsFalse(alwv.TryGetValue(out _));
            using (asyncLock = await alwv.GetLockOrValueAsync(async).ConfigureAwait(false))
            {
                Assert.IsFalse(asyncLock.HasValue);
                asyncLock.SetValue(42);
            }

            Assert.IsTrue(alwv.HasValue);
            Assert.IsTrue(alwv.TryGetValue(out var value));
            Assert.AreEqual(42, value);
            using (asyncLock = await alwv.GetLockOrValueAsync(async).ConfigureAwait(false))
            {
                Assert.IsTrue(asyncLock.HasValue);
                Assert.Throws <InvalidOperationException>(() => asyncLock.SetValue(6 * 9));
            }
        }
 public LockOrValue(AsyncLockWithValue <T> owner, long index)
 {
     _owner = owner;
     _index = index;
     _value = default;
 }
 public LockOrValue(T value)
 {
     _owner = default;
     _value = value;
     _index = 0;
 }