-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppCommandsTests.kt
216 lines (204 loc) · 6.98 KB
/
AppCommandsTests.kt
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package pt.isel.pc.problemsets.set3
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import pt.isel.pc.problemsets.set3.base.App
import pt.isel.pc.problemsets.set3.base.Messages
import pt.isel.pc.problemsets.utils.MultiThreadTestHelper
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.util.concurrent.CountDownLatch
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.time.Duration.Companion.seconds
class AppCommandsTests {
@Test
fun `shutdown command should shutdown the server gracefully within timeout`() {
val seconds = 5
val output = redirectInOutStream("/shutdown $seconds") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000) // time to receive the response
}
}
assertEquals(listOf(Messages.SERVER_IS_BOUND), output)
}
@Test
fun `shutdown command with invalid timeout should return an error message`() {
val output = redirectInOutStream("/shutdown -1") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000) // time to receive the response
App.shutdown() // without explicit shutdown, the server will not stop
}
}
assertEquals(
listOf(
Messages.SERVER_IS_BOUND,
Messages.unknownAppCommand(Messages.SHUTDOWN_COMMAND_INVALID_TIMEOUT)
),
output
)
}
@Test
fun `shutdown command without timeout should return an error message`() {
val output = redirectInOutStream("/shutdown") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000) // time to receive the response
App.shutdown() // without explicit shutdown, the server will not stop
}
}
assertEquals(
listOf(
Messages.SERVER_IS_BOUND,
Messages.unknownAppCommand(Messages.SHUTDOWN_COMMAND_TIMEOUT_NOT_SPECIFIED)
),
output
)
}
@Test
fun `exit command should shutdown the server abruptly`() {
val output = redirectInOutStream("/exit") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000)
}
}
assertEquals(listOf(Messages.SERVER_IS_BOUND), output)
}
@Test
fun `exit command does not receive any argument`() {
val output = redirectInOutStream("/exit 1") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000)
}
}
assertEquals(
listOf(
Messages.SERVER_IS_BOUND,
Messages.unknownAppCommand(Messages.EXIT_COMMAND_DOES_NOT_HAVE_ARGUMENTS)
),
output
)
}
@Test
fun `command to see available app commands should print all available commands`() {
val output = redirectInOutStream("/commands") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000) // time to receive the response
App.shutdown() // without explicit shutdown, the server will not stop
}
}
assertEquals(listOf(Messages.SERVER_IS_BOUND, Messages.APP_COMMANDS), output)
}
@Test
fun `command to see available app commands does not receive any argument`() {
val output = redirectInOutStream("/commands gibberish") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000) // time to receive the response
App.shutdown() // without explicit shutdown, the server will not stop
}
}
assertEquals(
listOf(
Messages.SERVER_IS_BOUND,
Messages.unknownAppCommand(Messages.AVAILABLE_COMMANDS_COMMAND_DOES_NOT_HAVE_ARGUMENTS)
),
output
)
}
@Test
fun `wrong command should return an response from the server`() {
val output = redirectInOutStream("any command available in the server?") {
runBlocking {
launch {
App.launch(this)
}
App.awaitListening()
delay(1000) // time to receive the response
App.shutdown() // without explicit shutdown, the server will not stop
}
}
assertEquals(
listOf(
Messages.SERVER_IS_BOUND,
Messages.unknownAppCommand(Messages.SEE_APP_COMMANDS)
),
output
)
}
@Test
fun `App can only be launched once`() {
val latch = CountDownLatch(1)
val testHelper = MultiThreadTestHelper(10.seconds)
assertFailsWith<IllegalStateException> {
testHelper.createAndStartThread {
runBlocking {
latch.await()
App.launch(this)
}
}
testHelper.createAndStartThread {
runBlocking {
latch.await()
App.launch(this)
}
}
Thread.sleep(2000)
latch.countDown()
testHelper.join()
}
}
companion object {
/**
* Executes the [test] function with the input and output redirected.
* @param lines Lines to write in the input.
* @param test Code to test that reads from stdin and writes to stdout.
* @return Lines written in the output.
*/
fun redirectInOutStream(vararg lines: String, test: () -> Unit): List<String> {
val oldInput = System.`in`
val oldOutput = System.out
return runCatching {
System.setIn(ByteArrayInputStream(lines.joinToString(System.lineSeparator()).toByteArray()))
val result = ByteArrayOutputStream()
// Set new output
System.setOut(PrintStream(result))
test()
val out = result.toString().split(System.lineSeparator())
return if (out.size > 1 && out.last().isEmpty()) out.dropLast(1) else out
}.also {
// Restore old input and output
System.setIn(oldInput)
System.setOut(oldOutput)
}.getOrNull() ?: emptyList()
}
}
}