|
| 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 | +} |
0 commit comments