瀏覽代碼

Server Connecting and Disconnected events

tags/v0.5.0
Chris Smith 5 年之前
父節點
當前提交
14b9190d2f

+ 2
- 0
CHANGELOG 查看文件

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

+ 3
- 0
src/main/kotlin/com/dmdirc/ktirc/IrcClient.kt 查看文件

112
                 // TODO: Proper error handling - what if connect() fails?
112
                 // TODO: Proper error handling - what if connect() fails?
113
                 socket = this
113
                 socket = this
114
 
114
 
115
+                emitEvent(ServerConnecting(currentTimeProvider()))
116
+
115
                 connect()
117
                 connect()
116
 
118
 
117
                 with(Channel<ByteArray>(Channel.UNLIMITED)) {
119
                 with(Channel<ByteArray>(Channel.UNLIMITED)) {
130
                 // TODO: Send correct host
132
                 // TODO: Send correct host
131
                 sendUser(profile.userName, "localhost", server.host, profile.realName)
133
                 sendUser(profile.userName, "localhost", server.host, profile.realName)
132
                 messageHandler.processMessages(this@IrcClientImpl, readLines(scope).map { parser.parse(it) })
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 查看文件

8
 /** Base class for all events. */
8
 /** Base class for all events. */
9
 sealed class IrcEvent(val time: LocalDateTime)
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
 /** Raised when the connection to the server has been established. The server will not be ready for use yet. */
14
 /** Raised when the connection to the server has been established. The server will not be ready for use yet. */
12
 class ServerConnected(time: LocalDateTime) : IrcEvent(time)
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
 /** Raised when the server is ready for use. */
20
 /** Raised when the server is ready for use. */
15
 class ServerReady(time: LocalDateTime) : IrcEvent(time)
21
 class ServerReady(time: LocalDateTime) : IrcEvent(time)
16
 
22
 

+ 2
- 0
src/main/kotlin/com/dmdirc/ktirc/events/ServerStateHandler.kt 查看文件

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

+ 3
- 1
src/main/kotlin/com/dmdirc/ktirc/model/ServerState.kt 查看文件

12
     internal var receivedWelcome = false
12
     internal var receivedWelcome = false
13
 
13
 
14
     /** The current status of the server. */
14
     /** The current status of the server. */
15
-    var status = ServerStatus.Connecting
15
+    var status = ServerStatus.Disconnected
16
         internal set
16
         internal set
17
 
17
 
18
     /**
18
     /**
108
  * Enumeration of the possible states of a server.
108
  * Enumeration of the possible states of a server.
109
  */
109
  */
110
 enum class ServerStatus {
110
 enum class ServerStatus {
111
+    /** The server is not connected. */
112
+    Disconnected,
111
     /** We are attempting to connect to the server. It is not yet ready for use. */
113
     /** We are attempting to connect to the server. It is not yet ready for use. */
112
     Connecting,
114
     Connecting,
113
     /** We are logging in, dealing with capabilities, etc. The server is not yet ready for use. */
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 查看文件

1
 package com.dmdirc.ktirc
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
 import com.dmdirc.ktirc.io.CaseMapping
4
 import com.dmdirc.ktirc.io.CaseMapping
7
 import com.dmdirc.ktirc.io.LineBufferedSocket
5
 import com.dmdirc.ktirc.io.LineBufferedSocket
8
 import com.dmdirc.ktirc.model.Profile
6
 import com.dmdirc.ktirc.model.Profile
76
     }
74
     }
77
 
75
 
78
     @Test
76
     @Test
79
-    fun `IrcClientImpl emits connected event with local time`() = runBlocking {
77
+    fun `IrcClientImpl emits connection events with local time`() = runBlocking {
80
         currentTimeProvider = { TestConstants.time }
78
         currentTimeProvider = { TestConstants.time }
81
         val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
79
         val client = IrcClientImpl(Server(HOST, PORT), Profile(NICK, REAL_NAME, USER_NAME))
82
         client.socketFactory = mockSocketFactory
80
         client.socketFactory = mockSocketFactory
83
         client.onEvent(mockEventHandler)
81
         client.onEvent(mockEventHandler)
84
         client.connect()
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
         assertEquals(TestConstants.time, captor.firstValue.time)
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
     @Test
110
     @Test

+ 13
- 0
src/test/kotlin/com/dmdirc/ktirc/events/ServerStateHandlerTest.kt 查看文件

37
         assertTrue(serverState.receivedWelcome)
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
     @Test
53
     @Test
41
     fun `ServerStateHandler sets state to negotiating on connected`() = runBlocking {
54
     fun `ServerStateHandler sets state to negotiating on connected`() = runBlocking {
42
         handler.processEvent(ircClient, ServerConnected(TestConstants.time))
55
         handler.processEvent(ircClient, ServerConnected(TestConstants.time))

+ 2
- 2
src/test/kotlin/com/dmdirc/ktirc/model/ServerStateTest.kt 查看文件

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

Loading…
取消
儲存