Browse Source

Add test for label generator

tags/v0.11.0
Chris Smith 5 years ago
parent
commit
25e129ebb1

+ 4
- 2
src/main/kotlin/com/dmdirc/ktirc/util/Labels.kt View File

4
 import com.dmdirc.ktirc.sasl.toBase64
4
 import com.dmdirc.ktirc.sasl.toBase64
5
 import java.time.ZoneOffset
5
 import java.time.ZoneOffset
6
 
6
 
7
-internal var generateLabel = { ircClient: IrcClient ->
7
+internal var generateLabel: (IrcClient) -> String = ::defaultGenerateLabel
8
+
9
+internal fun defaultGenerateLabel(ircClient: IrcClient): String {
8
     val time = currentTimeProvider().toEpochSecond(ZoneOffset.UTC)
10
     val time = currentTimeProvider().toEpochSecond(ZoneOffset.UTC)
9
     val counter = ircClient.serverState.labelCounter.incrementAndGet()
11
     val counter = ircClient.serverState.labelCounter.incrementAndGet()
10
-    ByteArray(6) {
12
+    return ByteArray(6) {
11
         when {
13
         when {
12
             it < 3 -> (time shr it and 0xff).toByte()
14
             it < 3 -> (time shr it and 0xff).toByte()
13
             else -> (counter shr (it - 3) and 0xff).toByte()
15
             else -> (counter shr (it - 3) and 0xff).toByte()

+ 3
- 3
src/test/kotlin/com/dmdirc/ktirc/IrcClientImplTest.kt View File

289
     }
289
     }
290
 
290
 
291
     @Test
291
     @Test
292
-    fun `sends text to socket without label if cap is missing`() = runBlocking {
292
+    fun `asynchronously sends text to socket without label if cap is missing`() = runBlocking {
293
         val client = IrcClientImpl(normalConfig)
293
         val client = IrcClientImpl(normalConfig)
294
         client.socketFactory = mockSocketFactory
294
         client.socketFactory = mockSocketFactory
295
         client.connect()
295
         client.connect()
300
     }
300
     }
301
 
301
 
302
     @Test
302
     @Test
303
-    fun `sends text to socket with added tags and label`() = runBlocking {
303
+    fun `asynchronously sends text to socket with added tags and label`() = runBlocking {
304
         generateLabel = { "abc123" }
304
         generateLabel = { "abc123" }
305
         val client = IrcClientImpl(normalConfig)
305
         val client = IrcClientImpl(normalConfig)
306
         client.socketFactory = mockSocketFactory
306
         client.socketFactory = mockSocketFactory
313
     }
313
     }
314
 
314
 
315
     @Test
315
     @Test
316
-    fun `sends tagged text to socket with label`() = runBlocking {
316
+    fun `asynchronously sends tagged text to socket with label`() = runBlocking {
317
         generateLabel = { "abc123" }
317
         generateLabel = { "abc123" }
318
         val client = IrcClientImpl(normalConfig)
318
         val client = IrcClientImpl(normalConfig)
319
         client.socketFactory = mockSocketFactory
319
         client.socketFactory = mockSocketFactory

+ 45
- 0
src/test/kotlin/com/dmdirc/ktirc/util/LabelsTest.kt View File

1
+package com.dmdirc.ktirc.util
2
+
3
+import com.dmdirc.ktirc.IrcClient
4
+import com.dmdirc.ktirc.TestConstants
5
+import com.dmdirc.ktirc.model.ServerState
6
+import io.mockk.every
7
+import io.mockk.mockk
8
+import org.junit.jupiter.api.Assertions.assertEquals
9
+import org.junit.jupiter.api.Assertions.assertNotEquals
10
+import org.junit.jupiter.api.Test
11
+
12
+internal class LabelsTest {
13
+
14
+    private val fakeServerState = ServerState("", "")
15
+    private val ircClient = mockk<IrcClient> {
16
+        every { serverState } returns fakeServerState
17
+    }
18
+
19
+    @Test
20
+    fun `increments the label id when generating labels`() {
21
+        assertEquals(0L, fakeServerState.labelCounter.get())
22
+        defaultGenerateLabel(ircClient)
23
+        assertEquals(1L, fakeServerState.labelCounter.get())
24
+        defaultGenerateLabel(ircClient)
25
+        assertEquals(2L, fakeServerState.labelCounter.get())
26
+    }
27
+
28
+    @Test
29
+    fun `generates unique labels in same instant`() {
30
+        currentTimeProvider = { TestConstants.time }
31
+        assertNotEquals(defaultGenerateLabel(ircClient), defaultGenerateLabel(ircClient))
32
+        assertNotEquals(defaultGenerateLabel(ircClient), defaultGenerateLabel(ircClient))
33
+    }
34
+
35
+    @Test
36
+    fun `generates unique labels at different times with the same counter value`() {
37
+        currentTimeProvider = { TestConstants.time }
38
+        val label1 = defaultGenerateLabel(ircClient)
39
+        fakeServerState.labelCounter.set(0L)
40
+        currentTimeProvider = { TestConstants.otherTime }
41
+        val label2 = defaultGenerateLabel(ircClient)
42
+        assertNotEquals(label1, label2)
43
+    }
44
+
45
+}

Loading…
Cancel
Save