From dec51efccdeda48545753d8a6763338a9245ddeb Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Mon, 12 Jan 2015 12:35:20 -0500 Subject: [PATCH] Added Multiline codec closed issue #23 --- .../Properties/AssemblyInfo.cs | 4 +- TimberWinR.UnitTests/Multiline1.txt | 13 + TimberWinR.UnitTests/Multiline2.txt | 19 ++ TimberWinR.UnitTests/MultilineTests.cs | 119 +++++++++ .../TimberWinR.UnitTests.csproj | 9 + TimberWinR/Inputs/InputListener.cs | 2 +- TimberWinR/Inputs/LogsListener.cs | 249 +++++++++++++++++- TimberWinR/Inputs/StdinListener.cs | 115 +++++++- TimberWinR/Manager.cs | 9 +- TimberWinR/Outputs/Redis.cs | 5 +- TimberWinR/Outputs/Stdout.cs | 14 +- TimberWinR/Parser.cs | 63 ++++- TimberWinR/ReleaseNotes.md | 4 + TimberWinR/TimberWinR.csproj | 1 + TimberWinR/mdocs/Codec.md | 31 +++ TimberWinR/mdocs/Logs.md | 1 + TimberWinR/mdocs/StdinInput.md | 1 + 17 files changed, 623 insertions(+), 36 deletions(-) create mode 100644 TimberWinR.UnitTests/Multiline1.txt create mode 100644 TimberWinR.UnitTests/Multiline2.txt create mode 100644 TimberWinR.UnitTests/MultilineTests.cs create mode 100644 TimberWinR/mdocs/Codec.md diff --git a/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs b/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs index eec81fc..ee7828b 100644 --- a/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs +++ b/TimberWinR.ServiceHost/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.18.0")] -[assembly: AssemblyFileVersion("1.3.18.0")] +[assembly: AssemblyVersion("1.3.19.0")] +[assembly: AssemblyFileVersion("1.3.19.0")] diff --git a/TimberWinR.UnitTests/Multiline1.txt b/TimberWinR.UnitTests/Multiline1.txt new file mode 100644 index 0000000..dd38336 --- /dev/null +++ b/TimberWinR.UnitTests/Multiline1.txt @@ -0,0 +1,13 @@ +multiline1 \ +ml1_1 \ +ml1_2 \ +ml1_2 +singleline1 +singleline2 +multiline2 \ +ml2_1 \ +ml2_2 +multiline3 \ +ml3_1 \ +ml3_2 +singleline3 \ No newline at end of file diff --git a/TimberWinR.UnitTests/Multiline2.txt b/TimberWinR.UnitTests/Multiline2.txt new file mode 100644 index 0000000..449f387 --- /dev/null +++ b/TimberWinR.UnitTests/Multiline2.txt @@ -0,0 +1,19 @@ +2015-01-07 13:14:26,572 TEST DEBUG [THREAD : 25] - Sending message to TServer - tcp://10.1111.11.111:1111 +'RequestAttachUserData' ('30') +message attributes: +AttributeConnID [long] = 00890 +AttributeReferenceID [int] = 88 +AttributeThisDN [str] = "2214" +AttributeUserData [bstr] = KVList: + 'ActivityID' [str] = "1-XXXXXX" +2015-01-07 13:14:26,574 TEST DEBUG [THREAD : 25] - Writing message RequestAttachUserData in 'proxy1' via '.StatePrimary proxy: proxy1' +2015-01-07 13:14:26,575 TEST DEBUG [THREAD : 25] - sending RequestAttachUserData to Test.Platform.Commons.Connection.CommonConnection +2015-01-07 13:20:31,665 TEST DEBUG [THREAD : SelectorThread] - Proxy got message 'EventOnHook' ('87') +message attributes: +AttributeEventSequenceNumber [long] = 4899493 +Time = ComplexClass(TimeStamp): + AttributeTimeinuSecs [int] = 573000 + AttributeTimeinSecs [int] = 1420644031 +AttributeThisDN [str] = "2214" +. Processing with state .StatePrimary proxy: proxy1 +2015-01-07 14:14:26,666 TEST DEBUG [THREAD : 25] - sending RequestAttachUserData to Test.Platform.Commons.Connection.CommonConnection diff --git a/TimberWinR.UnitTests/MultilineTests.cs b/TimberWinR.UnitTests/MultilineTests.cs new file mode 100644 index 0000000..229d948 --- /dev/null +++ b/TimberWinR.UnitTests/MultilineTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Newtonsoft.Json; +using NUnit.Framework; +using TimberWinR.Inputs; +using TimberWinR.Parser; +using Newtonsoft.Json.Linq; +using System.Diagnostics; + +namespace TimberWinR.UnitTests +{ + [TestFixture] + public class MultilineTests + { + [Test(Description = "Test using next")] + public void TestMultiline1() + { + using (StreamReader sr = new StreamReader("Multiline1.txt")) + { + List events = new List(); + + Console.SetIn(sr); + + Stdin sin = new Stdin(); + + sin.Codec = new Codec(); + sin.Codec.Pattern = "\\\\$"; + sin.Codec.What = Codec.WhatType.next; + sin.Codec.Type = Codec.CodecType.multiline; + + var cancelTokenSource = new CancellationTokenSource(); + + using (var syncHandle = new ManualResetEventSlim()) + { + try + { + StdinListener sl = new StdinListener(sin, cancelTokenSource.Token); + + sl.OnMessageRecieved += o => + { + events.Add(o); + if (events.Count >= 6) + cancelTokenSource.Cancel(); + }; + + if (!cancelTokenSource.Token.IsCancellationRequested) + syncHandle.Wait(TimeSpan.FromSeconds(10000), cancelTokenSource.Token); + } + catch (OperationCanceledException oex) + { + } + } + + Assert.AreEqual(events.Count, 6); + Assert.AreEqual(events[0]["message"].ToString(), "multiline1 \\\nml1_1 \\\nml1_2 \\\nml1_2 "); + Assert.AreEqual(events[1]["message"].ToString(), "singleline1"); + Assert.AreEqual(events[2]["message"].ToString(), "singleline2"); + Assert.AreEqual(events[3]["message"].ToString(), "multiline2 \\\nml2_1 \\\nml2_2"); + Assert.AreEqual(events[4]["message"].ToString(), "multiline3 \\\nml3_1 \\\nml3_2"); + Assert.AreEqual(events[5]["message"].ToString(), "singleline3"); + } + } + + [Test(Description = "Test using previous")] + public void TestMultiline2() + { + using (StreamReader sr = new StreamReader("Multiline2.txt")) + { + List events = new List(); + + Console.SetIn(sr); + + Stdin sin = new Stdin(); + + sin.Codec = new Codec(); + sin.Codec.Pattern = "^(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2},\\d{3})(.*)$"; + sin.Codec.What = Codec.WhatType.previous; + sin.Codec.Type = Codec.CodecType.multiline; + sin.Codec.Negate = true; + + var cancelTokenSource = new CancellationTokenSource(); + + using (var syncHandle = new ManualResetEventSlim()) + { + try + { + StdinListener sl = new StdinListener(sin, cancelTokenSource.Token); + + sl.OnMessageRecieved += o => + { + events.Add(o); + if (events.Count >= 4) + cancelTokenSource.Cancel(); + }; + + if (!cancelTokenSource.Token.IsCancellationRequested) + syncHandle.Wait(TimeSpan.FromSeconds(10000), cancelTokenSource.Token); + } + catch (OperationCanceledException oex) + { + } + } + + Assert.AreEqual(events.Count, 4); + Assert.AreEqual(events[0]["message"].ToString(), "2015-01-07 13:14:26,572 TEST DEBUG [THREAD : 25] - Sending message to TServer - tcp://10.1111.11.111:1111\n'RequestAttachUserData' ('30')\nmessage attributes:\nAttributeConnID [long] = 00890\nAttributeReferenceID [int] = 88\nAttributeThisDN [str] = \"2214\"\nAttributeUserData [bstr] = KVList: \n\t\t'ActivityID' [str] = \"1-XXXXXX\""); + Assert.AreEqual(events[1]["message"].ToString(), "2015-01-07 13:14:26,574 TEST DEBUG [THREAD : 25] - Writing message RequestAttachUserData in 'proxy1' via '.StatePrimary proxy: proxy1'"); + Assert.AreEqual(events[2]["message"].ToString(), "2015-01-07 13:14:26,575 TEST DEBUG [THREAD : 25] - sending RequestAttachUserData to Test.Platform.Commons.Connection.CommonConnection"); + Assert.AreEqual(events[3]["message"].ToString(), "2015-01-07 13:20:31,665 TEST DEBUG [THREAD : SelectorThread] - Proxy got message 'EventOnHook' ('87')\nmessage attributes:\nAttributeEventSequenceNumber [long] = 4899493\nTime = ComplexClass(TimeStamp):\n\tAttributeTimeinuSecs [int] = 573000\n\tAttributeTimeinSecs [int] = 1420644031\nAttributeThisDN [str] = \"2214\"\n. Processing with state .StatePrimary proxy: proxy1"); + } + } + + } +} diff --git a/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj b/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj index 95cd6ec..09026ad 100644 --- a/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj +++ b/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj @@ -64,6 +64,7 @@ + @@ -82,6 +83,14 @@ Designer + + + PreserveNewest + + + PreserveNewest + +