-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test that simulates the deadlock scenario in SubscriptionMan…
…agerTests.kt
- Loading branch information
1 parent
52987ab
commit b40fc6b
Showing
6 changed files
with
164 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package com.onesignal.user.internal.subscriptions | ||
|
||
import com.onesignal.common.modeling.IModelChangedHandler | ||
import com.onesignal.common.modeling.ISingletonModelStoreChangeHandler | ||
import com.onesignal.common.modeling.ModelChangeTags | ||
import com.onesignal.common.modeling.ModelChangedArgs | ||
import com.onesignal.core.internal.application.IApplicationService | ||
import com.onesignal.core.internal.config.ConfigModel | ||
import com.onesignal.extensions.RobolectricTest | ||
import com.onesignal.mocks.MockHelper | ||
import com.onesignal.session.internal.session.ISessionService | ||
import com.onesignal.user.internal.subscriptions.impl.SubscriptionManager | ||
import com.onesignal.user.subscriptions.ISmsSubscription | ||
|
@@ -18,11 +23,45 @@ import io.mockk.mockk | |
import io.mockk.runs | ||
import io.mockk.spyk | ||
import io.mockk.verify | ||
import junit.framework.TestCase | ||
import org.junit.runner.RunWith | ||
|
||
@RobolectricTest | ||
@RunWith(KotestTestRunner::class) | ||
class SubscriptionManagerTests : FunSpec({ | ||
|
||
test("Deadlock related to Model.setOptAnyProperty") { | ||
// Given | ||
val modelStore = MockHelper.configModelStore() | ||
val model = modelStore.model | ||
|
||
val t1 = Thread { | ||
// acquire "model.data", then trigger the onChanged event | ||
model.setOptAnyProperty("key1", "value1") | ||
} | ||
|
||
val t2 = Thread { | ||
// acquire "model.initializationLock", then wait for "model.data" to be released | ||
model.initializeFromModel("", MockHelper.configModelStore().model) | ||
} | ||
|
||
model.subscribe(object : IModelChangedHandler { | ||
// will be executed in t1 | ||
override fun onChanged(args: ModelChangedArgs, tag: String) { | ||
Thread.sleep(200) | ||
// waiting for "model.initializationLock" | ||
model.toJSON() | ||
} | ||
}) | ||
|
||
t1.start() | ||
t2.start() | ||
// Give some time for the thread to complete the task | ||
Thread.sleep(1000) | ||
|
||
TestCase.assertEquals(Thread.State.TERMINATED, t1.state) | ||
} | ||
|
||
test("initializes subscriptions from model store") { | ||
// Given | ||
val mockSubscriptionModelStore = mockk<SubscriptionModelStore>() | ||
|
@@ -91,12 +130,12 @@ class SubscriptionManagerTests : FunSpec({ | |
// Then | ||
verify { | ||
mockSubscriptionModelStore.add( | ||
withArg { | ||
it.type shouldBe SubscriptionType.EMAIL | ||
it.address shouldBe "[email protected]" | ||
it.optedIn shouldBe true | ||
it.status shouldBe SubscriptionStatus.SUBSCRIBED | ||
}, | ||
withArg { | ||
it.type shouldBe SubscriptionType.EMAIL | ||
it.address shouldBe "[email protected]" | ||
it.optedIn shouldBe true | ||
it.status shouldBe SubscriptionStatus.SUBSCRIBED | ||
}, | ||
) | ||
} | ||
} | ||
|
@@ -120,12 +159,12 @@ class SubscriptionManagerTests : FunSpec({ | |
// Then | ||
verify { | ||
mockSubscriptionModelStore.add( | ||
withArg { | ||
it.type shouldBe SubscriptionType.SMS | ||
it.address shouldBe "+15558675309" | ||
it.optedIn shouldBe true | ||
it.status shouldBe SubscriptionStatus.SUBSCRIBED | ||
}, | ||
withArg { | ||
it.type shouldBe SubscriptionType.SMS | ||
it.address shouldBe "+15558675309" | ||
it.optedIn shouldBe true | ||
it.status shouldBe SubscriptionStatus.SUBSCRIBED | ||
}, | ||
) | ||
} | ||
} | ||
|
@@ -149,12 +188,12 @@ class SubscriptionManagerTests : FunSpec({ | |
// Then | ||
verify { | ||
mockSubscriptionModelStore.add( | ||
withArg { | ||
it.type shouldBe SubscriptionType.PUSH | ||
it.address shouldBe "pushToken" | ||
it.optedIn shouldBe true | ||
it.status shouldBe SubscriptionStatus.SUBSCRIBED | ||
}, | ||
withArg { | ||
it.type shouldBe SubscriptionType.PUSH | ||
it.address shouldBe "pushToken" | ||
it.optedIn shouldBe true | ||
it.status shouldBe SubscriptionStatus.SUBSCRIBED | ||
}, | ||
) | ||
} | ||
} | ||
|
@@ -279,11 +318,11 @@ class SubscriptionManagerTests : FunSpec({ | |
subscriptions.smss[0].number shouldBe smsSubscription.address | ||
verify(exactly = 1) { | ||
spySubscriptionChangedHandler.onSubscriptionAdded( | ||
withArg { | ||
it.id shouldBe smsSubscription.id | ||
it should beInstanceOf<ISmsSubscription>() | ||
(it as ISmsSubscription).number shouldBe smsSubscription.address | ||
}, | ||
withArg { | ||
it.id shouldBe smsSubscription.id | ||
it should beInstanceOf<ISmsSubscription>() | ||
(it as ISmsSubscription).number shouldBe smsSubscription.address | ||
}, | ||
) | ||
} | ||
} | ||
|
@@ -313,14 +352,14 @@ class SubscriptionManagerTests : FunSpec({ | |
// When | ||
emailSubscription.address = "+15551234567" | ||
subscriptionManager.onModelUpdated( | ||
ModelChangedArgs( | ||
emailSubscription, | ||
SubscriptionModel::address.name, | ||
SubscriptionModel::address.name, | ||
"+15558675309", | ||
"+15551234567", | ||
), | ||
ModelChangeTags.NORMAL, | ||
ModelChangedArgs( | ||
emailSubscription, | ||
SubscriptionModel::address.name, | ||
SubscriptionModel::address.name, | ||
"+15558675309", | ||
"+15551234567", | ||
), | ||
ModelChangeTags.NORMAL, | ||
) | ||
val subscriptions = subscriptionManager.subscriptions | ||
|
||
|
@@ -361,11 +400,11 @@ class SubscriptionManagerTests : FunSpec({ | |
subscriptions.smss.count() shouldBe 0 | ||
verify(exactly = 1) { | ||
spySubscriptionChangedHandler.onSubscriptionRemoved( | ||
withArg { | ||
it.id shouldBe smsSubscription.id | ||
it should beInstanceOf<ISmsSubscription>() | ||
(it as ISmsSubscription).number shouldBe smsSubscription.address | ||
}, | ||
withArg { | ||
it.id shouldBe smsSubscription.id | ||
it should beInstanceOf<ISmsSubscription>() | ||
(it as ISmsSubscription).number shouldBe smsSubscription.address | ||
}, | ||
) | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
OneSignalSDK/onesignal/src/test/java/com/onesignal/common/DeadlockTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.onesignal.common | ||
|
||
import com.onesignal.common.modeling.IModelChangedHandler | ||
import com.onesignal.common.modeling.MapModel | ||
import com.onesignal.common.modeling.ModelChangedArgs | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.runner.junit4.KotestTestRunner | ||
import junit.framework.TestCase | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(KotestTestRunner::class) | ||
class DeadlockTests : FunSpec({ | ||
|
||
}) |