internal async Task GenerateWithValueType <ValueType>(string logFolder, ValueType value, IStateSerializer <ValueType> valueSerializer)
        {
            // directory setup
            if (FabricDirectory.Exists(logFolder))
            {
                FabricDirectory.Delete(logFolder, true);
            }
            FabricDirectory.CreateDirectory(logFolder);

            var rand = new Random();

            var reliabilitySimulator = new ReliabilitySimulator(
                logFolder,
                new Uri("fabric:/unittest/service" + rand.Next()), // random service name.
                OnDataLossCallback,                                // we are never calling OnDataLossAsync on this ReliabilitySimulator.
                CreateStateProvider);

            reliabilitySimulator.CreateReplica(true, false);

            var replicator = reliabilitySimulator.GetTransactionalReplicator();

            replicator.TryAddStateSerializer <ValueType>(valueSerializer);

            // Constants
            var distributedDictionary = new DistributedDictionary <long, ValueType>();
            var distributedQueue      = new DistributedQueue <ValueType>();
            var concurrentQueue       = new ReliableConcurrentQueue <ValueType>();

            // Setup
            await reliabilitySimulator.OpenReplicaAsync(ReplicaOpenMode.New).ConfigureAwait(false);

            await reliabilitySimulator.PromoteReplicaAsync(false).ConfigureAwait(false);

            var result = replicator.CreateAsyncEnumerable(false, false);

            Assert.AreEqual(0, result.ToEnumerable().Count(), "State Manager must be empty");

            // Write data.
            using (var txn = replicator.CreateTransaction())
            {
                await replicator.AddStateProviderAsync(txn, DictionaryName, distributedDictionary).ConfigureAwait(false);

                await txn.CommitAsync().ConfigureAwait(false);
            }

            result = replicator.CreateAsyncEnumerable(false, false);
            Assert.AreEqual(2, result.ToEnumerable().Count(), "State Manager must include all the state providers");

            await PopulateDictionaryAsync(replicator, DictionaryName, 0, 8, 8, value).ConfigureAwait(false);

            // Take backup
            await replicator.BackupAsync(ComplexDataBackupCallbackAsync).ConfigureAwait(false);

            // Clean up.
            await reliabilitySimulator.DropReplicaAsync();
        }
コード例 #2
0
        public async Task GetOrDefaultAsync_WithMissingKey_ReturnsDefault()
        {
            // Arrange
            var cache = CreateDistributedCache();
            var dic   = new DistributedDictionary <Guid, TestPerson>(cache, new OptionsWrapper <DistributedDictionaryOptions>(new DistributedDictionaryOptions()));
            var key   = Guid.Parse("054392d5-1a51-4f8a-96c7-2a3eb0db2842");

            // Act
            var result = await dic.GetOrDefaultAsync(key);

            // Assert
            Assert.Null(result);
        }
コード例 #3
0
        public async Task GetAsync_WithMissingKey_ThrowsKeyNotFound()
        {
            // Arrange
            var cache = CreateDistributedCache();
            var dic   = new DistributedDictionary <Guid, TestPerson>(cache, new OptionsWrapper <DistributedDictionaryOptions>(new DistributedDictionaryOptions()));
            var key   = Guid.Parse("054392d5-1a51-4f8a-96c7-2a3eb0db2842");

            // Act
            var act = dic.GetAsync(key);

            // Assert
            await Assert.ThrowsAsync <KeyNotFoundException>(() => act);
        }
コード例 #4
0
                /// <inheritdoc />
                public IDistributedDictionary <TKey, TValue> GetOrCreateDictionary <TKey, TValue>(string name)
                {
                    lock (this.@lock)
                    {
                        if (this.dictionaryMap.ContainsKey(name))
                        {
                            return((IDistributedDictionary <TKey, TValue>) this.dictionaryMap[name]);
                        }

                        var dictionary = new DistributedDictionary <TKey, TValue>(name);
                        var message    = new DictionaryCreatedMessage(this, name, typeof(TKey), typeof(TValue));
                        this.outMessengerService.Send(this.curator.GetNodes(), message);
                        this.dictionaryMap.TryAdd(name, dictionary);
                        return(dictionary);
                    }
                }
コード例 #5
0
        public async Task GetOrDefaultAsync_WithDefaultOptions_ReturnsCorrectObject()
        {
            // Arrange
            var cache = CreateDistributedCache();
            await cache.SetAsync("testhost:SimpleConcepts.DistributedDictionary.Tests.TestPerson:054392d5-1a51-4f8a-96c7-2a3eb0db2842",
                                 new byte[] { 123, 34, 78, 97, 109, 101, 34, 58, 34, 82, 97, 112, 104, 97, 101, 108, 34, 44, 34, 65, 103, 101, 34, 58, 51, 49, 125 });

            var dic = new DistributedDictionary <Guid, TestPerson>(cache, new OptionsWrapper <DistributedDictionaryOptions>(new DistributedDictionaryOptions()));
            var key = Guid.Parse("054392d5-1a51-4f8a-96c7-2a3eb0db2842");

            // Act
            var result = await dic.GetOrDefaultAsync(key);

            // Assert
            Assert.Equal("Raphael", result.Name);
            Assert.Equal(31, result.Age);
        }
