public void createNremoveMelementTest(String dir, int n, int m) { String testString = "Hello World"; int num_clients = 2; clients = new ZooKeeper[num_clients]; DistributedQueue[] queueHandles = new DistributedQueue[num_clients]; for (int i = 0; i < clients.Length; i++) { clients[i] = CreateClient(); queueHandles[i] = new DistributedQueue(clients[i], dir, null); } for (int i = 0; i < n; i++) { String offerString = testString + i; queueHandles[0].Enqueue(offerString.GetBytes()); } byte[] data = null; for (int i = 0; i < m; i++) { data = queueHandles[1].Dequeue(); } Assert.AreEqual(Encoding.UTF8.GetString(queueHandles[1].Peek()), testString + m); }
public void testRemove1() { String dir = "/testRemove1" + Guid.NewGuid(); //String testString = "Hello World"; int num_clients = 1; clients = new ZooKeeper[num_clients]; DistributedQueue[] queueHandles = new DistributedQueue[num_clients]; for (int i = 0; i < clients.Length; i++) { clients[i] = CreateClient(); queueHandles[i] = new DistributedQueue(clients[i], dir, null); } try { queueHandles[0].Dequeue(); } catch (NoSuchElementException) { return; } Assert.Fail(); //for (int i = 0; i < clients.Length; i++) //{ // clients[i].Dispose(); //} }
public void Initialize() { var address = Configurations.ZooKeeperAddress; _zk = ZooKeeperFactory.Instance.Connect(address); _queue = new DistributedQueue(_zk, "/QueueTest"); }
public void testTakeWait2() { String dir = "/testTakeWait2" + Guid.NewGuid(); string testString = "Hello World"; int num_clients = 1; clients = new ZooKeeper[num_clients]; DistributedQueue[] queueHandles = new DistributedQueue[num_clients]; for (int i = 0; i < clients.Length; i++) { clients[i] = CreateClient(); queueHandles[i] = new DistributedQueue(clients[i], dir, null); } int num_attempts = 2; for (int i = 0; i < num_attempts; i++) { byte[][] takeResult = new byte[1][]; string threadTestString = testString + i; var wait = new ManualResetEvent(false); int maxRetry = 10; ThreadPool.QueueUserWorkItem(st => { try { takeResult[0] = queueHandles[0].Take(); wait.Set(); } catch (KeeperException e) { } catch (ThreadInterruptedException e) { } }); Thread.Sleep(1000); ThreadPool.QueueUserWorkItem(st => { try { while (!wait.WaitOne(1000) && maxRetry-- > 0) { queueHandles[0].Enqueue(threadTestString.GetBytes()); } } catch (KeeperException e) { } catch (ThreadInterruptedException e) { } }); Assert.True(wait.WaitOne(3000), "Failed to take"); Assert.True(takeResult[0] != null); Assert.AreEqual(threadTestString, Encoding.UTF8.GetString(takeResult[0])); } }
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(); }
public void testTake1() { String dir = "/testTake1" + Guid.NewGuid(); String testString = "Hello World"; int num_clients = 1; clients = new ZooKeeper[num_clients]; DistributedQueue[] queueHandles = new DistributedQueue[num_clients]; for (int i = 0; i < clients.Length; i++) { clients[i] = CreateClient(); queueHandles[i] = new DistributedQueue(clients[i], dir, null); } queueHandles[0].Enqueue(testString.GetBytes()); byte[] dequeuedBytes = queueHandles[0].Take(); Assert.AreEqual(Encoding.UTF8.GetString(dequeuedBytes), testString); }
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; }