Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to remove toggle listeners that you have added #69

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading