Ejemplo n.º 1
0
        public void MessageOfUnknownTypeShouldPauseProcessingTillCorrespondingHandlerIsRegisteredTest()
        {
            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                var type1Received = new AutoResetEvent(false);
                var type2Received = new AutoResetEvent(false);

                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
                {
                    type1Received.Set();
                    acknowledge(true);
                }, "type1");

                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
                }, 0);
                Assert.That(type1Received.WaitOne(500), Is.True, "Message of subscribed type was not delivered");
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2"
                }, 0);
                //Give time for type2 message to be  pushed back by mq
                //Thread.Sleep(500);
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
                }, 0);
                Assert.That(type1Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing");
                Assert.That(type2Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing");
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { type2Received.Set();
                                                                                  acknowledge(true); }, "type2");
                Assert.That(type1Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered");
                Assert.That(type2Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered");
            }
        }
Ejemplo n.º 2
0
        public void UnknownMessageTypeHandlerWaitingDoesNotPreventTransportDisposeTest()
        {
            var    received         = new ManualResetEvent(false);
            Thread connectionThread = null;

            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
                {
                    connectionThread = Thread.CurrentThread;
                    received.Set();
                }, "type1");
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
                }, 0);
                Assert.That(received.WaitOne(100), Is.True, "Message was not delivered");
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2"
                }, 0);
            }

            Thread.Sleep(200);
            Assert.That(connectionThread.ThreadState, Is.EqualTo(ThreadState.Stopped), "Processing thread is still active in spite of transport dispose");
        }
Ejemplo n.º 3
0
        private void ensureProcessingGroupIsCreated(string destination)
        {
            if (m_Instance != null)
            {
                if (m_IsQueueGroup != isQueue(destination))
                {
                    throw new InvalidOperationException(string.Format("Can not process {0} {1} as it is already used for {2} processing. Sonic does not support processing topic and queue in same thread", m_IsQueueGroup ? "Topic" : "Queue", destination, m_IsQueueGroup ? "Queue" : "Topic"));
                }
                return;
            }

            lock (m_SyncRoot)
            {
                if (m_Instance == null)
                {
                    m_IsQueueGroup = isQueue(destination);
                    if (m_IsQueueGroup)
                    {
                        m_Instance = new QueueProcessingGroup(m_Connection, m_JailedTag, m_MessageFormat);
                    }
                    else
                    {
                        m_Instance = new TopicProcessingGroup(m_Connection, m_JailedTag, m_MessageFormat);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void SendTest()
        {
            using (var transport = new InMemoryTransport())
            {
                var delivered1 = new ManualResetEvent(false);
                var delivered2 = new ManualResetEvent(false);
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Subscribe(TEST_TOPIC, (message, ack) =>
                {
                    delivered1.Set();
                    Console.WriteLine("subscription1: message:" + message.Type);
                }, typeof(byte[]).Name);

                processingGroup.Subscribe(TEST_TOPIC, (message, ack) =>
                {
                    delivered2.Set();
                    Console.WriteLine("subscription2: message:" + message.Type);
                }, typeof(byte[]).Name);


                processingGroup.Send(TEST_TOPIC, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name
                }, 0);


                Assert.That(delivered1.WaitOne(1000), Is.True, "message was not delivered to all subscribers");
                Assert.That(delivered2.WaitOne(1000), Is.True, "message was not delivered to all subscribers");
                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 5
0
        public void NackTest()
        {
            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                var delivered = new ManualResetEvent(false);
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name
                }, 0);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
                {
                    Console.WriteLine("message:" + message.Type);
                    delivered.Set();
                    acknowledge(false);
                }, typeof(byte[]).Name);
                Assert.That(delivered.WaitOne(300), Is.True, "Message was not delivered");
            }

            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                var delivered = new ManualResetEvent(false);
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => delivered.Set(), typeof(byte[]).Name);
                Assert.That(delivered.WaitOne(1000), Is.True, "Message was not returned to queue");
            }
        }
