Skip to content

Commit

Permalink
Replaced HeartbeatWaiting with HeartbeatCooldown.
Browse files Browse the repository at this point in the history
Replaced NEXT_HEARTBEAT with TIMES_UP
  • Loading branch information
marcin-cebo committed Jul 26, 2023
1 parent 5e294c0 commit 14d2f48
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sealed class PresenceEvent : Event {
object Reconnect : PresenceEvent()
object Disconnect : PresenceEvent()
object LeftAll : PresenceEvent()
object NextHeartbeat : PresenceEvent()
object TimesUp : PresenceEvent() // End of waiting period between heartbeats. It's time for next heartbeat.

object HeartbeatSuccess : PresenceEvent()
data class HeartbeatFailure(val reason: PubNubException) : PresenceEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sealed class PresenceState : State<PresenceEffectInvocation, PresenceEvent, Pres
transitionTo(Heartbeating(event.channels, event.channelGroups))
}
is PresenceEvent.HeartbeatSuccess -> {
transitionTo(HeartbeatWaiting(channels, channelGroups))
transitionTo(HeartbeatCooldown(channels, channelGroups))
}
is PresenceEvent.HeartbeatFailure -> {
transitionTo(HeartbeatReconnecting(channels, channelGroups, 0, event.reason))
Expand Down Expand Up @@ -77,10 +77,10 @@ sealed class PresenceState : State<PresenceEffectInvocation, PresenceEvent, Pres
is PresenceEvent.StateSet -> {
transitionTo(Heartbeating(event.channels, event.channelGroups))
}
is PresenceEvent.HeartbeatSuccess -> { // toDo shouldn't we introduce HeartbeatReconnectSuccess event?
transitionTo(HeartbeatWaiting(channels, channelGroups))
is PresenceEvent.HeartbeatSuccess -> {
transitionTo(HeartbeatCooldown(channels, channelGroups))
}
is PresenceEvent.HeartbeatFailure -> { // toDo shouldn't we introduce HeartbeatReconnectFailure event?
is PresenceEvent.HeartbeatFailure -> {
transitionTo(HeartbeatReconnecting(channels, channelGroups, attempts + 1, event.reason))
}
is PresenceEvent.HeartbeatGiveup -> {
Expand Down Expand Up @@ -159,7 +159,7 @@ sealed class PresenceState : State<PresenceEffectInvocation, PresenceEvent, Pres
}
}

data class HeartbeatWaiting(
data class HeartbeatCooldown(
val channels: Set<String>,
val channelGroups: Set<String>
) : PresenceState() {
Expand All @@ -177,7 +177,7 @@ sealed class PresenceState : State<PresenceEffectInvocation, PresenceEvent, Pres
is PresenceEvent.StateSet -> {
transitionTo(Heartbeating(event.channels, event.channelGroups))
}
is PresenceEvent.NextHeartbeat -> {
is PresenceEvent.TimesUp -> {
transitionTo(Heartbeating(channels, channelGroups))
}
is PresenceEvent.Disconnect -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import com.pubnub.api.presence.eventengine.state.PresenceState
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class TransitionFromHeartbeatWaitingStateTest {
class TransitionFromHeartbeatCooldownStateTest {
val channels = setOf("Channel01", "Channel02")
val channelGroups = setOf("ChannelGroup01", "ChannelGroup02")
val reason = PubNubException("Test")

@Test
fun `should transit from HEARTBEAT_WAITING to INACTIVE and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and LEAVE invocations when there is LEFT_ALL event`() {
fun `should transit from HEARTBEAT_COOLDOWN to INACTIVE and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and LEAVE invocations when there is LEFT_ALL event`() {
// when
val (newState, invocations) = transition(
PresenceState.HeartbeatWaiting(channels, channelGroups),
PresenceState.HeartbeatCooldown(channels, channelGroups),
PresenceEvent.LeftAll
)

Expand All @@ -33,14 +33,14 @@ class TransitionFromHeartbeatWaitingStateTest {
}

@Test
fun `should transit from HEARTBEAT_WAITING to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT, LEAVE and HEARTBEAT invocations when there is LEFT event`() {
fun `should transit from HEARTBEAT_COOLDOWN to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT, LEAVE and HEARTBEAT invocations when there is LEFT event`() {
// given
val channelToLeave = setOf("Channel01")
val channelGroupToLeave = setOf("ChannelGroup01")

// when
val (newState, invocations) = transition(
PresenceState.HeartbeatWaiting(channels, channelGroups),
PresenceState.HeartbeatCooldown(channels, channelGroups),
PresenceEvent.Left(channelToLeave, channelGroupToLeave)
)

Expand All @@ -57,10 +57,10 @@ class TransitionFromHeartbeatWaitingStateTest {
}

@Test
fun `should transit from HEARTBEAT_WAITING to HEARTBEAT_STOPPED and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and LEAVE invocations when there is DISCONNECT event`() {
fun `should transit from HEARTBEAT_COOLDOWN to HEARTBEAT_STOPPED and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and LEAVE invocations when there is DISCONNECT event`() {
// when
val (newState, invocations) = transition(
PresenceState.HeartbeatWaiting(channels, channelGroups),
PresenceState.HeartbeatCooldown(channels, channelGroups),
PresenceEvent.Disconnect
)

Expand All @@ -76,14 +76,14 @@ class TransitionFromHeartbeatWaitingStateTest {
}

@Test
fun `should transit from HEARTBEAT_WAITING to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and HEARTBEAT invocations when there is JOINED event`() {
fun `should transit from HEARTBEAT_COOLDOWN to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and HEARTBEAT invocations when there is JOINED event`() {
// given
val newChannels = channels + setOf("NewChannel")
val newChannelGroup = channelGroups + setOf("NewChannelGroup")

// when
val (newState, invocations) = transition(
PresenceState.HeartbeatWaiting(channels, channelGroups),
PresenceState.HeartbeatCooldown(channels, channelGroups),
PresenceEvent.Joined(newChannels, newChannelGroup)
)

Expand All @@ -99,14 +99,14 @@ class TransitionFromHeartbeatWaitingStateTest {
}

@Test
fun `should transit from HEARTBEAT_WAITING to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and HEARTBEAT invocations when there is STATE_SET event`() {
fun `should transit from HEARTBEAT_COOLDOWN to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and HEARTBEAT invocations when there is STATE_SET event`() {
// given
val newChannels = channels + setOf("NewChannel")
val newChannelGroup = channelGroups + setOf("NewChannelGroup")

// when
val (newState, invocations) = transition(
PresenceState.HeartbeatWaiting(channels, channelGroups),
PresenceState.HeartbeatCooldown(channels, channelGroups),
PresenceEvent.Joined(newChannels, newChannelGroup)
)

Expand All @@ -122,14 +122,14 @@ class TransitionFromHeartbeatWaitingStateTest {
}

@Test
fun `should transit from HEARTBEAT_WAITING to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and HEARTBEAT invocations when there is NEXT_HEARTBEAT event`() {
fun `should transit from HEARTBEAT_COOLDOWN to HEARTBEATING and create CANCEL_SCHEDULE_NEXT_HEARTBEAT and HEARTBEAT invocations when there is TIMES_UP event`() {
// given
val newChannels = channels + setOf("NewChannel")
val newChannelGroup = channelGroups + setOf("NewChannelGroup")

// when
val (newState, invocations) = transition(
PresenceState.HeartbeatWaiting(channels, channelGroups),
PresenceState.HeartbeatCooldown(channels, channelGroups),
PresenceEvent.Joined(newChannels, newChannelGroup)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class TransitionFromHeartbeatReconnectingStateTest {
}

@Test
fun `should transit from HEARTBEAT_RECONNECTING to HEARTBEAT_WAITING and create CANCEL_DELEYED_HEARTBEAT and SCHEDULE_NEXT_HEARTBEAT invocations when there is HEARTBEAT_SUCCESS event`() {
fun `should transit from HEARTBEAT_RECONNECTING to HEARTBEAT_COOLDOWN and create CANCEL_DELEYED_HEARTBEAT and SCHEDULE_NEXT_HEARTBEAT invocations when there is HEARTBEAT_SUCCESS event`() {
// when
val (newState, invocations) = transition(
PresenceState.HeartbeatReconnecting(
Expand All @@ -160,7 +160,7 @@ class TransitionFromHeartbeatReconnectingStateTest {
)

// then
Assertions.assertEquals(PresenceState.HeartbeatWaiting(channels, channelGroups), newState)
Assertions.assertEquals(PresenceState.HeartbeatCooldown(channels, channelGroups), newState)
Assertions.assertTrue(invocations.any { it is PresenceEffectInvocation.ScheduleNextHeartbeat })
Assertions.assertTrue(invocations.any { it is PresenceEffectInvocation.CancelDelayedHeartbeat })
Assertions.assertEquals(2, invocations.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ class TransitionFromHeartbeatingStateTest {
}

@Test
fun `should transit from HEARTBEATING to HEARTBEAT_WAITING and create SCHEDULE_NEXT_HEARTBEAT invocations when there is HEARTBEAT_SUCCESS event`() {
fun `should transit from HEARTBEATING to HEARTBEAT_COOLDOWN and create SCHEDULE_NEXT_HEARTBEAT invocations when there is HEARTBEAT_SUCCESS event`() {
// when
val (newState, invocations) = transition(
PresenceState.Heartbeating(channels, channelGroups),
PresenceEvent.HeartbeatSuccess
)

// then
Assertions.assertEquals(PresenceState.HeartbeatWaiting(channels, channelGroups), newState)
Assertions.assertEquals(PresenceState.HeartbeatCooldown(channels, channelGroups), newState)
Assertions.assertTrue(invocations.any { it is PresenceEffectInvocation.ScheduleNextHeartbeat })
Assertions.assertEquals(1, invocations.size)
}
Expand Down

0 comments on commit 14d2f48

Please sign in to comment.