Browse Source

Server Connecting and Disconnected events

tags/v0.5.0
Chris Smith 5 years ago
parent
commit
14b9190d2f

+ 2
- 0
CHANGELOG View File

@@ -3,6 +3,8 @@ vNEXT (in development)
3 3
  * Added MotdFinished event
4 4
  * Added UserAccountChanged event
5 5
  * Improved some documentation
6
+ * Added ServerConnecting and ServerDisconnected events
7
+ * Server status now starts as Disconnected rather than Connecting
6 8
 
7 9
 v0.4.0
8 10
 

+ 3
- 0
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt View File

@@ -112,6 +112,8 @@ class IrcClientImpl(private val server: Server, private val profile: Profile) :
112 112
                 // TODO: Proper error handling - what if connect() fails?
113 113
                 socket = this
114 114
 
115
+                emitEvent(ServerConnecting(currentTimeProvider()))
116
+
115 117
                 connect()
116 118
 
117 119
                 with(Channel<ByteArray>(Channel.UNLIMITED)) {
@@ -130,6 +132,7 @@ class IrcClientImpl(private val server: Server, private val profile: Profile) :
130 132
                 // TODO: Send correct host
131 133
                 sendUser(profile.userName, "localhost", server.host, profile.realName)
132 134
                 messageHandler.processMessages(this@IrcClientImpl, readLines(scope).map { parser.parse(it) })
135
+                emitEvent(ServerDisconnected(currentTimeProvider()))
133 136
             }
134 137
         }
135 138
     }

+ 6
- 0
src/main/kotlin/com/dmdirc/ktirc/events/Events.kt View File

@@ -8,9 +8,15 @@ import java.time.LocalDateTime
8 8
 /** Base class for all events. */
9 9
 sealed class IrcEvent(val time: LocalDateTime)
10 10
 
11
+/** Raised when a connection to the server is being established. */
12
+class ServerConnecting(time: LocalDateTime) : IrcEvent(time)
13
+
11 14
 /** Raised when the connection to the server has been established. The server will not be ready for use yet. */
12 15
 class ServerConnected(time: LocalDateTime) : IrcEvent(time)
13 16
 
17
+/** Raised when the connection to the server has ended. */
18
+class ServerDisconnected(time: LocalDateTime) : IrcEvent(time)
19
+
14 20
 /** Raised when the server is ready for use. */
15 21
 class ServerReady(time: LocalDateTime) : IrcEvent(time)
16 22
 

+ 2
- 0
src/main/kotlin/com/dmdirc/ktirc/events/ServerStateHandler.kt View File

