Beispiel #1
0
        private static void OnMessage(IReceiverLink receiver, Message message)
        {
            AmqpTrace.WriteLine(TraceLevel.Information, "received command from controller");
            int button = (int)message.ApplicationProperties["button"];

            OnAction(button);
        }
Beispiel #2
0
        public async Task WebSocketSendReceiveAsync()
        {
            if (Environment.GetEnvironmentVariable("CoreBroker") == "1")
            {
                // No Websocket listener on .Net Core
                return;
            }

            string testName = "WebSocketSendReceiveAsync";

            // assuming it matches the broker's setup and port is not taken
            Address wsAddress = new Address(address);
            int     nMsgs     = 50;

            ConnectionFactory connectionFactory = new ConnectionFactory(
                new TransportProvider[] { new WebSocketTransportFactory() });
            Connection connection = await connectionFactory.CreateAsync(wsAddress);

            Session    session = new Session(connection);
            SenderLink sender  = new SenderLink(session, "sender-" + testName, "q1");

            for (int i = 0; i < nMsgs; ++i)
            {
                Message message = new Message();
                message.Properties = new Properties()
                {
                    MessageId = "msg" + i, GroupId = testName
                };
                message.ApplicationProperties       = new ApplicationProperties();
                message.ApplicationProperties["sn"] = i;
                await sender.SendAsync(message);
            }

            ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, "q1");

            for (int i = 0; i < nMsgs; ++i)
            {
                Message message = await receiver.ReceiveAsync();

                Trace.WriteLine(TraceLevel.Information, "receive: {0}", message.ApplicationProperties["sn"]);
                receiver.Accept(message);
            }

            await sender.CloseAsync();

            await receiver.CloseAsync();

            await session.CloseAsync();

            await connection.CloseAsync();
        }
Beispiel #3
0
        public static void Main()
        {
            AmqpTrace.TraceLevel    = TraceLevel.Information;
            AmqpTrace.TraceListener = (l, f, a) => Debug.Print(Fx.Format(f, a));

            try
            {
                new EventHubsExample().Run();
            }
            catch (Exception e)
            {
                AmqpTrace.WriteLine(TraceLevel.Error, e.ToString());
            }
        }
        private void Listen()
        {
            // real applicaiton needs to implement error handling and recovery
            Connection   connection = new Connection(new Address(this.address));
            Session      session    = new Session(connection);
            SenderLink   sender     = new SenderLink(session, "send-link", "data");
            ReceiverLink receiver   = new ReceiverLink(session, "receive-link", "control");

            receiver.Start(100, this.OnMessage);

            while (true)
            {
                this.changed.WaitOne();

                Message message = new Message();
                message.ApplicationProperties = new Amqp.Framing.ApplicationProperties();
                message.ApplicationProperties["temperature"] = this.temperature;
                sender.Send(message, null, null);
                AmqpTrace.WriteLine(TraceLevel.Information, "sent data to monitor");
            }
        }
Beispiel #5
0
        public static void Main()
        {
            // setup and connect network
            NetworkHelpers.SetupAndConnectNetwork(true);

            AmqpTrace.TraceLevel    = TraceLevel.Information;
            AmqpTrace.TraceListener = (l, f, a) => Debug.WriteLine(Fx.Format(f, a));
            Connection.DisableServerCertValidation = true;

            // wait for network and valid system date time
            NetworkHelpers.IpAddressAvailable.WaitOne();
            NetworkHelpers.DateTimeAvailable.WaitOne();

            try
            {
                new EventHubsExample().Run();
            }
            catch (Exception e)
            {
                AmqpTrace.WriteLine(TraceLevel.Error, e.ToString());
            }
        }
