Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added writejson processor to testclient (write event over tcp and set…
… isjson flag to true)
- Loading branch information
Taras Roshko
committed
Nov 14, 2012
1 parent
206b63d
commit 5913029
Showing
4 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/EventStore/EventStore.TestClient/Commands/WriteJsonProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 [<stream-id> <expected-version> <data> [<metadata>]]"; } } | ||
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<TcpClientMessageDto.WriteEventsCompleted>(); | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters