public void Sequential_clients_reuse_connection() { var pool = ConnectionPool.Create(TestConfig.Host, TestConfig.Port); Assert.AreEqual(0, pool.ActiveConnections); Assert.AreEqual(0, pool.IdleConnections); using(var client = new BeanstalkClient(pool)) { var data = "abc"; var stream = data.AsStream(); var put = client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), stream, data.Length); var reserve = client.Reserve(); Assert.AreEqual(put.JobId, reserve.Id); Assert.IsTrue(client.Delete(reserve.Id)); using(var reader = new StreamReader(reserve.Data)) { Assert.AreEqual(data, reader.ReadToEnd()); } Assert.AreEqual(1, pool.ActiveConnections); Assert.AreEqual(0, pool.IdleConnections); } Assert.AreEqual(0, pool.ActiveConnections); Assert.AreEqual(1, pool.IdleConnections); using(var client = new BeanstalkClient(pool)) { var data = "abc"; var stream = data.AsStream(); var put = client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), stream, data.Length); var reserve = client.Reserve(); Assert.AreEqual(put.JobId, reserve.Id); Assert.IsTrue(client.Delete(reserve.Id)); using(var reader = new StreamReader(reserve.Data)) { Assert.AreEqual(data, reader.ReadToEnd()); } Assert.AreEqual(1, pool.ActiveConnections); Assert.AreEqual(0, pool.IdleConnections); } Assert.AreEqual(0, pool.ActiveConnections); Assert.AreEqual(1, pool.IdleConnections); }
public void Out_of_memory_response_throws() { _mockSocket.Expect("put 123 0 60 3\r\nfoo\r\n", "OUT_OF_MEMORY\r\n"); var data = "foo".AsStream(); try { _client.Put(0, TimeSpan.Zero, TimeSpan.Zero, data, data.Length); Assert.Fail("didn't throw"); } catch (InvalidStatusException e) { Assert.AreEqual(ResponseStatus.OutOfMemory, e.Status); return; } catch (Exception e) { Assert.Fail(string.Format("threw '{0}' instead of InvalidStatusException", e)); } }
public void Sequential_clients_reuse_connection() { var pool = ConnectionPool.Create(TestConfig.Host, TestConfig.Port); Assert.AreEqual(0, pool.ActiveConnections); Assert.AreEqual(0, pool.IdleConnections); using (var client = new BeanstalkClient(pool)) { var data = "abc"; var stream = data.AsStream(); var put = client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), stream, data.Length); var reserve = client.Reserve(); Assert.AreEqual(put.JobId, reserve.Id); Assert.IsTrue(client.Delete(reserve.Id)); using (var reader = new StreamReader(reserve.Data)) { Assert.AreEqual(data, reader.ReadToEnd()); } Assert.AreEqual(1, pool.ActiveConnections); Assert.AreEqual(0, pool.IdleConnections); } Assert.AreEqual(0, pool.ActiveConnections); Assert.AreEqual(1, pool.IdleConnections); using (var client = new BeanstalkClient(pool)) { var data = "abc"; var stream = data.AsStream(); var put = client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), stream, data.Length); var reserve = client.Reserve(); Assert.AreEqual(put.JobId, reserve.Id); Assert.IsTrue(client.Delete(reserve.Id)); using (var reader = new StreamReader(reserve.Data)) { Assert.AreEqual(data, reader.ReadToEnd()); } Assert.AreEqual(1, pool.ActiveConnections); Assert.AreEqual(0, pool.IdleConnections); } Assert.AreEqual(0, pool.ActiveConnections); Assert.AreEqual(1, pool.IdleConnections); }
public void Bulk_put_reserve_delete_with_multiple_connections() { var pool = ConnectionPool.GetPool(TestConfig.Host, TestConfig.Port); var goSignal = new ManualResetEvent(false); var n = 10000; var enqueue = 7; var dequeue = 5; var data = new List<MemoryStream>(); for(var i = 0; i < n; i++) { data.Add(("data-" + i).AsStream()); } var idx = -1; var r = new Random(); var enqueued = 0; ParameterizedThreadStart enqueueWorker = id => { goSignal.WaitOne(); Thread.Sleep((int)id * 100); Console.WriteLine("enqueue worker {0:00} started", id); var client = new BeanstalkClient(pool); while(true) { var i = Interlocked.Increment(ref idx); if(i >= n) { break; } var item = data[i]; Interlocked.Increment(ref enqueued); client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), item, item.Length); } client.Dispose(); Console.WriteLine("enqueue worker {0:00} finished", id); }; var dequeued = 0; ParameterizedThreadStart dequeueWorker = id => { goSignal.WaitOne(); Thread.Sleep(500 + (int)id * 100); Console.WriteLine("dequeue worker {0:00} started", id); var client = new BeanstalkClient(pool); while(true) { try { var job = client.Reserve(TimeSpan.Zero); client.Delete(job.Id); Interlocked.Increment(ref dequeued); } catch(TimedoutException) { break; } } client.Dispose(); Console.WriteLine("dequeue worker {0:00} finished", id); }; for(var i = 0; i < dequeue; i++) { new Thread(dequeueWorker).Start(i); } for(var i = 0; i < enqueue; i++) { new Thread(enqueueWorker).Start(i); } Thread.Sleep(1000); goSignal.Set(); while(dequeued < n) { Console.WriteLine("{0}>{1} - busy: {2}, idle: {3}", dequeued, enqueued, pool.ActiveConnections, pool.IdleConnections); Thread.Sleep(200); } Thread.Sleep(1000); Console.WriteLine("{0}>{1} - busy: {2}, idle: {3}", dequeued, enqueued, pool.ActiveConnections, pool.IdleConnections); }
public void Bulk_put_reserve_delete_with_multiple_connections() { var pool = ConnectionPool.GetPool(TestConfig.Host, TestConfig.Port); var goSignal = new ManualResetEvent(false); var n = 10000; var enqueue = 7; var dequeue = 5; var data = new List <MemoryStream>(); for (var i = 0; i < n; i++) { data.Add(("data-" + i).AsStream()); } var idx = -1; var r = new Random(); var enqueued = 0; ParameterizedThreadStart enqueueWorker = id => { goSignal.WaitOne(); Thread.Sleep((int)id * 100); Console.WriteLine("enqueue worker {0:00} started", id); var client = new BeanstalkClient(pool); while (true) { var i = Interlocked.Increment(ref idx); if (i >= n) { break; } var item = data[i]; Interlocked.Increment(ref enqueued); client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), item, item.Length); } client.Dispose(); Console.WriteLine("enqueue worker {0:00} finished", id); }; var dequeued = 0; ParameterizedThreadStart dequeueWorker = id => { goSignal.WaitOne(); Thread.Sleep(500 + (int)id * 100); Console.WriteLine("dequeue worker {0:00} started", id); var client = new BeanstalkClient(pool); while (true) { try { var job = client.Reserve(TimeSpan.Zero); client.Delete(job.Id); Interlocked.Increment(ref dequeued); } catch (TimedoutException) { break; } } client.Dispose(); Console.WriteLine("dequeue worker {0:00} finished", id); }; for (var i = 0; i < dequeue; i++) { new Thread(dequeueWorker).Start(i); } for (var i = 0; i < enqueue; i++) { new Thread(enqueueWorker).Start(i); } Thread.Sleep(1000); goSignal.Set(); while (dequeued < n) { Console.WriteLine("{0}>{1} - busy: {2}, idle: {3}", dequeued, enqueued, pool.ActiveConnections, pool.IdleConnections); Thread.Sleep(200); } Thread.Sleep(1000); Console.WriteLine("{0}>{1} - busy: {2}, idle: {3}", dequeued, enqueued, pool.ActiveConnections, pool.IdleConnections); }