Ejemplo n.º 6
0
        public void PerformanceTest(int messageSize)
        {
            var messageBytes = new byte[messageSize];

            new Random().NextBytes(messageBytes);

            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                Stopwatch        sw = Stopwatch.StartNew();
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = messageBytes, Type = typeof(byte[]).Name
                }, 0);
                int sendCounter;
                for (sendCounter = 0; sw.ElapsedMilliseconds < 4000; sendCounter++)
                {
                    processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                        Bytes = messageBytes, Type = typeof(byte[]).Name
                    }, 0);
                }
                int receiveCounter = 0;

                var ev = new ManualResetEvent(false);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => receiveCounter++, typeof(byte[]).Name);
                ev.WaitOne(2000);
                Console.WriteLine("Send: {0} per second. {1:0.00} Mbit/s", sendCounter / 4, 1.0 * sendCounter * messageSize / 4 / 1024 / 1024 * 8);
                Console.WriteLine("Receive: {0} per second. {1:0.00}  Mbit/s", receiveCounter / 2, 1.0 * receiveCounter * messageSize / 2 / 1024 / 1024 * 8);
            }
        }
Ejemplo n.º 7
0
 public void Dispose()
 {
     if (ProcessingGroup == null)
     {
         return;
     }
     ProcessingGroup.Dispose();
     ProcessingGroup = null;
 }
Ejemplo n.º 8
0
 public void AttemptToSubscribeSameDestinationWithoutMessageTypeTwiceFailureTest()
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, null);
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, null);
     }
 }
        internal void RemoveProcessingGroup(IProcessingGroup <T> processingGroup)
        {
            if (processingGroup == null)
            {
                throw new ArgumentNullException(nameof(processingGroup));
            }

            _processingGroups.Remove(processingGroup);
        }
Ejemplo n.º 10
0
 public void Dispose()
 {
     if (m_Instance != null)
     {
         lock (m_SyncRoot)
         {
             if (m_Instance != null)
             {
                 m_Instance.Dispose();
                 m_Instance = null;
             }
         }
     }
 }
Ejemplo n.º 11
0
 public void UnsubscribeTest(string messageType)
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         var ev = new AutoResetEvent(false);
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = messageType
         }, 0);
         IDisposable subscription = processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => ev.Set(), messageType);
         Assert.That(ev.WaitOne(500), Is.True, "Message was not delivered");
         subscription.Dispose();
         Assert.That(ev.WaitOne(500), Is.False, "Message was delivered for canceled subscription");
     }
 }
Ejemplo n.º 12
0
 public void UnsubscribeTest()
 {
     using (var transport = new InMemoryTransport())
     {
         var ev = new AutoResetEvent(false);
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         IDisposable      subscription    = processingGroup.Subscribe(TEST_TOPIC, (message, ack) => ev.Set(), null);
         processingGroup.Send(TEST_TOPIC, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = null
         }, 0);
         Assert.That(ev.WaitOne(500), Is.True, "Message was not delivered");
         subscription.Dispose();
         Assert.That(ev.WaitOne(500), Is.False, "Message was delivered for canceled subscription");
     }
 }
Ejemplo n.º 13
0
        public void DisposeTest()
        {
            ManualResetEvent delivered = new ManualResetEvent(false);
            int deliveredMessagesCount = 0;
            var transport = new InMemoryTransport();
            IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);

            processingGroup.Subscribe(TEST_TOPIC, (message, ack) =>
            {
                delivered.WaitOne();
                Interlocked.Increment(ref deliveredMessagesCount);
            }, null);
            processingGroup.Send(TEST_TOPIC, new BinaryMessage(), 0);
            Thread.Sleep(200);
            var task = Task.Factory.StartNew(transport.Dispose);

            Assert.That(task.Wait(200), Is.False, "transport was disposd before all message processing finished");
            delivered.Set();
            Assert.That(task.Wait(1000), Is.True, "transport was not disposd after all message processing finished");
        }
