Skip to content

Commit f6a8d55

Browse files
committed
working on protocol unit tests
1 parent 661d66e commit f6a8d55

File tree

9 files changed

+155
-41
lines changed

9 files changed

+155
-41
lines changed

Beanstalk.Client.IntegrationTest/BeanstalkClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public void Put_Reserve_Delete() {
3434
var stream = data.AsStream();
3535
var put = client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), stream, data.Length);
3636
var reserve = client.Reserve();
37-
Assert.AreEqual(put.JobId, reserve.JobId);
38-
Assert.IsTrue(client.Delete(reserve.JobId));
37+
Assert.AreEqual(put.JobId, reserve.Id);
38+
Assert.IsTrue(client.Delete(reserve.Id));
3939
using(var reader = new StreamReader(reserve.Data)) {
4040
Assert.AreEqual(data, reader.ReadToEnd());
4141
}

Beanstalk.Client.Test/Beanstalk.Client.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
<Reference Include="System.Xml" />
5858
</ItemGroup>
5959
<ItemGroup>
60-
<Compile Include="BeanstalkClientTests.cs" />
60+
<Compile Include="BeanstalkProtocolTests.cs" />
6161
<Compile Include="ExtensionTests.cs" />
62+
<Compile Include="MockSocket.cs" />
6263
<Compile Include="Properties\AssemblyInfo.cs" />
6364
<Compile Include="RequestTests.cs" />
6465
<Compile Include="TestExtensions.cs" />

Beanstalk.Client.Test/BeanstalkClientTests.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* libBeanstalk.NET
3+
* Copyright (C) 2010 Arne F. Claassen
4+
* geekblog [at] claassen [dot] net
5+
* http://github.com/sdether/libBeanstalk.NET
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
using System;
21+
using NUnit.Framework;
22+
23+
namespace Droog.Beanstalk.Client.Test {
24+
25+
[TestFixture]
26+
public class BeanstalkProtocolTests {
27+
private MockSocket _mockSocket;
28+
private BeanstalkClient _client;
29+
private int _socketCreated = 0;
30+
31+
[SetUp]
32+
public void Setup() {
33+
_socketCreated = 0;
34+
_mockSocket = new MockSocket();
35+
_client = new BeanstalkClient(() => {
36+
_socketCreated++;
37+
_mockSocket.Connected = true;
38+
return _mockSocket;
39+
});
40+
}
41+
42+
[Test]
43+
public void Can_connect_to_and_disconnect_from_server() {
44+
_client.Connect();
45+
Assert.AreEqual(1, _socketCreated);
46+
_client.Close();
47+
Assert.AreEqual(1, _mockSocket.CloseCalled);
48+
}
49+
50+
[Test]
51+
public void Dispose_disconnects_from_server() {
52+
_client.Connect();
53+
Assert.AreEqual(1, _socketCreated);
54+
_client.Dispose();
55+
Assert.AreEqual(1, _mockSocket.CloseCalled);
56+
}
57+
58+
[Test]
59+
public void Can_put_data() {
60+
_mockSocket.Expect("put 123 0 60 3\r\nfoo\r\n", "INSERTED 456\r\n");
61+
var data = "foo".AsStream();
62+
var response = _client.Put(123, TimeSpan.Zero, TimeSpan.FromSeconds(60), data, data.Length);
63+
_mockSocket.Verify();
64+
Assert.AreEqual(456, response.JobId);
65+
}
66+
67+
[Test]
68+
public void Can_set_tube() {
69+
_mockSocket.Expect("use bob\r\n", "USING bob\r\n");
70+
_client.CurrentTube = "bob";
71+
_mockSocket.Verify();
72+
Assert.AreEqual("bob",_client.CurrentTube);
73+
}
74+
75+
[Test]
76+
public void Can_reserve_without_timeout() {
77+
_mockSocket.Expect("reserve\r\n", "RESERVED 123 3\r\nbar\r\n");
78+
var job = _client.Reserve();
79+
_mockSocket.Verify();
80+
Assert.AreEqual(123,job.Id);
81+
Assert.AreEqual("bar",job.Data.AsText());
82+
}
83+
84+
[Test]
85+
public void Can_reserve_with_timeout() {
86+
_mockSocket.Expect("reserve 10\r\n", "RESERVED 123 3\r\nbar\r\n");
87+
var job = _client.Reserve(TimeSpan.FromSeconds(10));
88+
_mockSocket.Verify();
89+
Assert.AreEqual(123, job.Id);
90+
Assert.AreEqual("bar", job.Data.AsText());
91+
}
92+
}
93+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.IO;
3+
using Droog.Beanstalk.Client.Protocol;
4+
using NUnit.Framework;
5+
6+
namespace Droog.Beanstalk.Client.Test {
7+
public class MockSocket : ISocket {
8+
public int CloseCalled;
9+
private readonly MemoryStream _sentData = new MemoryStream();
10+
private MemoryStream _receivedData = new MemoryStream();
11+
private string _sent;
12+
13+
public void Close() {
14+
CloseCalled++;
15+
Connected = false;
16+
}
17+
18+
public bool Connected { get; set; }
19+
20+
public int Send(byte[] buffer, int offset, int size) {
21+
_sentData.Write(buffer, offset, size);
22+
return size;
23+
}
24+
25+
public int Receive(byte[] buffer, int offset, int size) {
26+
return _receivedData.Read(buffer, offset, size);
27+
}
28+
29+
public void Expect(string sent, string received) {
30+
_receivedData = received.AsStream();
31+
_sent = sent;
32+
}
33+
34+
public void Verify() {
35+
Assert.AreEqual(_sent, _sentData.AsText());
36+
}
37+
}
38+
}

Beanstalk.Client.Test/TestExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,12 @@ public static string AsText(this RequestData data) {
3434
return Encoding.ASCII.GetString(buffer, 0, count);
3535
}
3636

37+
public static string AsText(this Stream stream) {
38+
stream.Position = 0;
39+
using(var reader = new StreamReader(stream)) {
40+
return reader.ReadToEnd();
41+
}
42+
}
43+
3744
}
3845
}