Beispiel #6
0
        public static int RunTests()
        {
#if DOTNET
            Assembly assembly = typeof(TestRunner).Assembly();
#else
            Assembly assembly = typeof(TestRunner).Assembly;
#endif
            AmqpTrace.WriteLine(TraceLevel.Information, "Running all unit tests in {0}", assembly.FullName);
            Type[] types  = assembly.GetTypes();
            int    passed = 0;
            int    failed = 0;

            AmqpTrace.WriteLine(TraceLevel.Information, "Results\t\tTest");
            AmqpTrace.WriteLine(TraceLevel.Information, "-------\t\t--------");

            foreach (var type in types)
            {
                MethodInfo[] methods        = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                MethodInfo[] testMethods    = new MethodInfo[methods.Length];
                MethodInfo   testInitialize = null;
                MethodInfo   testCleanup    = null;
                int          count          = 0;

                foreach (var method in methods)
                {
                    if (method.Name.Equals("TestInitialize"))
                    {
                        testInitialize = method;
                    }
                    else if (method.Name.Equals("TestCleanup"))
                    {
                        testCleanup = method;
                    }
                    else if (method.Name.Length > 11 && method.Name.Substring(0, 11).Equals("TestMethod_"))
                    {
                        testMethods[count++] = method;
                    }
                }

                if (count > 0)
                {
                    object instance = type.GetConstructor(new Type[0]).Invoke(new object[0]);
                    if (testInitialize != null)
                    {
                        testInitialize.Invoke(instance, null);
                    }

                    for (int i = 0; i < count; i++)
                    {
                        string testName = type.Name + "." + testMethods[i].Name;

                        try
                        {
                            testMethods[i].Invoke(instance, null);
                            ++passed;
                            AmqpTrace.WriteLine(TraceLevel.Information, "Passed\t\t{0}", testName);
                        }
                        catch (Exception exception)
                        {
                            ++failed;
                            AmqpTrace.WriteLine(TraceLevel.Information, "Failed\t\t{0}", testName);
                            AmqpTrace.WriteLine(TraceLevel.Information, exception.ToString());
                        }
                    }

                    if (testCleanup != null)
                    {
                        testCleanup.Invoke(instance, null);
                    }
                }
            }

            AmqpTrace.WriteLine(TraceLevel.Information, "{0}/{1} test(s) Passed, {2} Failed", passed, passed + failed, failed);

            return(failed);
        }
Beispiel #7
0
 static void OnMessage(ReceiverLink receiver, Message message)
 {
     AmqpTrace.WriteLine(TraceLevel.Information, "receive: {0}", message.ApplicationProperties["action"]);
 }
Beispiel #8
0
        public static int RunTests()
        {
            Assembly assembly = GetAssembly();

            AmqpTrace.WriteLine(TraceLevel.Output, "Running all unit tests in {0}", assembly.FullName);
            Type[] types  = assembly.GetTypes();
            int    passed = 0;
            int    failed = 0;

            AmqpTrace.WriteLine(TraceLevel.Output, "Results\t\tTest");
            AmqpTrace.WriteLine(TraceLevel.Output, "-------\t\t--------");

            foreach (var type in types)
            {
                MethodInfo[] methods             = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                MethodInfo[] testMethods         = new MethodInfo[methods.Length];
                MethodInfo   testClassInitialize = null;
                MethodInfo   testClassCleanup    = null;
                MethodInfo   testInitialize      = null;
                MethodInfo   testCleanup         = null;
                int          count = 0;

                foreach (var method in methods)
                {
                    if (method.Name == "ClassInitialize")
                    {
                        testClassInitialize = method;
                    }
                    else if (method.Name == "ClassCleanup")
                    {
                        testClassCleanup = method;
                    }
                    else if (method.Name == "TestInitialize")
                    {
                        testInitialize = method;
                    }
                    else if (method.Name == "TestCleanup")
                    {
                        testCleanup = method;
                    }
                    else if (IsTestMethod(method))
                    {
                        testMethods[count++] = method;
                    }
                }

                if (count > 0)
                {
                    object instance = type.GetConstructor(new Type[0]).Invoke(new object[0]);

                    if (testClassInitialize != null)
                    {
                        try
                        {
                            InvokeMethod(testClassInitialize, instance);
                        }
                        catch (Exception exception)
                        {
                            failed += count;
                            AmqpTrace.WriteLine(TraceLevel.Output, exception.ToString());
                            continue;
                        }
                    }

                    for (int i = 0; i < count; i++)
                    {
                        string testName = type.Name + "." + testMethods[i].Name;

                        try
                        {
                            if (testInitialize != null)
                            {
                                InvokeMethod(testInitialize, instance);
                            }

                            InvokeMethod(testMethods[i], instance);

                            if (testCleanup != null)
                            {
                                InvokeMethod(testCleanup, instance);
                            }

                            passed++;
                            AmqpTrace.WriteLine(TraceLevel.Output, "Passed\t\t{0}", testName);
                        }
                        catch (Exception exception)
                        {
                            failed++;
                            AmqpTrace.WriteLine(TraceLevel.Output, "Failed\t\t{0}", testName);
                            AmqpTrace.WriteLine(TraceLevel.Output, exception.ToString());
                        }
                    }

                    if (testClassCleanup != null)
                    {
                        try
                        {
                            InvokeMethod(testClassCleanup, instance);
                        }
                        catch (Exception exception)
                        {
                            AmqpTrace.WriteLine(TraceLevel.Output, exception.ToString());
                            continue;
                        }
                    }
                }
            }

            AmqpTrace.WriteLine(TraceLevel.Output, "{0}/{1} test(s) Passed, {2} Failed", passed, passed + failed, failed);

            return(failed);
        }