Beispiel #1
0
        public void Test()
        {
            string       message = "This is a test of serialized event data.";
            Exception    myError = new PlatformNotSupportedException("Error.Message", new InsufficientMemoryException());
            LogEventArgs arg1    = null;

            LogEventHandler eh = new LogEventHandler(delegate(object s, LogEventArgs e) { arg1 = e; });

            string[] stack = new string[] { "step 1", "step 2" };
            using (Log.Start(stack[0]))
                using (Log.Start(stack[1]))
                {
                    Log.LogWrite += eh;
                    Log.Error(myError, message);
                    Log.LogWrite -= eh;
                }
            Assert.IsNotNull(arg1);
            Assert.AreEqual(1, arg1.Count);
            Assert.AreEqual(1, arg1.ToArray().Length);

            EventData data = arg1.ToArray()[0];

            Assert.IsNotNull(data);
            BasicLogTest.AssertMessage(GetType(), stack, data, LogLevels.Error, message, myError.GetType());
            Assert.AreEqual(String.Join("::", stack), data.ToString("{LogStack}"));

            BinaryFormatter ser = new BinaryFormatter();
            MemoryStream    ms  = new MemoryStream();

            ser.Serialize(ms, arg1);
            Assert.Greater((int)ms.Position, 0);

            ms.Position = 0;
            object restored = ser.Deserialize(ms);

            Assert.IsNotNull(restored);
            Assert.AreEqual(typeof(LogEventArgs), restored.GetType());
            LogEventArgs arg2 = restored as LogEventArgs;

            Assert.IsNotNull(arg2);
            Assert.AreEqual(1, arg2.Count);
            Assert.AreEqual(1, arg2.ToArray().Length);

            data = arg2.ToArray()[0];
            Assert.IsNotNull(data);

            Assert.IsNotNull(data.Exception);
            Assert.AreNotEqual(myError.GetType(), data.Exception.GetType());
            Assert.AreEqual(typeof(Log).Assembly, data.Exception.GetType().Assembly);
            Assert.AreEqual(myError.Message, data.Exception.Message);
            Assert.AreEqual(myError.StackTrace, data.Exception.StackTrace);
            Assert.AreEqual(myError.Source, data.Exception.Source);
            Assert.AreEqual(myError.ToString(), data.Exception.ToString());

            BasicLogTest.AssertMessage(GetType(), stack, data, LogLevels.Error, message, data.Exception.GetType());
        }
        public void TestVerbose()
        {
            Exception e = new PlatformNotSupportedException();

            Log.Verbose(e);
            AssertMessage(LastMessage, LogLevels.Verbose, e.Message, e.GetType());

            Log.Verbose(e, "a-->{0}", UniqueData);
            AssertMessage(LastMessage, LogLevels.Verbose, String.Format("a-->{0}", UniqueData), e.GetType());

            Log.Verbose("b-->{0}", UniqueData);
            AssertMessage(LastMessage, LogLevels.Verbose, String.Format("b-->{0}", UniqueData), null);

            Log.Config.Level = LogLevels.None;
            LastMessage.ToString();

            Log.Verbose(e);
            Assert.AreEqual(0, _lastMessages.Count);
            Log.Verbose(e, "a-->{0}", UniqueData);
            Assert.AreEqual(0, _lastMessages.Count);
            Log.Verbose("b-->{0}", UniqueData);
            Assert.AreEqual(0, _lastMessages.Count);
        }