Beanstalk.Client/BeanstalkClient.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ public IEnumerable<string> GetTubes() {
247247
}
248248

249249
public void Close() {
250-
if(_isDisposed) {
251-
throw new ObjectDisposedException(GetType().ToString());
252-
}
250+
ThrowIfDisposed();
253251
if(_socket == null || !_socket.Connected) {
254252
return;
255253
}
@@ -290,9 +288,7 @@ private Response Exec(Request request) {
290288
}
291289

292290
private void VerifyConnection() {
293-
if(_isDisposed) {
294-
throw new ObjectDisposedException(GetType().ToString());
295-
}
291+
ThrowIfDisposed();
296292
if(_socket != null && !_socket.Connected) {
297293
_socket.Close();
298294
_socket = null;
@@ -302,6 +298,12 @@ private void VerifyConnection() {
302298
}
303299
}
304300

301+
private void ThrowIfDisposed() {
302+
if(_isDisposed) {
303+
throw new ObjectDisposedException(GetType().ToString());
304+
}
305+
}
306+
305307
public void Dispose() {
306308
Dispose(true);
307309
}

Beanstalk.Client/BeanstalkExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static PutResponse Put(this IBeanstalkClient client, string data, uint pr
5151
public static Job<string> Reserve(this IBeanstalkClient client) {
5252
var job = client.Reserve();
5353
using(var reader = new StreamReader(job.Data)) {
54-
return new Job<string>(job.JobId, reader.ReadToEnd());
54+
return new Job<string>(job.Id, reader.ReadToEnd());
5555
}
5656
}
5757

@@ -61,7 +61,7 @@ public static Job<string> Reserve(this IBeanstalkClient client, TimeSpan timeout
6161
return null;
6262
}
6363
using(var reader = new StreamReader(job.Data)) {
64-
return new Job<string>(job.JobId, reader.ReadToEnd());
64+
return new Job<string>(job.Id, reader.ReadToEnd());
6565
}
6666
}
6767
}

Beanstalk.Client/Job.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222

2323
namespace Droog.Beanstalk.Client {
2424
public class Job {
25-
private readonly uint _jobId;
25+
private readonly uint _id;
2626
private readonly Stream _data;
2727
private readonly long _length;
2828

2929
public Job(uint jobId, Stream data, long length) {
30-
_jobId = jobId;
30+
_id = jobId;
3131
_data = data;
3232
_length = length;
3333
}
3434

35-
public uint JobId { get { return _jobId; } }
35+
public uint Id { get { return _id; } }
3636
public Stream Data { get { return _data; } }
3737
public long DataLength { get { return _length; } }
3838
}

0 commit comments

Comments
 (0)