]> git.saurik.com Git - redis.git/blob - client-libraries/scala/src/test/scala/com/redis/SocketOperationsSpec.scala
4f7bbac4f773c2a7654e4d863a5e9e9497f53fe2
[redis.git] / client-libraries / scala / src / test / scala / com / redis / SocketOperationsSpec.scala
1 import org.specs._
2 import com.redis._
3
4 import java.io._
5 import java.net.Socket
6
7 import org.specs.mock.Mockito
8 import org.mockito.Mock._
9 import org.mockito.Mockito._
10
11 class SocketOperationTest(val host:String, val port: Int) extends SocketOperations
12
13 object SocketOperationsSpec extends Specification with Mockito {
14
15 "Socket Operations" should {
16 var socketOperation: SocketOperationTest = null
17 var socket: Socket = null
18 var in: BufferedReader = null
19
20 doBefore {
21 socketOperation = new SocketOperationTest("localhost", 6379666)
22 socket = mock[Socket]
23 in = mock[BufferedReader]
24 socketOperation.socket = socket
25 socketOperation.in = in
26 }
27
28 def readOkFromInput = { when(in.readLine()).thenReturn(socketOperation.OK) }
29 def readSingleFromInput = { when(in.readLine()).thenReturn(socketOperation.SINGLE) }
30 def readBulkFromInput = { in.readLine() returns("$6\r\nfoobar\r\n") thenReturns("$6\r\nfoobar\r\n") }
31 def readIntFromInput = { when(in.readLine()).thenReturn(socketOperation.INT+"666") }
32
33 "tell if it's connected" in {
34 socketOperation.connected mustEqual true
35 socketOperation.socket = null
36 socketOperation.connected mustEqual false
37 }
38
39 "return false when can't connect" in {
40 socketOperation.connect mustEqual false
41 }
42
43 "return current data input stream" in {
44 socketOperation.getInputStream mustEqual in
45 }
46
47 "read a line from socket" in {
48 readOkFromInput
49 socketOperation.in mustEqual in
50 socketOperation.readline mustEqual socketOperation.OK
51 }
52
53 "read type response" in {
54 readOkFromInput
55 socketOperation.readtype mustEqual ("+", socketOperation.OK)
56 }
57
58 "when reading responses" in {
59
60 "read OK" in {
61 readOkFromInput
62 socketOperation.readResponse mustEqual socketOperation.OK
63 }
64
65 "read single line" in {
66 readSingleFromInput
67 socketOperation.readResponse mustEqual socketOperation.SINGLE
68 }
69
70 "reconnect on error" in {
71 socketOperation.readResponse mustEqual false
72 socket.close was called
73 socketOperation.connected mustEqual true
74 }
75
76 "read in bulk" in {
77 // readBulkFromInput
78 // this shouldn't be the response, it doesn't seem to work return and then returns.
79 // Here's what should happen: '$6\r\n' on first readLine and then 'foobar\r\n'
80 readBulkFromInput
81 socketOperation.readtype mustEqual ("$", "$6\r\nfoobar\r\n")
82 socketOperation.readResponse mustEqual "$6\r\nfoobar\r\n"
83 socketOperation.bulkReply("$6\r\nfoobar\r\n") was called
84 }
85
86 "read integer" in {
87 readIntFromInput
88 socketOperation.readInt mustEqual 666
89 }
90
91 "read a boolean return value" in {
92 readOkFromInput
93 socketOperation.readBoolean mustEqual true
94 }
95 }
96 }
97 }