Ejemplo n.º 14
0
 public void ConnectionFailureTest()
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         var onFailureCalled = new AutoResetEvent(false);
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(() =>
         {
             onFailureCalled.Set();
             Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
         });
         processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType"
         }, 0);
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "messageType");
         FieldInfo field      = typeof(ProcessingGroup).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance);
         var       connection = field.GetValue(processingGroup) as IConnection;
         connection.Abort(1, "All your base are belong to us");
         Assert.That(onFailureCalled.WaitOne(500), Is.True, "Subsciptionwas not notefied on failure");
     }
 }
Ejemplo n.º 15
0
 public void HandlerWaitStopsAndMessageOfUnknownTypeReturnsToQueueOnUnsubscribeTest()
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         var         received             = new AutoResetEvent(false);
         IDisposable subscription         = processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
         {
             received.Set();
             Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
         }, "type2");
         processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
         }, 0);
         Assert.That(received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing");
         subscription.Dispose();
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => received.Set(), "type1");
         Assert.That(received.WaitOne(500), Is.True, "Message was not returned to queue");
     }
 }
Ejemplo n.º 16
0
        private static void MainMenu()
        {
            //------------------------------------------Enumerator Check
            //var enumerator = sqlORM.GetEnumerator();
            //enumerator.MoveNext();
            //var current = enumerator.Current;
            //------------------------------------------

            //------------------------------------------MongoDB Check
            //var connectopn = ConfigurationManager.ConnectionStrings["MongoDBObservationConnection"].ConnectionString;
            //var newItem = new FlashObservation( 11, 23.3, new Coordinates(12, 12), DateTime.Now);

            //var mongoStorage = new MongoDBStorage<IEnergyObservation>(connectopn, "observation", "energyObservations");
            //mongoStorage.Add(newItem);

            //var analizer = new MongoDBAnalizer(mongoStorage.ObservationDBCollection);
            ////mongoStorage.Clear();
            ////mongoStorage.Remove(newItem);
            //Console.WriteLine(analizer.GetMinEnergy(new Coordinates(12, 12)));
            //var countItems = mongoStorage.Count;

            //-------------------------------------------------------

            //--------------------------------------API Plugin Check
            //var httpClient = new HttpClientService();
            //var apiStorage = new APIStorage<IEnergyObservation>(httpClient);
            //var analizer = new APIAnalizer(httpClient);
            //var newItem = new FlashObservation(5989, 11, 23.3, new Coordinates(5976, 12, 12), DateTime.Now);
            ////apiStorage.Remove(newItem);
            //IEnergyObservation[] array = new IEnergyObservation[45];
            //array[0] = newItem;
            //apiStorage.CopyTo(array, 1);

            //var result = analizer.GetAverageEnergy(DateTime.Now.AddDays(-2), DateTime.Now);
            //var result = analizer.GetAverageEnergy(new Coordinates(5979, 45, 19), new Coordinates(5980, 14, 14));
            //var result2 = analizer.GetDistributionByEnergyValue();
            //var result3 = analizer.GetDistributionByObservationTime();
            //var result4 = analizer.GetDistributionByCoordinates();
            //var result5 = analizer.GetMinEnergyPosition();
            //------------------------------------------------------

            LoadPlugin();
            ShowMainMenu();

            var key = Console.ReadKey();

            while (key.Key != ConsoleKey.D0)
            {
                if (key.Key == ConsoleKey.D1)
                {
                    Console.Clear();
                    ShowProcessorMenu();
                    do
                    {
                        var processorKey = Console.ReadKey();
                        if (processorKey.Key == ConsoleKey.D0)
                        {
                            break;
                        }
                        switch (processorKey.Key) //Processors not from plugins
                        {
                        case ConsoleKey.D1:
                            ConsoleProcessingMenu();
                            break;

                        case ConsoleKey.D2:
                            SaveToFileProcessingMenu();
                            break;
                        }
                        if (char.IsNumber(processorKey.KeyChar)) //Processors from plugins
                        {
                            Console.Clear();
                            StartFinishMenu();
                            var consoleProcessingKey = Console.ReadKey();

                            int processorIndexInMenu    = int.Parse(processorKey.KeyChar.ToString());
                            var plaginProcessingFactory = _app.ProcessingFactories.ElementAt(processorIndexInMenu + 1 - startPluginProcessorsIndexInMenu);
                            var plaginSourceFactory     = _app.SourceFactories.Where(sourceFactory => sourceFactory.GetType().ToString().StartsWith(selectedPluginDllName)).First();

                            _testRegistration       = _app.CreateAndRegisterSource(plaginSourceFactory);
                            attachedProcessingGroup = _testRegistration.AttachProcessingGroup(plaginProcessingFactory);

                            _testRegistration.Start().Wait();

                            ShowAnalizerResult(attachedProcessingGroup.Analizer);
                        }
                    } while (true);
                }
            }
        }
