-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShutdownTests.kt
108 lines (97 loc) · 3.96 KB
/
ShutdownTests.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
package pt.isel.pc.problemsets.set3
import kotlinx.coroutines.coroutineScope
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.set3.base.Server
import pt.isel.pc.problemsets.set3.solution.use
import pt.isel.pc.problemsets.utils.TestClient
import pt.isel.pc.problemsets.utils.TestServer
import pt.isel.pc.problemsets.utils.randomTo
import kotlin.test.assertEquals
internal class ShutdownTests {
private val listeningAddress = App.listeningAddress
private val listeningPort = App.listeningPort
@Test
fun `Abruptly shutdown the server`() {
// given: a random set of clients
val nOfClients = 1000 randomTo 2000
val clients = List(nOfClients) {
TestClient(it, listeningAddress, listeningPort)
}
runBlocking {
Server(listeningAddress, listeningPort, nrThreads = 10).use { server ->
// and: a server listening
server.waitUntilListening()
coroutineScope {
launch {
clients.forEach {
// when: a client connects
it.connect()
}
}
}
// when: sending a signal for the server to end abruptly
server.exit()
}
}
}
@Test
fun `Graceful server shutdown`() {
// given: a random set of clients
val nOfClients = 2500 randomTo 5000
val clients = List(nOfClients) {
TestClient(it, listeningAddress, listeningPort)
}
runBlocking {
Server(listeningAddress, listeningPort, nrThreads = 10).use { server ->
// and: a server listening
server.waitUntilListening()
coroutineScope {
launch {
clients.forEach {
// when: a client connects
it.connect()
}
}
}
clients.forEach {
launch {
// then: all clients receive the server-ending message
assertEquals(Messages.SERVER_IS_ENDING, it.receive())
}
}
// when: sending a signal for the server to gracefully
server.shutdown()
}
}
}
// @Test // Requires project build
fun `Graceful server shutdown with external process termination`() {
TestServer.start().use { server ->
// given: a server listening for connections
server.waitFor { it == Messages.SERVER_IS_BOUND }
// and: two connected clients
val client0 = TestClient(0, listeningAddress, listeningPort).apply { connect() }
server.waitFor { it == Messages.SERVER_ACCEPTED_CLIENT }
val client1 = TestClient(1, listeningAddress, listeningPort).apply { connect() }
server.waitFor { it == Messages.SERVER_ACCEPTED_CLIENT }
// when: sending a message while not in a room
client0.send("hello")
// then: receives expected response
assertEquals(Messages.ERR_NOT_IN_A_ROOM, client0.receive())
// when: sending a message to enter a room
client0.send("/enter lounge")
// then: receives expected response
assertEquals(Messages.enteredRoom("lounge"), client0.receive())
// when: sending a signal for the server to end
server.sendSignal()
// then: both clients receive the server-ending message
assertEquals(Messages.SERVER_IS_ENDING, client0.receive())
assertEquals(Messages.SERVER_IS_ENDING, client1.receive())
server.join()
}
}
}