/
MemcachedSingleNodeTest.scala
102 lines (85 loc) · 3.56 KB
/
MemcachedSingleNodeTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.infinispan.server.memcached
import test.MemcachedTestingUtil
import org.infinispan.test.SingleCacheManagerTest
import org.infinispan.test.fwk.TestCacheManagerFactory
import net.spy.memcached.MemcachedClient
import org.testng.annotations.{Test, AfterClass}
import org.infinispan.manager.EmbeddedCacheManager
import java.net.Socket
import collection.mutable.ListBuffer
import java.io.InputStream
import org.testng.Assert._
/**
* Base class for single node tests.
*
* @author Galder Zamarreño
* @since 4.1
*/
abstract class MemcachedSingleNodeTest extends SingleCacheManagerTest with MemcachedTestingUtil {
private var memcachedClient: MemcachedClient = _
private var memcachedServer: MemcachedServer = _
private val operationTimeout: Int = 60
override def createCacheManager: EmbeddedCacheManager = {
cacheManager = createTestCacheManager
memcachedServer = startMemcachedTextServer(cacheManager)
memcachedClient = createMemcachedClient(60000, server.getPort)
return cacheManager
}
protected def createTestCacheManager: EmbeddedCacheManager = TestCacheManagerFactory.createLocalCacheManager
@AfterClass(alwaysRun = true)
override def destroyAfterClass {
super.destroyAfterClass
log.debug("Test finished, close memcached server", null)
shutdownClient
memcachedServer.stop
}
protected def client: MemcachedClient = memcachedClient
protected def timeout: Int = operationTimeout
protected def server: MemcachedServer = memcachedServer
@Test(enabled = false) // Disable explicitly to avoid TestNG thinking this is a test!!
protected def shutdownClient = memcachedClient.shutdown
protected def send(req: String): String = sendMulti(req, 1).head
protected def sendMulti(req: String, expectedResponses: Int): List[String] = {
val socket = new Socket(server.getHost, server.getPort)
try {
socket.getOutputStream.write(req.getBytes)
val buffer = new ListBuffer[String]
for (i <- 0 until expectedResponses)
buffer += readLine(socket.getInputStream, new StringBuilder)
buffer.toList
}
finally {
socket.close
}
}
protected def readLine(is: InputStream, sb: StringBuilder): String = {
var next = is.read
if (next == 13) { // CR
next = is.read
if (next == 10) { // LF
sb.toString.trim
} else {
sb.append(next.asInstanceOf[Char])
readLine(is, sb)
}
} else if (next == 10) { //LF
sb.toString.trim
} else {
sb.append(next.asInstanceOf[Char])
readLine(is, sb)
}
}
@Test(enabled = false) // Disable explicitly to avoid TestNG thinking this is a test!!
protected def assertClientError(resp: String) = assertExpectedResponse(resp, "CLIENT_ERROR", false)
@Test(enabled = false) // Disable explicitly to avoid TestNG thinking this is a test!!
protected def assertError(resp: String) = assertExpectedResponse(resp, "ERROR", true)
@Test(enabled = false) // Disable explicitly to avoid TestNG thinking this is a test!!
protected def assertStored(resp: String) = assertExpectedResponse(resp, "STORED", true)
@Test(enabled = false) // Disable explicitly to avoid TestNG thinking this is a test!!
protected def assertExpectedResponse(resp: String, expectedResp: String, strictComparison: Boolean) {
if (strictComparison)
assertEquals(resp, expectedResp, "Instead response is: " + resp)
else
assertTrue(resp.contains(expectedResp), "Instead response is: " + resp)
}
}