コード例 #6
0
        public async Task SetAsync_WithDefaultOptions_PutsCorrectBytesOnCorrectKey()
        {
            // Arrange
            var cache = CreateDistributedCache();
            var dic   = new DistributedDictionary <Guid, TestPerson>(cache, new OptionsWrapper <DistributedDictionaryOptions>(new DistributedDictionaryOptions()));
            var key   = Guid.Parse("054392d5-1a51-4f8a-96c7-2a3eb0db2842");
            var value = new TestPerson {
                Name = "Raphael", Age = 31
            };

            // Act
            await dic.SetAsync(key, value);

            // Assert
            var bytes = await cache.GetAsync("testhost:SimpleConcepts.DistributedDictionary.Tests.TestPerson:054392d5-1a51-4f8a-96c7-2a3eb0db2842");

            Assert.Equal(new byte[] { 123, 34, 78, 97, 109, 101, 34, 58, 34, 82, 97, 112, 104, 97, 101, 108, 34, 44, 34, 65, 103, 101, 34, 58, 51, 49, 125 }, bytes);
        }
コード例 #7
0
        public async Task RemoveAsync_WithDefaultOptions_RemovesFromCache()
        {
            // Arrange
            var cache = CreateDistributedCache();
            await cache.SetAsync("testhost:SimpleConcepts.DistributedDictionary.Tests.TestPerson:054392d5-1a51-4f8a-96c7-2a3eb0db2842",
                                 new byte[] { 123, 34, 78, 97, 109, 101, 34, 58, 34, 82, 97, 112, 104, 97, 101, 108, 34, 44, 34, 65, 103, 101, 34, 58, 51, 49, 125 });

            var dic = new DistributedDictionary <Guid, TestPerson>(cache, new OptionsWrapper <DistributedDictionaryOptions>(new DistributedDictionaryOptions()));
            var key = Guid.Parse("054392d5-1a51-4f8a-96c7-2a3eb0db2842");

            // Act
            await dic.RemoveAsync(key);

            // Assert
            var bytes = await cache.GetAsync("testhost:SimpleConcepts.DistributedDictionary.Tests.TestPerson:054392d5-1a51-4f8a-96c7-2a3eb0db2842");

            Assert.Null(bytes);
        }
コード例 #8
0
        protected static async Task GenerateBackup(string logFolder)
        {
            const int NumberOfStateProviders = 6;

            if (Directory.Exists(logFolder))
            {
                Directory.Delete(logFolder, true);
            }

            Directory.CreateDirectory(logFolder);

            var reliabilitySimulator = new ReliabilitySimulator(
                logFolder,
                new Uri("fabric:/unittest/service"),
                DateTime.UtcNow.ToFileTimeUtc(),
                DateTime.UtcNow.ToFileTimeUtc(),
                OnDataLossCallback,
                CreateStateProvider);

            reliabilitySimulator.CreateReplica(true, false);

            var replicator = reliabilitySimulator.GetTransactionalReplicator();

            // Constants
            var distributedDictionary = new DistributedDictionary <long, long>();
            var distributedQueue      = new DistributedQueue <long>();
            var concurrentQueue       = new ReliableConcurrentQueue <long>();

            // Setup
            await reliabilitySimulator.OpenReplicaAsync(ReplicaOpenMode.New).ConfigureAwait(false);

            await reliabilitySimulator.PromoteReplicaAsync(false).ConfigureAwait(false);

            var result = replicator.CreateAsyncEnumerable(false, false);

            Assert.AreEqual(0, result.ToEnumerable().Count(), "State Manager must be empty");

            // Write data.
            using (var txn = replicator.CreateTransaction())
            {
                await replicator.AddStateProviderAsync(txn, DictionaryName, distributedDictionary).ConfigureAwait(false);

                await replicator.AddStateProviderAsync(txn, QueueName, distributedQueue).ConfigureAwait(false);

                await replicator.AddStateProviderAsync(txn, ConcurrentQueueName, concurrentQueue).ConfigureAwait(false);

                await txn.CommitAsync().ConfigureAwait(false);
            }

            result = replicator.CreateAsyncEnumerable(false, false);
            Assert.AreEqual(NumberOfStateProviders, result.ToEnumerable().Count(), "State Manager must include all the state providers");

            // Assumptions by tests for the backup generated.
            // 1. TotalDictionaryInserts contains total no of keys in dictionary.
            // 2. If there are n keys in dictionary, then they are from 0 - (n-1).

            int batchCount = 8, batchSize = 8;
            await CollectionHelper.PopulateDictionaryAsync(replicator, DictionaryName, 0, batchCount, batchSize).ConfigureAwait(false);

            await CollectionHelper.PopulateQueueAsync(replicator, QueueName, 0, batchCount, batchSize).ConfigureAwait(false);

            await CollectionHelper.PopulateConcurrentQueueAsync(replicator, ConcurrentQueueName, 0, batchCount, batchSize).ConfigureAwait(false);

            // Take backup
            await replicator.BackupAsync(BackupCallbackAsync).ConfigureAwait(false);

            // Clean up.
            await reliabilitySimulator.DropReplicaAsync();

            NumOperationsPerTransaction       = batchSize;
            TotalDictionaryInserts            = batchCount * batchSize;
            TotalDictionaryTransactions       = batchCount;
            TotalQueueTransactions            = batchCount;
            TotoalConcurrentQueueTransactions = batchCount;
            // + 1 for backup
            TotalTransactions = TotalDictionaryTransactions + TotalQueueTransactions + TotoalConcurrentQueueTransactions + 1;
        }