Exemplo n.º 1
0
        public void CanHaveMultipleWorkers()
        {
            const int workItemCount = 50;
            var       latch         = new CountDownLatch(workItemCount);
            int       errorCount    = 0;
            int       abandonCount  = 0;
            var       queue         = new InMemoryQueue <SimpleWorkItem>(retries: 1, workItemTimeoutMilliseconds: 50, retryDelayMilliseconds: 0);

            Parallel.For(0, workItemCount, i => queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello",
                Id   = i
            }));

            Task.Factory.StartNew(() => queue.StartWorking(w => DoWork(w, latch, ref abandonCount, ref errorCount)));
            Task.Factory.StartNew(() => queue.StartWorking(w => DoWork(w, latch, ref abandonCount, ref errorCount)));
            Task.Factory.StartNew(() => queue.StartWorking(w => DoWork(w, latch, ref abandonCount, ref errorCount)));

            bool success = latch.Wait(2000);

            Assert.True(success, "Failed to receive all work items.");
            Task.Delay(50).Wait();
            Assert.Equal(workItemCount, queue.Completed + queue.DeadletterCount);
            Assert.Equal(errorCount, queue.WorkerErrors);
            Assert.Equal(abandonCount + errorCount, queue.Abandoned);
        }
        public void CanTolerateSubscriberFailure()
        {
            if (_messageBus == null)
            {
                return;
            }

            var latch = new CountDownLatch(2);

            _messageBus.Subscribe <SimpleMessageA>(msg => {
                throw new ApplicationException();
            });
            _messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            _messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            _messageBus.Publish(new SimpleMessageA {
                Data = "Hello"
            });

            bool success = latch.Wait(5000);

            Assert.True(success, "Failed to receive all messages.");
        }
        public void CanSubscribeToAllMessageTypes()
        {
            if (_messageBus == null)
            {
                return;
            }

            var latch = new CountDownLatch(3);

            _messageBus.Subscribe <object>(msg => {
                latch.Signal();
            });
            _messageBus.Publish(new SimpleMessageA {
                Data = "Hello"
            });
            _messageBus.Publish(new SimpleMessageB {
                Data = "Hello"
            });
            _messageBus.Publish(new SimpleMessageC {
                Data = "Hello"
            });

            bool success = latch.Wait(5000);

            Assert.True(success, "Failed to receive all messages.");
        }
 private BacktoryResponse <ConnectResponse> connectSynchronized()
 {
     connectLatch = new CountDownLatch(LATCH_COUNT);
     Debug.Log(TAG + "connecting");
     connectorStateEngine.ChangeState(ConnectorStateEngine.StateChangeEvent.CONNECT);
     try {
         if (connectLatch.Wait(TIMEOUT_IN_SECONDS * 1000))
         {
             if (connectorStateEngine.getConnectionState() == ConnectorStateEngine.ConnectorState.CONNECTED)
             {
                 ConnectResponse response = new ConnectResponse(webSocket.userId, webSocket.username);
                 return(BacktoryResponse <ConnectResponse> .Success((int)BacktoryHttpStatusCode.OK, response));
             }
             else
             {
                 return(new BacktoryResponse <ConnectResponse> (1000, "Error connecting", default(ConnectResponse), false));
             }
         }
         else
         {
             Debug.LogError(TAG + "Timeout waiting to connect");
             connectorStateEngine.ChangeState(ConnectorStateEngine.StateChangeEvent.GENERAL_ERROR);
             return(new BacktoryResponse <ConnectResponse> (1000, "Timeout waiting to connect", default(ConnectResponse), false));
         }
     } catch (Exception e) {
         Debug.LogError(TAG + e.Data);
         return(new BacktoryResponse <ConnectResponse> (1000, "Error while connecting", default(ConnectResponse), false));
     }
 }
        public void CanSendMessageToMultipleSubscribers()
        {
            if (_messageBus == null)
            {
                return;
            }

            var latch = new CountDownLatch(3);

            _messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            _messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            _messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            _messageBus.Publish(new SimpleMessageA {
                Data = "Hello"
            });

            bool success = latch.Wait(15000);

            Assert.True(success, "Failed to receive all messages.");
        }
        public void CanHaveMultipleWorkers()
        {
            if (_queue == null)
            {
                return;
            }

            const int workItemCount = 50;
            var       latch         = new CountDownLatch(workItemCount);
            int       errorCount    = 0;
            int       abandonCount  = 0;

            Task.Factory.StartNew(() => _queue.StartWorking(w => DoWork(w, latch, ref abandonCount, ref errorCount)));
            Task.Factory.StartNew(() => _queue.StartWorking(w => DoWork(w, latch, ref abandonCount, ref errorCount)));
            Task.Factory.StartNew(() => _queue.StartWorking(w => DoWork(w, latch, ref abandonCount, ref errorCount)));

            Parallel.For(0, workItemCount, i => _queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello",
                Id   = i
            }));

            Assert.Equal(workItemCount, _queue.Enqueued);
            bool success = latch.Wait(60000);

            Task.Delay(5000).Wait();
            Assert.True(success, "Failed to receive all work items.");
            Assert.Equal(workItemCount, _queue.Completed + _queue.DeadletterCount);
            Assert.Equal(errorCount, _queue.WorkerErrors);
            Assert.Equal(abandonCount + errorCount, _queue.Abandoned);
        }
        public void CanSendMessageToMultipleSubscribers()
        {
            var latch      = new CountDownLatch(3);
            var messageBus = new InMemoryMessageBus();

            messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            messageBus.PublishAsync(new SimpleMessageA {
                Data = "Hello"
            });

            bool success = latch.Wait(100);

            Assert.True(success, "Failed to recieve all messages.");
        }
        public void WillReceiveDerivedMessageTypes()
        {
            if (_messageBus == null)
            {
                return;
            }

            var latch = new CountDownLatch(2);

            _messageBus.Subscribe <ISimpleMessage>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            _messageBus.Publish(new SimpleMessageA {
                Data = "Hello"
            });
            _messageBus.Publish(new SimpleMessageB {
                Data = "Hello"
            });
            _messageBus.Publish(new SimpleMessageC {
                Data = "Hello"
            });

            bool success = latch.Wait(5000);

            Assert.True(success, "Failed to receive all messages.");
        }
