-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSocketClientTest.kt
More file actions
188 lines (164 loc) · 6.55 KB
/
Copy pathSocketClientTest.kt
File metadata and controls
188 lines (164 loc) · 6.55 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package org.naphi
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.After
import org.junit.Test
import org.naphi.client.ConnectionTimeoutException
import org.naphi.client.SocketClient
import org.naphi.client.SocketClientException
import org.naphi.server.Server
import org.naphi.contract.*
import java.net.SocketTimeoutException
import java.time.Duration
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.LongAdder
class SocketClientTest {
private val client = SocketClient(maxConnectionsToDestination = 2, socketTimeout = Duration.ofSeconds(2), connectionRequestTimeout = Duration.ofSeconds(2))
private lateinit var server: Server
@After
fun cleanup() {
client.close()
if (::server.isInitialized) {
server.close()
}
}
@Test
fun `server should echo body and headers`() {
// given
val body = "Echo!"
server = Server(port = 8090, handler = { request ->
Response(
status = Status.OK,
body = request.body,
headers = HttpHeaders("x-custom-header" to request.headers["x-custom-header"].first(), "content-length" to body.length.toString()))
})
// when
val response = client.exchange(
url = "http://localhost:8090",
request = Request(
path = "/",
method = RequestMethod.POST,
headers = HttpHeaders("x-custom-header" to "ABC", "content-length" to body.length.toString()),
body = body))
// then
assertThat(response.status).isEqualTo(Status.OK)
assertThat(response.body).isEqualTo("Echo!")
assertThat(response.headers["x-custom-header"]).containsExactly("ABC")
}
@Test
fun `client should reuse opened connections`() {
// given
server = Server(port = 8090, handler = {
Response(status = Status.OK, headers = HttpHeaders("content-length" to "0"))
})
// when
repeat(times = 3) {
client.exchange(
url = "http://localhost:8090",
request = Request(
path = "/",
method = RequestMethod.POST,
headers = HttpHeaders("content-length" to "0")))
}
// then
assertThat(server.connectionsEstablished()).isEqualTo(1)
assertThat(client.stats().poolStats.connectionsEstablished).isEqualTo(1)
}
@Test
fun `client should open up to 2 connections`() {
// given
server = Server(port = 8090, handler = {
Thread.sleep(1000)
Response(status = Status.OK, headers = HttpHeaders("content-length" to "0"))
})
val successCalls = LongAdder()
// when
val threadPool = Executors.newFixedThreadPool(5)
repeat(times = 3) {
threadPool.submit {
val response = client.exchange(
url = "http://localhost:8090",
request = Request(
path = "/",
method = RequestMethod.POST,
headers = HttpHeaders("content-length" to "0")))
if (response.status == Status.OK) {
successCalls.increment()
}
}
}
threadPool.awaitTermination(4, TimeUnit.SECONDS)
// then
assertThat(successCalls.sum()).isEqualTo(3)
assertThat(server.connectionsEstablished()).isEqualTo(2)
}
@Test
fun `client should close stale connections`() {
// given
server = Server(port = 8090, handler = {
Thread.sleep(1000)
Response(status = Status.OK, headers = HttpHeaders("content-length" to "0"))
})
val client = SocketClient(
keepAliveTimeout = Duration.ofMillis(1000),
checkKeepAliveInterval = Duration.ofMillis(100),
socketTimeout = Duration.ofSeconds(2))
// when
var response = client.exchange(
url = "http://localhost:8090",
request = Request(
path = "/",
method = RequestMethod.POST,
headers = HttpHeaders("content-length" to "0")))
// then
assertThat(response.status).isEqualTo(Status.OK)
// when
Thread.sleep(client.keepAliveTimeout.toMillis() + 2 * client.checkKeepAliveInterval.toMillis())
// when
response = client.exchange(
url = "http://localhost:8090",
request = Request(
path = "/",
method = RequestMethod.POST,
headers = HttpHeaders("content-length" to "0")))
// then
assertThat(response.status).isEqualTo(Status.OK)
assertThat(server.connectionsEstablished()).isEqualTo(2)
}
@Test
fun `should throw exception when connection timed out`() {
// given
/**
* https://en.wikipedia.org/wiki/Reserved_IP_addresses
* requests to this IP should be dropped by router. This IP is reserved for documentation
* and should not be used in practice.
*/
val nonRoutableIp = "192.0.2.0"
// when & then
assertThatThrownBy { client.exchange(
url = "http://$nonRoutableIp:80",
request = Request(
path = "/",
method = RequestMethod.POST,
headers = HttpHeaders("content-length" to "0"))) }
.isInstanceOf(ConnectionTimeoutException::class.java)
}
@Test
fun `should throw exception when server exceeded socket timeout`() {
// given
server = Server(port = 8090, handler = {
Thread.sleep(2100)
Response(status = Status.OK, headers = HttpHeaders("content-length" to "0"))
})
// when & then
assertThatThrownBy { client.exchange(
url = "http://localhost:8090",
request = Request(
path = "/",
method = RequestMethod.POST,
headers = HttpHeaders("content-length" to "0"))) }
.isInstanceOf(SocketClientException::class.java)
.hasCauseInstanceOf(SocketTimeoutException::class.java)
}
}