From 591302975a7f759f117d611c6833bcc47dbfd34a Mon Sep 17 00:00:00 2001 From: Taras Roshko Date: Wed, 14 Nov 2012 16:02:03 +0200 Subject: [PATCH] added writejson processor to testclient (write event over tcp and set isjson flag to true) --- .../EventStore.TestClient/Client.cs | 1 + .../Commands/ReadHttpProcessor.cs | 5 +- .../Commands/WriteJsonProcessor.cs | 149 ++++++++++++++++++ .../EventStore.TestClient.csproj | 1 + 4 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 src/EventStore/EventStore.TestClient/Commands/WriteJsonProcessor.cs diff --git a/src/EventStore/EventStore.TestClient/Client.cs b/src/EventStore/EventStore.TestClient/Client.cs index 72a29f06fc7..a88fb759b8a 100644 --- a/src/EventStore/EventStore.TestClient/Client.cs +++ b/src/EventStore/EventStore.TestClient/Client.cs @@ -85,6 +85,7 @@ private void RegisterProcessors() _commands.Register(new CreateStreamProcessor()); _commands.Register(new WriteProcessor()); + _commands.Register(new WriteJsonProcessor()); _commands.Register(new WriteFloodProcessor()); _commands.Register(new WriteFloodWaitingProcessor()); diff --git a/src/EventStore/EventStore.TestClient/Commands/ReadHttpProcessor.cs b/src/EventStore/EventStore.TestClient/Commands/ReadHttpProcessor.cs index dbb7211a6a6..b20f8519789 100644 --- a/src/EventStore/EventStore.TestClient/Commands/ReadHttpProcessor.cs +++ b/src/EventStore/EventStore.TestClient/Commands/ReadHttpProcessor.cs @@ -26,7 +26,6 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // using System.Diagnostics; -using EventStore.Transport.Http; using EventStore.Common.Utils; using EventStore.Transport.Http.Client; @@ -56,7 +55,7 @@ public bool Execute(CommandProcessorContext context, string[] args) context.IsAsync(); var client = new HttpAsyncClient(); - var readUrl = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}/event/{1}", eventStreamId, version); + var readUrl = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}/event/{1}?format=json", eventStreamId, version); context.Log.Info("[{0}]: Reading...", context.Client.HttpEndpoint); @@ -65,7 +64,7 @@ public bool Execute(CommandProcessorContext context, string[] args) response => { sw.Stop(); - context.Log.Info("READ events from <{0}>: {1}", eventStreamId, response.Body); + context.Log.Info("READ events from <{0}>: \n{1}", eventStreamId, response.Body); context.Log.Info("Read request took: {0}.", sw.Elapsed); context.Success(); }, diff --git a/src/EventStore/EventStore.TestClient/Commands/WriteJsonProcessor.cs b/src/EventStore/EventStore.TestClient/Commands/WriteJsonProcessor.cs new file mode 100644 index 00000000000..c9718634310 --- /dev/null +++ b/src/EventStore/EventStore.TestClient/Commands/WriteJsonProcessor.cs @@ -0,0 +1,149 @@ +// Copyright (c) 2012, Event Store LLP +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// Neither the name of the Event Store LLP nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +using System; +using System.Diagnostics; +using System.Net.Sockets; +using System.Text; +using EventStore.Core.Data; +using EventStore.Core.Messages; +using EventStore.Core.Services.Transport.Http.Codecs; +using EventStore.Core.Services.Transport.Tcp; + +namespace EventStore.TestClient.Commands +{ + internal class WriteJsonProcessor : ICmdProcessor + { + public string Usage { get { return "WRJ [ []]"; } } + public string Keyword { get { return "WRJ"; } } + + private readonly Random _random = new Random(); + + public bool Execute(CommandProcessorContext context, string[] args) + { + var eventStreamId = "test-stream"; + var expectedVersion = ExpectedVersion.Any; + var data = GenerateTestData(); + string metadata = null; + + if (args.Length > 0) + { + if (args.Length < 3 || args.Length > 4) + return false; + eventStreamId = args[0]; + expectedVersion = args[1].ToUpper() == "ANY" ? ExpectedVersion.Any : int.Parse(args[1]); + data = args[2]; + if (args.Length == 4) + metadata = args[3]; + } + + context.IsAsync(); + var writeDto = new TcpClientMessageDto.WriteEvents( + eventStreamId, + expectedVersion, + new[] + { + new TcpClientMessageDto.ClientEvent(Guid.NewGuid().ToByteArray(), + "JsonDataEvent", + true, + Encoding.UTF8.GetBytes(data), + Encoding.UTF8.GetBytes(metadata ?? string.Empty)) + }, + true); + var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), writeDto.Serialize()); + + var sw = new Stopwatch(); + bool dataReceived = false; + + context.Client.CreateTcpConnection( + context, + connectionEstablished: conn => + { + context.Log.Info("[{0}]: Writing...", conn.EffectiveEndPoint); + sw.Start(); + conn.EnqueueSend(package.AsByteArray()); + }, + handlePackage: (conn, pkg) => + { + if (pkg.Command != TcpCommand.WriteEventsCompleted) + { + context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command)); + return; + } + + dataReceived = true; + sw.Stop(); + + var dto = pkg.Data.Deserialize(); + if ((OperationErrorCode)dto.ErrorCode == OperationErrorCode.Success) + { + context.Log.Info("Successfully written. EventId: {0}.", package.CorrelationId); + PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds); + } + else + { + context.Log.Info("Error while writing: {0} ({1}).", dto.Error, (OperationErrorCode)dto.ErrorCode); + } + + context.Log.Info("Write request took: {0}.", sw.Elapsed); + conn.Close(); + context.Success(); + }, + connectionClosed: (connection, error) => + { + if (dataReceived && error == SocketError.Success) + context.Success(); + else + context.Fail(); + }); + + context.WaitForCompletion(); + return true; + } + + private string GenerateTestData() + { + return Codec.Json.To(new TestData(Guid.NewGuid().ToString(), _random.Next(1, 101))); + } + } + + internal class TestData + { + public string Name { get; set; } + public int Version { get; set; } + + public TestData() + { + } + + public TestData(string name, int version) + { + Name = name; + Version = version; + } + } +} \ No newline at end of file diff --git a/src/EventStore/EventStore.TestClient/EventStore.TestClient.csproj b/src/EventStore/EventStore.TestClient/EventStore.TestClient.csproj index 652009f50c8..b49243d18ce 100644 --- a/src/EventStore/EventStore.TestClient/EventStore.TestClient.csproj +++ b/src/EventStore/EventStore.TestClient/EventStore.TestClient.csproj @@ -91,6 +91,7 @@ +