Exemplo n.º 9
0
        public void StartDataTransfer(int timeout = 0)
        {
            if (timeout < 0)
            {
                throw new ArgumentException("timeout may not be negative");
            }

            startdtConSignal = new CountDownLatch(1);

            writer.Write(StartdtActBuffer, 0, StartdtActBuffer.Length);
            writer.Flush();

            if (timeout == 0)
            {
                startdtConSignal.Wait();
            }
            else
            {
                startdtConSignal.Wait(timeout);
            }
        }
Exemplo n.º 10
0
        public void CanPostManyEvents()
        {
            _eventQueue.DeleteQueue();
            RemoveAllEvents();

            const int batchSize  = 250;
            const int batchCount = 10;

            try {
                var countdown         = new CountDownLatch(10);
                var messageSubscriber = IoC.GetInstance <IMessageSubscriber>();
                messageSubscriber.Subscribe <EntityChanged>(ch => {
                    if (ch.ChangeType != ChangeType.Added || ch.Type != typeof(PersistentEvent).Name)
                    {
                        return;
                    }

                    if (countdown.Remaining <= 0)
                    {
                        throw new ApplicationException("Too many change notifications.");
                    }

                    countdown.Signal();
                });

                Parallel.For(0, batchCount, i => {
                    _eventController.Request = CreateRequestMessage(new ClaimsPrincipal(IdentityUtils.CreateUserIdentity(TestConstants.UserEmail, TestConstants.UserId, new[] { TestConstants.OrganizationId }, new[] { AuthorizationRoles.Client }, TestConstants.ProjectId)), true, false);
                    var events           = new RandomEventGenerator().Generate(batchSize);
                    var compressedEvents = Encoding.UTF8.GetBytes(new DefaultJsonSerializer().Serialize(events)).Compress();
                    var actionResult     = _eventController.Post(compressedEvents, version: 2, userAgent: "exceptionless/2.0.0.0");
                    Assert.IsType <StatusCodeResult>(actionResult);
                });

                Assert.Equal(batchCount, _eventQueue.GetQueueCount());

                var sw = new Stopwatch();
                var processEventsJob = IoC.GetInstance <EventPostsJob>();
                sw.Start();
                processEventsJob.RunUntilEmpty();
                sw.Stop();
                Trace.WriteLine(sw.Elapsed);

                Assert.Equal(0, _eventQueue.GetQueueCount());
                Assert.Equal(batchSize * batchCount, EventCount());

                bool success = countdown.Wait(5000);
                Assert.True(success);
            } finally {
                _eventQueue.DeleteQueue();
            }
        }
        public void WillSaveOnce()
        {
            var latch   = new CountDownLatch(2);
            var storage = new InMemoryFileStorage();
            var dict    = new PersistedDictionary("test.json", storage, new DefaultJsonSerializer(), 50);

            dict.Saved += (sender, args) => latch.Signal();
            for (int i = 0; i < 10; i++)
            {
                dict["test" + i] = i.ToString();
            }
            Assert.Equal(10, dict.Count);
            bool success = latch.Wait(250);

            Assert.False(success, "Dictionary was saved multiple times.");
            Assert.Equal(1, latch.Remaining);
            Assert.True(storage.Exists("test.json"));

            dict["test"] = "test";
            Assert.Equal(11, dict.Count);
            success = latch.Wait(250);
            Assert.True(success, "Failed to save dictionary.");
            Assert.True(storage.Exists("test.json"));
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            CountDownLatch cdl = new CountDownLatch(10);

            for (int i = 0; i < 10; i++)
            {
                Task t = new Task(() =>
                {
                    Thread.Sleep(1000);
                    cdl.CountDown();
                    Console.WriteLine(cdl.GetCountDownNumber());
                });
                t.Start();
            }
            cdl.Wait();
            Console.WriteLine("over");
            Console.ReadLine();
        }
