@@ -2,12 +2,15 @@ package io.sentry.compose.navigation3.lifecycle
22
33import androidx.lifecycle.Lifecycle
44import com.google.common.truth.Truth.assertThat
5+ import io.sentry.IScope
56import io.sentry.ITransaction
67import io.sentry.Scope
78import io.sentry.SentryOptions
89import io.sentry.SpanStatus
10+ import org.junit.Assert.assertThrows
911import org.junit.Test
1012import org.mockito.kotlin.any
13+ import org.mockito.kotlin.doThrow
1114import org.mockito.kotlin.mock
1215import org.mockito.kotlin.never
1316import org.mockito.kotlin.verify
@@ -765,27 +768,171 @@ class NavLeaseCoordinatorTest {
765768 assertThat(fixture.cleared).isEmpty()
766769 }
767770
771+ @Test
772+ fun `unregistering the leaseholder forgets the owner when finishing its transaction throws` () {
773+ val fixture = Fixture .create()
774+ val navLeaseCoordinator = fixture.navLeaseCoordinator
775+ val failure = IllegalStateException (" finish failed" )
776+ fixture.acquireLease(owner, transaction = transaction)
777+ doThrow(failure).whenever(transaction).finish(SpanStatus .OK )
778+
779+ val thrown =
780+ assertThrows(IllegalStateException ::class .java) {
781+ navLeaseCoordinator.unregisterCandidate(owner, fixture.scope)
782+ }
783+
784+ assertThat(thrown).isSameInstanceAs(failure)
785+ assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
786+ assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isFalse()
787+
788+ fixture.published.clear()
789+ navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
790+ fixture.acquireLease(incoming)
791+
792+ assertThat(fixture.published).containsExactly(incoming)
793+ assertThat(fixture.cleared).isEmpty()
794+ }
795+
796+ @Test
797+ fun `unregistering the leaseholder forgets the owner when unbinding its transaction throws` () {
798+ val fixture = Fixture .create()
799+ val navLeaseCoordinator = fixture.navLeaseCoordinator
800+ val failure = IllegalStateException (" unbind failed" )
801+ val failingScope = mock<IScope >()
802+ fixture.acquireLease(owner, transaction = transaction)
803+ doThrow(failure).whenever(failingScope).withTransaction(any())
804+
805+ val thrown =
806+ assertThrows(IllegalStateException ::class .java) {
807+ navLeaseCoordinator.unregisterCandidate(owner, failingScope)
808+ }
809+
810+ assertThat(thrown).isSameInstanceAs(failure)
811+ assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
812+ assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isFalse()
813+
814+ fixture.published.clear()
815+ navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
816+ fixture.acquireLease(incoming)
817+
818+ assertThat(fixture.published).containsExactly(incoming)
819+ assertThat(fixture.cleared).isEmpty()
820+ }
821+
822+ @Test
823+ fun `unregistering the leaseholder forgets the owner when clearing its published nav data throws` () {
824+ val fixture = Fixture .create()
825+ val navLeaseCoordinator = fixture.navLeaseCoordinator
826+ val failure = IllegalStateException (" clear failed" )
827+ fixture.acquireLease(owner, onClearPublishedData = { throw failure })
828+
829+ val thrown =
830+ assertThrows(IllegalStateException ::class .java) {
831+ navLeaseCoordinator.unregisterCandidate(owner, fixture.scope)
832+ }
833+
834+ assertThat(thrown).isSameInstanceAs(failure)
835+ assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
836+
837+ fixture.published.clear()
838+ navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
839+ fixture.acquireLease(incoming)
840+
841+ assertThat(fixture.published).containsExactly(incoming)
842+ assertThat(navLeaseCoordinator.hasLease(incoming)).isTrue()
843+ }
844+
845+ @Test
846+ fun `unregistering the leaseholder forgets the owner when restoring a fallback transaction throws` () {
847+ val fixture = Fixture .create()
848+ val navLeaseCoordinator = fixture.navLeaseCoordinator
849+ val failure = IllegalStateException (" fallback restoration failed" )
850+ val failingScope = mock<IScope >()
851+ fixture.acquireLease(previous, transaction = transaction)
852+ fixture.acquireLease(owner)
853+ doThrow(failure).whenever(failingScope).withTransaction(any())
854+
855+ val thrown =
856+ assertThrows(IllegalStateException ::class .java) {
857+ navLeaseCoordinator.unregisterCandidate(owner, failingScope)
858+ }
859+
860+ assertThat(thrown).isSameInstanceAs(failure)
861+ assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
862+ assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isTrue()
863+
864+ fixture.published.clear()
865+ fixture.cleared.clear()
866+ navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
867+ fixture.acquireLease(incoming)
868+
869+ assertThat(fixture.published).containsExactly(incoming)
870+ assertThat(fixture.cleared).doesNotContain(owner)
871+ }
872+
873+ @Test
874+ fun `unregistering the leaseholder preserves the fallback lease when publishing fallback nav data throws` () {
875+ val fixture = Fixture .create()
876+ val navLeaseCoordinator = fixture.navLeaseCoordinator
877+ val failure = IllegalStateException (" fallback publication failed" )
878+ var failPublication = false
879+ navLeaseCoordinator.registerCandidate(
880+ previous,
881+ Lifecycle .State .RESUMED ,
882+ onPublishData = {
883+ if (failPublication) {
884+ throw failure
885+ }
886+ },
887+ onClearPublishedData = {},
888+ )
889+ navLeaseCoordinator.onBackStackUpdate(previous, fixture.scope)
890+ fixture.acquireLease(owner, transaction = transaction)
891+ failPublication = true
892+
893+ val thrown =
894+ assertThrows(IllegalStateException ::class .java) {
895+ navLeaseCoordinator.unregisterCandidate(owner, fixture.scope)
896+ }
897+
898+ assertThat(thrown).isSameInstanceAs(failure)
899+ assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
900+ assertThat(navLeaseCoordinator.hasLease(previous)).isTrue()
901+ assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isFalse()
902+
903+ fixture.published.clear()
904+ navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
905+
906+ assertThat(fixture.published).isEmpty()
907+ assertThat(navLeaseCoordinator.hasLease(previous)).isTrue()
908+ }
909+
768910 private class Fixture {
769911 val navLeaseCoordinator = NavLeaseCoordinator ()
770912 val scope = Scope (SentryOptions ())
771913 val published = mutableListOf<Any >()
772914 val cleared = mutableListOf<Any >()
773915
774- fun register (owner : Any , state : Lifecycle .State ) {
916+ fun register (
917+ owner : Any ,
918+ state : Lifecycle .State ,
919+ onClearPublishedData : (IScope ) -> Unit = { cleared += owner },
920+ ) {
775921 navLeaseCoordinator.registerCandidate(
776922 owner,
777923 state,
778924 onPublishData = { published + = owner },
779- onClearPublishedData = { cleared + = owner } ,
925+ onClearPublishedData = onClearPublishedData ,
780926 )
781927 }
782928
783929 fun acquireLease (
784930 owner : Any ,
785931 state : Lifecycle .State = Lifecycle .State .RESUMED ,
786932 transaction : ITransaction ? = null,
933+ onClearPublishedData : (IScope ) -> Unit = { cleared += owner },
787934 ) {
788- register(owner, state)
935+ register(owner, state, onClearPublishedData )
789936 navLeaseCoordinator.onBackStackUpdate(owner, scope)
790937 if (transaction != null ) {
791938 navLeaseCoordinator.retainTransaction(owner, transaction)
0 commit comments