PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Riak.Tests/Class1.cs

https://github.com/code-attic/Symbiote
C# | 226 lines | 185 code | 37 blank | 4 comment | 2 complexity | 8d9c7466fb7ced540183d563e273d33e MD5 | raw file
Possible License(s): Unlicense
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using Machine.Specifications;
  7. using Symbiote.Core;
  8. using Symbiote.Core.Extensions;
  9. using Symbiote.Core.Persistence;
  10. using Symbiote.Core.Serialization;
  11. using Symbiote.Riak.Impl.Data;
  12. using Symbiote.Riak.Impl.ProtoBuf;
  13. using Symbiote.Riak.Impl.ProtoBuf.Request;
  14. using Symbiote.Riak.Impl.ProtoBuf.Response;
  15. using Symbiote.StructureMapAdapter;
  16. using Symbiote.Riak;
  17. namespace Riak.Tests
  18. {
  19. public class with_assimilate
  20. {
  21. //public static string Ip = "10.15.198.214";
  22. //public static string Ip = "10.15.199.62";
  23. //public static string Ip = "10.15.198.71";
  24. public static string Ip = "192.168.1.105";
  25. private Establish context = () =>
  26. {
  27. Assimilate
  28. .Initialize()
  29. .Riak( x => x.AddNode( r => r.Address( Ip ).ForProtocolBufferPort( 8087 ) ) );
  30. };
  31. }
  32. public class with_riak_server
  33. : with_assimilate
  34. {
  35. public static IRiakClient Client { get; set; }
  36. public static IKeyValueStore KeyValues { get; set; }
  37. private Establish context = () =>
  38. {
  39. Client = Assimilate.GetInstanceOf<IRiakClient>();
  40. KeyValues = Assimilate.GetInstanceOf<IKeyValueStore>();
  41. };
  42. }
  43. public class when_writing_and_retrieving_value_with_server
  44. : with_riak_server
  45. {
  46. public static string value;
  47. private Because of = () =>
  48. {
  49. var document = new Document<string>("this is a string", null);
  50. document.ContentType = "text/plain";
  51. Client.Delete<string>("test", "a", 1);
  52. Client.Persist("test", "a", null, document, 1, 1);
  53. value = Client.Get<string>( "test", "a", 1 ).Value;
  54. };
  55. private It should_be_incremented = () => value.ShouldEqual("this is a string");
  56. }
  57. public class when_using_key_value_store
  58. : with_riak_server
  59. {
  60. public static string value;
  61. private Because of = () =>
  62. {
  63. var persisted = KeyValues.Persist( "test", "this is a test" );
  64. KeyValues.Persist("test", "Actually, this is NOT a test...");
  65. value = KeyValues.Get<string>("test");
  66. var deleted = KeyValues.Delete<string>( "test" );
  67. };
  68. private It should_get_message = () => value.ShouldEqual("Actually, this is NOT a test...");
  69. }
  70. public class when_getting_a_bit_silly
  71. : with_riak_server
  72. {
  73. public static readonly string _speedtest = "speedTest{0}";
  74. public static Stopwatch watch { get; set; }
  75. private Because of = () =>
  76. {
  77. watch = Stopwatch.StartNew();
  78. for (int i = 0; i < 1000; i++)
  79. {
  80. KeyValues.Persist( _speedtest.AsFormat(i), i );
  81. }
  82. watch.Stop();
  83. for (int i = 0; i < 1000; i++)
  84. {
  85. KeyValues.Delete<int>(_speedtest.AsFormat(i));
  86. }
  87. };
  88. private It should_persist_1000_in_1_second = () => watch.ElapsedMilliseconds.ShouldBeLessThanOrEqualTo( 1000 );
  89. }
  90. public class when_pinging_riak
  91. : with_riak_server
  92. {
  93. public static bool Success { get; set; }
  94. private Because of = () =>
  95. {
  96. Success = Client.Ping();
  97. };
  98. private It should_ping = () => Success.ShouldBeTrue();
  99. }
  100. public class when_setting_client_id
  101. : with_riak_server
  102. {
  103. public static string Id;
  104. private Because of = () =>
  105. {
  106. Client.SetClientId( "test" );
  107. Id = Client.GetClientId();
  108. };
  109. private It should_retrieve_client_id = () => Id.ShouldEqual( "test" );
  110. }
  111. public class when_sending_get_manually
  112. : with_assimilate
  113. {
  114. public static TcpClient Client { get; set; }
  115. public static byte[] response { get; set; }
  116. private Because of = () =>
  117. {
  118. Client = new TcpClient( Ip, 8087);
  119. var stream = Client.GetStream();
  120. var factory = new ProtoBufCommandFactory();
  121. var command = factory.CreateGet( "test", "a", 1 );
  122. var content = command.ToProtocolBuffer();
  123. var message = BitConverter
  124. .GetBytes( IPAddress.HostToNetworkOrder( 1 + content.Length ) )
  125. .Concat( new [] { Convert.ToByte( 9 ) })
  126. .Concat( content )
  127. .ToArray();
  128. stream.Write(
  129. message,
  130. 0,
  131. 5 + content.Length
  132. );
  133. response = new byte[16 * 1024];
  134. //stream.Read( response, 0, 16 * 1024 );
  135. var serializer = new RiakSerializer();
  136. var result = serializer.GetResult( stream );
  137. var doc = result as GetResult;
  138. };
  139. private It should_get_response_of_two = () => response[4].ShouldEqual( Convert.ToByte( 10 ) );
  140. }
  141. public class when_sending_ping
  142. : with_assimilate
  143. {
  144. public static TcpClient Client { get; set; }
  145. public static byte[] response { get; set; }
  146. private Because of = () =>
  147. {
  148. Client = new TcpClient( Ip, 8081);
  149. var stream = Client.GetStream();
  150. var riakSerializer = new RiakSerializer();
  151. var generated = riakSerializer.GetCommandBytes(new Ping());
  152. stream.Write(
  153. generated,
  154. 0,
  155. generated.Length
  156. );
  157. response = new byte[5];
  158. stream.Read(response, 0, 5);
  159. };
  160. private It should_get_response_of_two = () => response[4].ShouldEqual(Convert.ToByte(2));
  161. }
  162. public class when_creating_ping_command
  163. : with_assimilate
  164. {
  165. public static byte[] Manual;
  166. public static byte[] Generated;
  167. private Because of = () =>
  168. {
  169. Manual = BitConverter.GetBytes( IPAddress.HostToNetworkOrder( 1 ) )
  170. .Concat( new[] { Convert.ToByte( 1 ) } )
  171. .ToArray();
  172. var riakSerializer = new RiakSerializer();
  173. Generated = riakSerializer.GetCommandBytes( new Ping() );
  174. };
  175. private It should_have_equivalent_approaches = () => Manual.ShouldEqual( Generated );
  176. private It should_have_the_same_bytes = () => Manual.SequenceEqual( Generated );
  177. }
  178. public class when_serializing_string
  179. {
  180. public static string result;
  181. private Because of = () =>
  182. {
  183. var original = "test";
  184. var bytes = original.ToProtocolBuffer();
  185. result = bytes.FromProtocolBuffer<string>();
  186. };
  187. private It should_get_string_back = () => result.ShouldEqual( "test" );
  188. }
  189. }