protected override async Task ProcessAsync(IKronosClient client) { // Act Exception ex = await Record.ExceptionAsync(() => client.CountAsync()); Assert.IsType <KronosException>(ex); }
public async Task Contains_WorksCorrectly() { const int port = 9995; const string key = "key"; bool containsFromClientApi; bool containsFromStorage; IExpiryProvider expiryProvider = new StorageExpiryProvider(); using (IStorage storage = new InMemoryStorage(expiryProvider)) { var processor = new SocketProcessor(); var requestProcessor = new RequestProcessor(storage); using (IListener server = new Listener(IPAddress.Any, port, processor, requestProcessor)) { server.Start(); IKronosClient client = KronosClientFactory.CreateClient(localHost, port); containsFromClientApi = await client.ContainsAsync(key); containsFromStorage = storage.Contains(key); } } Assert.False(containsFromClientApi); Assert.Equal(containsFromClientApi, containsFromStorage); }
public async Task Insert_And_Count_WorksCorrectly() { const int port = 9997; const string key = "key"; byte[] data = Encoding.UTF8.GetBytes("lorem ipsum"); DateTime expiry = DateTime.MaxValue; int countFromClientApi; int countFromStorage; IExpiryProvider expiryProvider = new StorageExpiryProvider(); using (IStorage storage = new InMemoryStorage(expiryProvider)) { var processor = new SocketProcessor(); var requestProcessor = new RequestProcessor(storage); using (IListener server = new Listener(IPAddress.Any, port, processor, requestProcessor)) { server.Start(); IKronosClient client = KronosClientFactory.CreateClient(localHost, port); await client.InsertAsync(key, data, expiry); countFromClientApi = await client.CountAsync(); countFromStorage = storage.Count; } } Assert.Equal(countFromClientApi, 1); Assert.Equal(countFromClientApi, countFromStorage); }
protected override async Task RunInternalAsync(IKronosClient client, byte[] package) { string key = Guid.NewGuid().ToString(); await client.InsertAsync(key, package, DateTime.UtcNow.AddSeconds(2)); byte[] data = await client.GetAsync(key); Debug.Assert(data.SequenceEqual(package)); }
private async Task SendToWarnup(IKronosClient client, int size, int times) { byte[] package = PrepareData(size); for (int i = 0; i < times; i++) { string key = Guid.NewGuid().ToString(); await client.InsertAsync(key, package, DateTime.UtcNow.AddDays(1)); } }
public void CreateClient_FromLocalhost() { const int port = 500; // Act IKronosClient client = KronosClientFactory.FromLocalhost(port); // Assert Assert.NotNull(client); }
private async Task WarnupServer(IKronosClient client) { Console.WriteLine("Warnup"); var watch = Stopwatch.StartNew(); await SendToWarnup(client, size : 10, times : 10000); await SendToWarnup(client, size : Mb(5), times : 10); watch.Stop(); Console.WriteLine($"Warnup finished in {watch.ElapsedMilliseconds}ms"); }
public void CreateClient_FromIpAndPort() { // Arrange const string ip = "8.8.8.8"; const int port = 500; // Act IKronosClient client = KronosClientFactory.CreateClientFromIp(ip, port); // Assert Assert.NotNull(client); }
public void CreateClient_FromDomain() { // Arrange const string localHost = "localhost"; const int port = 500; // Act IKronosClient client = KronosClientFactory.CreateClient(localHost, port); // Assert Assert.NotNull(client); }
public async Task <Results> Run(string configPath, int iterations, int workersCount, int packageSize) { var results = new Results(); var watch = new Stopwatch(); try { IKronosClient client = KronosClientFactory.CreateClient(configPath); await WarnupServer(client); var package = PrepareData(Mb(packageSize)); int iterationsPerWorker = (int)Math.Ceiling(iterations / (double)workersCount); Console.WriteLine($"Starting {workersCount} workers with {iterationsPerWorker} iterations per each one"); Task[] workers = new Task[workersCount]; watch.Start(); for (int w = 0; w < workersCount; w++) { Task workerTask = Task.Run(async() => { IKronosClient ownClient = KronosClientFactory.CreateClient(configPath); for (int i = 0; i < iterationsPerWorker; i++) { try { await RunInternalAsync(ownClient, package); } catch (Exception ex) { results.Exceptions.Add(ex); } } }); workers[w] = workerTask; } await Task.WhenAll(workers); watch.Stop(); results.Time = watch.Elapsed; } catch (Exception ex) { Console.WriteLine(ex.ToString()); watch.Stop(); results.Time = watch.Elapsed; results.Exceptions.Add(ex); } return(results); }
protected override async Task ProcessAsync(IKronosClient client) { // Arrange string key = Guid.NewGuid().ToString(); byte[] data = new byte[1024]; // Act await client.InsertAsync(key, data, DateTime.UtcNow.AddDays(5)); byte[] received = await client.GetAsync(key); // Assert Assert.Equal(data, received); }
private static async Task StartAsync() { string configPath = "KronosConfig.json"; IKronosClient client = KronosClientFactory.FromFile(configPath); var watch = Stopwatch.StartNew(); byte[] package = new byte[1024 * 9]; new Random().NextBytes(package); for (int i = 0; i < 10000; i++) { Debug.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); string key = Guid.NewGuid().ToString(); DateTime expiryDate = DateTime.UtcNow.AddDays(1); Debug.WriteLine("ADD - testing"); await client.InsertAsync(key, package, expiryDate); Debug.WriteLine($"ADD - done (size: {package.Length})"); Debug.WriteLine("COUNT - testing"); int count = await client.CountAsync(); Debug.WriteLine($"COUNT - done (count: {count})"); Debug.WriteLine("CONTAINS - testing"); bool contains = await client.ContainsAsync(key); Debug.WriteLine($"CONTAINS - done (exists: {contains})"); Debug.WriteLine("GET - testing"); byte[] fromServer = await client.GetAsync(key); Debug.WriteLine($"GET - done (size: {fromServer.Length})"); Debug.Assert(fromServer.Length == package.Length); Debug.WriteLine("DELETE - testing"); await client.DeleteAsync(key); bool containsAfterDeletion = await client.ContainsAsync(key); Debug.WriteLine($"DELETE - done (exists after deletion: {containsAfterDeletion})"); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); }
protected override async Task ProcessAsync(IKronosClient client) { // Arrange string key = Guid.NewGuid().ToString(); byte[] data = new byte[1024]; // Act bool added = await client.InsertAsync(key, data, DateTime.UtcNow.AddDays(5)); bool contains = await client.ContainsAsync(key); // Assert Assert.True(added, "Object is not added"); Assert.True(contains, "Object is not added, contains returns false"); }
protected override async Task ProcessAsync(IKronosClient client) { // Arrange string key = Guid.NewGuid().ToString(); byte[] data = new byte[1024]; // Act await client.InsertAsync(key, data, DateTime.UtcNow); await Task.Delay(Settings.CleanupTimeMs); bool contains = await client.ContainsAsync(key); // Assert Assert.False(contains, "Object exists after cleanup"); }
protected override async Task ProcessAsync(IKronosClient client) { // Arrange string key = Guid.NewGuid().ToString(); byte[] data = new byte[1024]; // Act await client.InsertAsync(key, data, DateTime.UtcNow.AddDays(5)); await client.DeleteAsync(key); bool contains = await client.ContainsAsync(key); // Assert Assert.False(contains); }
protected override async Task ProcessAsync(IKronosClient client) { // Arrange string key = Guid.NewGuid().ToString(); byte[] data = new byte[1024]; // Act await client.InsertAsync(key, data, DateTime.UtcNow.AddDays(5)); await client.ClearAsync(); int count = await client.CountAsync(); // Assert Assert.Equal(count, 0); }
private static async Task SendFileFilesAsync() { const string directory = @"path"; string configPath = "KronosConfig.json"; IKronosClient client = KronosClientFactory.CreateClient(configPath); foreach (string file in Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories)) { var time = Stopwatch.StartNew(); var data = File.ReadAllBytes(file); string name = Path.GetFileName(file); Console.WriteLine($"Sending {name}"); await client.InsertAsync(name, data, DateTime.Now.AddMinutes(10)); time.Stop(); Console.WriteLine($"Sending finished. Time: {time.ElapsedMilliseconds}"); } }
protected override async Task ProcessAsync(IKronosClient client) { // Arrange string key = Guid.NewGuid().ToString(); byte[] data = new byte[1024]; TimeSpan expiryTime = TimeSpan.FromSeconds(1); DateTime now = DateTime.UtcNow; // Act bool added = await client.InsertAsync(key, data, now + expiryTime); await Task.Delay(expiryTime); bool addedAgain = await client.InsertAsync(key, data, now + TimeSpan.FromDays(1)); // Assert Assert.True(added, "Object was not added"); Assert.True(addedAgain, "Object is not added even if previous was expired"); }
protected override async Task RunInternalAsync(IKronosClient client, byte[] package) { string key = Guid.NewGuid().ToString(); DateTime expiryDate = DateTime.UtcNow.AddSeconds(5); Debug.WriteLine($"ADD - testing"); await client.InsertAsync(key, package, expiryDate); Debug.WriteLine($" ADD - done (size: {package.Length})"); Debug.WriteLine($" COUNT - testing"); int count = await client.CountAsync(); Debug.WriteLine($" COUNT - done (count: {count})"); Debug.WriteLine($" CONTAINS - testing"); bool contains = await client.ContainsAsync(key); Debug.WriteLine($"CONTAINS - done (exists: {contains})"); Debug.WriteLine($" GET - testing"); byte[] fromServer = await client.GetAsync(key); Debug.WriteLine($" GET - done (size: {fromServer.Length})"); if (fromServer.Length != package.Length) { throw new Exception( $"Received message is invalid! Size should be {package.Length}, but wit {fromServer.Length}"); } Debug.WriteLine($" DELETE - testing"); await client.DeleteAsync(key); bool containsAfterDeletion = await client.ContainsAsync(key); Debug.WriteLine($" DELETE - done (exists after deletion: {containsAfterDeletion})"); }
public async Task RunInternalAsync() { await ResetEvent.WaitAsync(); Task server = null; try { const int port = Settings.DefaultPort; LogMessage($"Creating kronos client with port {port}"); IKronosClient client = KronosClientFactory.FromLocalhost(port); LogMessage($"Creating server with port {port}"); server = Task.Factory.StartNew(() => Program.Start(GetSettings(), GetLoggerConfig()), TaskCreationOptions.LongRunning); while (!Program.IsWorking) { LogMessage("Waiting for server warnup..."); await Task.Delay(100); if (server.IsFaulted) { LogMessage($"Server is faulted. Exception: {server.Exception}"); throw server.Exception; } } LogMessage("Processing internal test"); await ProcessAsync(client).ConfigureAwait(true); LogMessage("Processing internal finished"); } catch (Exception ex) { LogMessage($"EXCEPTION: {ex}"); Assert.False(true, ex.Message); } finally { try { LogMessage("Stopping server"); Program.Stop(); LogMessage("Waiting for server task to finish"); if (server != null) { await server.ConfigureAwait(true); } LogMessage("Server stopped"); } catch (AggregateException aex) { foreach (var ex in aex.InnerExceptions) { LogMessage(ex.ToString()); } } catch (Exception ex) { LogMessage($"EXCEPTION: {ex}"); Assert.False(true, ex.Message); } ResetEvent.Release(); } }
protected abstract Task ProcessAsync(IKronosClient client);
protected abstract Task RunInternalAsync(IKronosClient client, byte[] package);