Skip to content

Commit

Permalink
Add the ability to remove toggle listeners that you have added (#69)
Browse files Browse the repository at this point in the history
* Fix synchronizing on the right object when adding a new listener

* Add the ability to remove the toggle listeners
  • Loading branch information
StylianosGakis authored Feb 1, 2024
1 parent a8acf75 commit 1b6adb6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/kotlin/io/getunleash/UnleashClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ class UnleashClient(
refreshPolicy.addTogglesUpdatedListener(listener)
}

/**
* Removes a TogglesUpdatedListener from our [io.getunleash.polling.RefreshPolicy]
* @param listener the listener to remove
* @since 0.2
*/
override fun removeTogglesUpdatedListener(listener: TogglesUpdatedListener) {
refreshPolicy.removeTogglesUpdatedListener(listener)
}

/**
* Adds a TogglesErroredListener to our [io.getunleash.polling.RefreshPolicy]
* @param listener the listener to add
Expand All @@ -163,4 +172,13 @@ class UnleashClient(
override fun addTogglesErroredListener(listener: TogglesErroredListener) {
refreshPolicy.addTogglesErroredListener(listener)
}

/**
* Removes a TogglesErroredListener from our [io.getunleash.polling.RefreshPolicy]
* @param listener The listener to remove
* @since 0.2
*/
override fun removeTogglesErroredListener(listener: TogglesErroredListener) {
refreshPolicy.removeTogglesErroredListener(listener)
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/io/getunleash/UnleashClientSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ interface UnleashClientSpec : Closeable {
fun updateContext(context: UnleashContext): CompletableFuture<Void>
fun getContext(): UnleashContext
fun addTogglesUpdatedListener(listener: TogglesUpdatedListener)
fun removeTogglesUpdatedListener(listener: TogglesUpdatedListener)
fun addTogglesErroredListener(listener: TogglesErroredListener)
fun removeTogglesErroredListener(listener: TogglesErroredListener)

fun startPolling()
fun isPolling(): Boolean
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/io/getunleash/polling/RefreshPolicy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,29 @@ abstract class RefreshPolicy(
}

fun addTogglesUpdatedListener(listener: TogglesUpdatedListener): Unit {
synchronized(listener) {
synchronized(listeners) {
listeners.add(listener)
}
}

fun removeTogglesUpdatedListener(listener: TogglesUpdatedListener) {
synchronized(listeners) {
listeners.remove(listener)
}
}

fun addTogglesErroredListener(errorListener: TogglesErroredListener): Unit {
synchronized(errorListeners) {
errorListeners.add(errorListener)
}
}

fun removeTogglesErroredListener(listener: TogglesErroredListener): Unit {
synchronized(errorListeners) {
errorListeners.remove(listener)
}
}

fun addTogglesCheckedListener(checkListener: TogglesCheckedListener) {
synchronized(checkListeners) {
checkListeners.add(checkListener)
Expand Down
34 changes: 34 additions & 0 deletions src/test/kotlin/io/getunleash/polling/RefreshPolicyTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package io.getunleash.polling

import io.getunleash.UnleashConfig
import io.getunleash.UnleashContext
import io.getunleash.data.Toggle
import java9.util.concurrent.CompletableFuture
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.mockito.kotlin.mock
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.LongAdder


class RefreshPolicyTest {
Expand All @@ -11,4 +18,31 @@ class RefreshPolicyTest {
val s = RefreshPolicy.sha256("expected")
assertThat(s).isEqualTo("cea23dd4b87e8b00d19fb9ccaaef93e97353c7353e2070f3baf05aeb3995dff4")
}

@Test
fun `Can add and remove an update listener`() {
val refreshPolicy = object : RefreshPolicy(
unleashFetcher = mock(),
cache = mock(),
logger = mock(),
config = UnleashConfig("", ""),
context = UnleashContext(),
) {
override val isReady: AtomicBoolean = AtomicBoolean(true)

override fun getConfigurationAsync(): CompletableFuture<Map<String, Toggle>> {
error("Not applicable")
}

override fun startPolling() {}
override fun isPolling(): Boolean = false
}
val checks = LongAdder()
val togglesUpdatedListener = TogglesUpdatedListener { checks.add(1) }
refreshPolicy.addTogglesUpdatedListener(togglesUpdatedListener)
refreshPolicy.broadcastTogglesUpdated()
refreshPolicy.removeTogglesUpdatedListener(togglesUpdatedListener)
refreshPolicy.broadcastTogglesUpdated()
assertThat(checks.sum()).isEqualTo(1)
}
}

0 comments on commit 1b6adb6

Please sign in to comment.