Exemplo n.º 13
0
        public void MeasureWorkerThroughput()
        {
            var queue = GetQueue(retries: 3, workItemTimeout: TimeSpan.FromSeconds(2), retryDelay: TimeSpan.FromSeconds(1));

            if (queue == null)
            {
                return;
            }

            FlushAll();

            using (queue) {
                queue.DeleteQueue();

                const int workItemCount = 10000;
                for (int i = 0; i < workItemCount; i++)
                {
                    queue.Enqueue(new SimpleWorkItem {
                        Data = "Hello"
                    });
                }
                Assert.Equal(workItemCount, queue.GetQueueCount());

                var countdown = new CountDownLatch(workItemCount);
                var metrics   = new InMemoryMetricsClient();
                queue.StartWorking(workItem => {
                    Assert.Equal("Hello", workItem.Value.Data);
                    workItem.Complete();
                    metrics.Counter("work");
                    countdown.Signal();
                });
                countdown.Wait(60 * 1000);
                metrics.DisplayStats();

                Assert.Equal(workItemCount, queue.DequeuedCount);
                Assert.Equal(workItemCount, queue.CompletedCount);
                Assert.Equal(0, queue.GetQueueCount());

                Trace.WriteLine(CountAllKeys());
            }
        }
Exemplo n.º 14
0
        public void CanSubscribeToAllMessageTypes()
        {
            var latch = new CountDownLatch(3);

            _messageBus.Value.Subscribe <object>(msg => {
                latch.Signal();
            });
            _messageBus.Value.PublishAsync(new SimpleMessageA {
                Data = "Hello"
            });
            _messageBus.Value.PublishAsync(new SimpleMessageB {
                Data = "Hello"
            });
            _messageBus.Value.PublishAsync(new SimpleMessageC {
                Data = "Hello"
            });

            bool success = latch.Wait(5000);

            Assert.True(success, "Failed to recieve all messages.");
        }
 private BacktoryResponse <BacktoryVoid> disconnectSyncronized()
 {
     disconnectLatch = new CountDownLatch(LATCH_COUNT);
     Debug.Log(TAG + "disconnecting");
     connectorStateEngine.ChangeState(ConnectorStateEngine.StateChangeEvent.DISCONNECT);
     try {
         if (disconnectLatch.Wait(TIMEOUT_IN_SECONDS * 1000))
         {
             return(BacktoryResponse <BacktoryVoid> .Success(200, default(BacktoryVoid)));
         }
         else
         {
             Debug.LogError(TAG + "Timeout waiting to connect");
             connectorStateEngine.ChangeState(ConnectorStateEngine.StateChangeEvent.GENERAL_ERROR);
             return(new BacktoryResponse <BacktoryVoid> (1000, "Timeout waiting to connect", default(BacktoryVoid), false));
         }
     } catch (Exception e) {
         Debug.LogError(TAG + e.Data);
         return(new BacktoryResponse <BacktoryVoid> (1000, "Error while connecting", default(BacktoryVoid), false));
     }
 }
        public void CanSubscribeToAllMessageTypes()
        {
            var latch      = new CountDownLatch(3);
            var messageBus = new InMemoryMessageBus();

            messageBus.Subscribe <object>(msg => {
                latch.Signal();
            });
            messageBus.Publish(new SimpleMessageA {
                Data = "Hello"
            });
            messageBus.Publish(new SimpleMessageB {
                Data = "Hello"
            });
            messageBus.Publish(new SimpleMessageC {
                Data = "Hello"
            });

            bool success = latch.Wait(100);

            Assert.True(success, "Failed to receive all messages.");
        }
        public void WillReceiveDerivedMessageTypes()
        {
            var latch      = new CountDownLatch(2);
            var messageBus = new InMemoryMessageBus();

            messageBus.Subscribe <ISimpleMessage>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            messageBus.PublishAsync(new SimpleMessageA {
                Data = "Hello"
            });
            messageBus.PublishAsync(new SimpleMessageB {
                Data = "Hello"
            });
            messageBus.PublishAsync(new SimpleMessageC {
                Data = "Hello"
            });

            bool success = latch.Wait(100);

            Assert.True(success, "Failed to receive all messages.");
        }
        public void CanTolerateSubscriberFailure()
        {
            var latch      = new CountDownLatch(2);
            var messageBus = new InMemoryMessageBus();

            messageBus.Subscribe <SimpleMessageA>(msg => {
                throw new ApplicationException();
            });
            messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            messageBus.Subscribe <SimpleMessageA>(msg => {
                Assert.Equal("Hello", msg.Data);
                latch.Signal();
            });
            messageBus.PublishAsync(new SimpleMessageA {
                Data = "Hello"
            });

            bool success = latch.Wait(900);

            Assert.True(success, "Failed to receive all messages.");
        }