Beispiel #3
0
        private static async Task TestService(IProductService client)
        {
            //ExceptionSerializationTest();

            Console.WriteLine("start test");

            var clientInfo = client as IRpcClient;

            if (clientInfo != null)
            {
                Console.WriteLine("formatter is " + clientInfo.Formatter?.GetType());
            }

            var testBatchName = "GetAll";

            try
            {
                var ps1 = client.GetAll();
                Assert.AreNotEqual(ps1, null);
                Assert.Greater(ps1.Length, 0);
                Assert.AreEqual(ps1.Length, 10);

                ps1 = await client.GetAllAsync();

                Assert.AreNotEqual(ps1, null);
                Assert.Greater(ps1.Length, 0);
                Assert.AreEqual(ps1.Length, 10);

                Console.WriteLine(testBatchName + " Test Success");
            }
            catch (Exception ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine(testBatchName + " Test Failed. " + ex);
                Console.ResetColor();
            }

            testBatchName = "Add, GetById";
            try
            {
                var id1 = client.Add(new Product
                {
                    Id   = 1001,
                    Name = "1001-name",
                    Tags = new List <string> {
                        "Tag1", "Tag2"
                    }
                });
                Assert.AreEqual(id1, 1001);

                id1 = await client.AddAsync(new Product
                {
                    Id   = 1002,
                    Name = "1002-name",
                    Tags = new List <string> {
                        "Tag1", "Tag2"
                    }
                });

                Assert.AreEqual(id1, 1002);

                var product1 = client.GetById(1001);
                Assert.AreEqual(product1.Id, 1001);
                Assert.AreEqual(product1.Name, "1001-name");
                Assert.AreEqual(product1.Tags.Count, 2);
                Assert.AreEqual(product1.Tags[0], "Tag1");
                Assert.AreEqual(product1.Tags[1], "Tag2");

                var product2 = await client.GetByIdAsync(1002);

                Assert.AreEqual(product2.Id, 1002);
                Assert.AreEqual(product2.Name, "1002-name");
                Assert.AreEqual(product2.Tags.Count, 2);
                Assert.AreEqual(product2.Tags[0], "Tag1");
                Assert.AreEqual(product2.Tags[1], "Tag2");

                var id2 = client.Add(null);
                Console.WriteLine(testBatchName + " Test Success");
            }
            catch (Exception ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine(testBatchName + " Test Failed. " + ex);
                Console.ResetColor();
            }

            testBatchName = "SetNumber, GetNumber";
            try
            {
                client.SetNumber(98);
                var num = client.GetNumber();
                Assert.AreEqual(num, 98);

                await client.SetNumberAsync(97);

                var num2 = await client.GetNumberAsync();

                Assert.AreEqual(num2, 97);
                Console.WriteLine(testBatchName + " Test Success");
            }
            catch (Exception ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine(testBatchName + " Test Failed. " + ex);
                Console.ResetColor();
            }

            testBatchName = "GetPage";
            try
            {
                var ps1 = client.GetPage(1, 5);
                Assert.AreNotEqual(ps1, null);
                Assert.AreEqual(ps1.Length, 5);

                var ps2 = await client.GetPageAsync(1, 5);

                Assert.AreNotEqual(ps2, null);
                Assert.AreEqual(ps2.Length, 5);
                Console.WriteLine(testBatchName + " Test Success");
            }
            catch (Exception ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine(testBatchName + " Test Failed. " + ex);
                Console.ResetColor();
            }

            testBatchName = "ThrowException";
            try
            {
                client.ExceptionTest();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            testBatchName = "ExceptionTestAsync";
            try
            {
                client.ExceptionTestAsync().Wait();
            }
            catch (AggregateException ex)
            {
                Console.WriteLine(ex.InnerException);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            var exObj = new PlatformNotSupportedException("win31");

            testBatchName = "ThrowException";
            try
            {
                client.ThrowException(exObj);
            }
            catch (Exception ex)
            {
                try
                {
                    if (ex.GetType() != exObj.GetType())
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine($"Warning: Exception Type not equal: {ex.GetType()} <-> {exObj.GetType()}");
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.WriteLine($"Exception Type are equal: {ex.GetType()} <-> {exObj.GetType()}");
                    }
                    Assert.AreEqual(ex.Message, exObj.Message);
                    Console.WriteLine(testBatchName + " Test Success");
                }
                catch (Exception ex2)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine(testBatchName + " Test Failed. " + ex2);
                    Console.ResetColor();
                }
            }

            Console.WriteLine("Test Finished.");
        }