@@ -9,7 +9,9 @@ internal class ServerStateHandler : EventHandler {
9 9
 
10 10
     override fun processEvent(client: IrcClient, event: IrcEvent): List<IrcEvent> {
11 11
         when (event) {
12
+            is ServerConnecting -> client.serverState.status = ServerStatus.Connecting
12 13
             is ServerConnected -> client.serverState.status = ServerStatus.Negotiating
14
+            is ServerDisconnected -> client.serverState.status = ServerStatus.Disconnected
13 15
             is ServerWelcome -> handleWelcome(client.serverState, event.server, event.localNick)
14 16
             is ServerFeaturesUpdated -> client.serverState.features.setAll(event.serverFeatures)
15 17
 

+ 3
- 1
src/main/kotlin/com/dmdirc/ktirc/model/ServerState.kt View File

@@ -12,7 +12,7 @@ class ServerState internal constructor(initialNickname: String, initialServerNam
12 12
     internal var receivedWelcome = false
13 13
 
14 14
     /** The current status of the server. */
15
-    var status = ServerStatus.Connecting
15
+    var status = ServerStatus.Disconnected
16 16
         internal set
17 17
 
18 18
     /**
@@ -108,6 +108,8 @@ internal val serverFeatures: Map<String, ServerFeature<*>> by lazy {
108 108
  * Enumeration of the possible states of a server.
109 109
  */
110 110
 enum class ServerStatus {
111
+    /** The server is not connected. */
112
+    Disconnected,
111 113
     /** We are attempting to connect to the server. It is not yet ready for use. */
112 114
     Connecting,
113 115
     /** We are logging in, dealing with capabilities, etc. The server is not yet ready for use. */

+ 25
- 6
src/test/kotlin/com/dmdirc/ktirc/IrcClientTest.kt View File

@@ -1,8 +1,6 @@
1 1
 package com.dmdirc.ktirc
2 2
 
3
-import com.dmdirc.ktirc.events.IrcEvent
4
-import com.dmdirc.ktirc.events.ServerConnected
5
-import com.dmdirc.ktirc.events.ServerWelcome
3
+import com.dmdirc.ktirc.events.*
6 4
 import com.dmdirc.ktirc.io.CaseMapping
7 5
 import com.dmdirc.ktirc.io.LineBufferedSocket
8 6
 import com.dmdirc.ktirc.model.Profile
@@ -76,16 +74,37 @@ internal class IrcClientImplTest {
76 74
     }
77 75
 
78 76
     @Test
79
-    fun `IrcClientImpl emits connected event with local time`() = runBlocking {
77
+    fun `IrcClientImpl emits connection events with local time`() = runBlocking {
80 78
         currentTimeProvider = { TestConstants.time }
81 79
         val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
82 80
         client.socketFactory = mockSocketFactory
83 81
         client.onEvent(mockEventHandler)
84 82
         client.connect()
85 83
 
86
-        val captor = argumentCaptor<ServerConnected>()
87
-        verify(mockEventHandler, timeout(500)).invoke(captor.capture())
84
+        val captor = argumentCaptor<IrcEvent>()
85
+        verify(mockEventHandler, timeout(500).atLeast(2)).invoke(captor.capture())
86
+
87
+        assertTrue(captor.firstValue is ServerConnecting)
88 88
         assertEquals(TestConstants.time, captor.firstValue.time)
89
+
90
+        assertTrue(captor.secondValue is ServerConnected)
91
+        assertEquals(TestConstants.time, captor.secondValue.time)
92
+    }
93
+
94
+    @Test
95
+    fun `IrcClientImpl emits disconnected event with local time when read channel closed`() = runBlocking {
96
+        currentTimeProvider = { TestConstants.time }
97
+        val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
98
+        client.socketFactory = mockSocketFactory
99
+        client.connect()
100
+        client.blockUntilConnected()
101
+
102
+        client.onEvent(mockEventHandler)
103
+        readLineChannel.close()
104
+
105
+        val captor = argumentCaptor<ServerDisconnected>()
106
+        verify(mockEventHandler, timeout(500)).invoke(captor.capture())
107
+        assertEquals(TestConstants.time, captor.lastValue.time)
89 108
     }
90 109
 
91 110
     @Test

+ 13
- 0
src/test/kotlin/com/dmdirc/ktirc/events/ServerStateHandlerTest.kt View File

@@ -37,6 +37,19 @@ internal class ServerStateHandlerTest {
37 37
         assertTrue(serverState.receivedWelcome)
38 38
     }
39 39
 
40
+    @Test
41
+    fun `ServerStateHandler sets state to connecting on event`() = runBlocking {
42
+        handler.processEvent(ircClient, ServerConnecting(TestConstants.time))
43
+        assertEquals(ServerStatus.Connecting, serverState.status)
44
+    }
45
+
46
+    @Test
47
+    fun `ServerStateHandler sets state to disconnected on event`() = runBlocking {
48
+        serverState.status = ServerStatus.Ready
49
+        handler.processEvent(ircClient, ServerDisconnected(TestConstants.time))
50
+        assertEquals(ServerStatus.Disconnected, serverState.status)
51
+    }
52
+
40 53
     @Test
41 54
     fun `ServerStateHandler sets state to negotiating on connected`() = runBlocking {
42 55
         handler.processEvent(ircClient, ServerConnected(TestConstants.time))

+ 2
- 2
src/test/kotlin/com/dmdirc/ktirc/model/ServerStateTest.kt View File

@@ -18,9 +18,9 @@ internal class ServerStateTest {
18 18
     }
19 19
 
20 20
     @Test
21
-    fun `ServerState should default status to connecting`() {
21
+    fun `ServerState should default status to disconnected`() {
22 22
         val serverState = ServerState("acidBurn", "")
23
-        assertEquals(ServerStatus.Connecting, serverState.status)
23
+        assertEquals(ServerStatus.Disconnected, serverState.status)
24 24
     }
25 25
 
26 26
 }

Loading…
Cancel
Save