Exemplo n.º 19
0
        public void CanPostManyEvents() {
            _eventQueue.DeleteQueue();
            RemoveAllEvents();

            const int batchSize = 250;
            const int batchCount = 10;

            try {
                var countdown = new CountDownLatch(10);
                var messageSubscriber = IoC.GetInstance<IMessageSubscriber>();
                messageSubscriber.Subscribe<EntityChanged>(ch => {
                    if (ch.ChangeType != ChangeType.Added || ch.Type != typeof(PersistentEvent).Name)
                        return;

                    if (countdown.Remaining <= 0)
                        throw new ApplicationException("Too many change notifications.");

                    countdown.Signal();
                });

                Parallel.For(0, batchCount, i => {
                    _eventController.Request = CreateRequestMessage(new ClaimsPrincipal(IdentityUtils.CreateUserIdentity(TestConstants.UserEmail, TestConstants.UserId, new[] { TestConstants.OrganizationId }, new[] { AuthorizationRoles.Client }, TestConstants.ProjectId)), true, false);
                    var events = new RandomEventGenerator().Generate(batchSize);
                    var compressedEvents = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(events)).CompressAsync().Result;
                    var actionResult = _eventController.PostAsync(compressedEvents, version: 2, userAgent: "exceptionless/2.0.0.0").Result;
                    Assert.IsType<StatusCodeResult>(actionResult);
                });

                Assert.Equal(batchCount, _eventQueue.GetQueueCount());

                var sw = new Stopwatch();
                var processEventsJob = IoC.GetInstance<EventPostsJob>();
                sw.Start();
                processEventsJob.RunUntilEmpty();
                sw.Stop();
                Trace.WriteLine(sw.Elapsed);

                Assert.Equal(0, _eventQueue.GetQueueCount());
                Assert.Equal(batchSize * batchCount, EventCount());

                bool success = countdown.Wait(5000);
                Assert.True(success);
            } finally {
                _eventQueue.DeleteQueue();
            }
        }