public async Task TryGetValueAsyncTest()
        {
            DictionaryChange change = null;
            TransactedConcurrentDictionary <int, string> d = new TransactedConcurrentDictionary <int, string>(
                new Uri("test://mocks", UriKind.Absolute),
                (c) =>
            {
                change = c;
                return(true);
            }
                );

            using (var tx = _stateManager.CreateTransaction())
            {
                Assert.IsFalse((await d.TryGetValueAsync(tx, 1, LockMode.Default)).HasValue);
                Assert.IsTrue(await d.TryAddAsync(tx, 1, "One"));

                using (var tx2 = _stateManager.CreateTransaction())
                {
                    await Assert.ThrowsExceptionAsync <TimeoutException>(
                        async() =>
                    {
                        await d.TryGetValueAsync(tx2, 1, LockMode.Default, timeout: TimeSpan.FromMilliseconds(20));
                    }
                        );
                }
                await tx.CommitAsync();

                Assert.AreEqual("One", change.Added);
            }
        }
 private async Task <ConditionalValue <string> > GetValue(TransactedConcurrentDictionary <int, string> d, int key, TimeSpan timeout = default(TimeSpan))
 {
     using (var tx = _stateManager.CreateTransaction())
     {
         return(await d.TryGetValueAsync(tx, key, LockMode.Default, timeout : timeout));
     }
 }
コード例 #3
0
        public async Task <ConditionalValue <T> > TryGetAsync <T>(Uri name) where T : IReliableState
        {
            using (var tx = CreateTransaction())
            {
                var result = await _store.TryGetValueAsync(tx, name, LockMode.Default);

                return(new ConditionalValue <T>(result.HasValue, (T)result.Value));
            }
        }
        public async Task SetAsyncTest()
        {
            DictionaryChange change = null;
            TransactedConcurrentDictionary <int, string> d = new TransactedConcurrentDictionary <int, string>(
                new Uri("test://mocks", UriKind.Absolute),
                (c) =>
            {
                change = c;
                return(true);
            }
                );

            using (var tx = _stateManager.CreateTransaction())
            {
                await d.SetAsync(tx, 1, "Zero");
            }
            using (var tx = _stateManager.CreateTransaction())
            {
                ConditionalValue <string> value = await d.TryGetValueAsync(tx, 1, LockMode.Default);

                Assert.IsFalse(value.HasValue);
            }

            using (var tx = _stateManager.CreateTransaction())
            {
                await d.SetAsync(tx, 1, "One");

                await tx.CommitAsync();

                Assert.AreEqual("One", change.Added);
                Assert.AreEqual(null, change.Removed);
            }

            using (var tx = _stateManager.CreateTransaction())
            {
                await d.SetAsync(tx, 1, "Two");
            }
            using (var tx = _stateManager.CreateTransaction())
            {
                ConditionalValue <string> value = await d.TryGetValueAsync(tx, 1, LockMode.Default);

                Assert.AreEqual("One", value.Value);
            }

            using (var tx = _stateManager.CreateTransaction())
            {
                await d.AddAsync(tx, 2, "Two");

                await tx.CommitAsync();
            }

            using (var tx = _stateManager.CreateTransaction())
            {
                await d.SetAsync(tx, 2, "Three");

                await tx.CommitAsync();

                Assert.AreEqual("Two", change.Removed);
                Assert.AreEqual("Three", change.Added);
            }

            Assert.AreEqual(2, await GetCount(d));
        }