Ejemplo n.º 17
0
        private static void ProcessFactoriesInApp()
        {
            Console.WriteLine("Choose source factory to work with.");
            int i = 0;

            foreach (var appSourceFactory in App.SourceFactories)
            {
                ++i;
                Console.WriteLine($"{i}: {appSourceFactory.GetType()}");
            }

            var userChoiceIsValid = false;
            ISourceFactory <FlashObservation> sourceFactory = null;

            if (App.SourceFactories.Count == 0)
            {
                sourceFactory = new RandomEnergySourceFactory(new Logger());
                Console.WriteLine($"The source factory is not defined in plugin, so it will be {nameof(RandomEnergySource)}");
            }
            else
            {
                while (!userChoiceIsValid)
                {
                    if (TryReadUserInput(0, App.SourceFactories.Count, out int userChoice))
                    {
                        userChoiceIsValid = true;
                        sourceFactory     = App.SourceFactories.ElementAt(userChoice - 1);
                    }
                }
            }

            Console.WriteLine("Choose processing factory to work with.");
            i = 0;
            foreach (var appProcessingFactory in App.ProcessingFactories)
            {
                ++i;
                Console.WriteLine($"{i}: {appProcessingFactory.GetType()}");
            }

            IProcessingFactory <FlashObservation> processingFactory = null;

            userChoiceIsValid = false;
            while (!userChoiceIsValid !)
            {
                if (TryReadUserInput(0, App.ProcessingFactories.Count, out int userChoice))
                {
                    userChoiceIsValid = true;
                    processingFactory = App.ProcessingFactories.ElementAt(userChoice - 1);
                }
            }

            Console.WriteLine($"Processing source factory {sourceFactory.GetType()}...");
            ISourceRegistration <FlashObservation> sourceRegistration = App.CreateAndRegisterSource(sourceFactory);
            IProcessingGroup <FlashObservation>    processingGroup    = sourceRegistration.AttachProcessingGroup(processingFactory);

            SourceRegistrations.Add(sourceRegistration);
            Console.WriteLine("Click Ctrl+C to stop generating values");
            sourceRegistration.Start().Wait();

            ProcessAnalyzer(processingGroup.Analizer);
        }
Ejemplo n.º 18
0
 public void SetProcessingGroup(IProcessingGroup processingGroup)
 {
     ProcessingGroup = processingGroup;
 }
Ejemplo n.º 19
0
 internal void RemoveProcessingGroup(IProcessingGroup processingGroup)
 {
     _processingGroups.Remove(processingGroup);
 }
Ejemplo n.º 20
0
 internal void RemoveProcessingGroup(IProcessingGroup <IEnergyObservation> processingGroup)
 {
     _processingGroups.Remove(processingGroup);
 }