-
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.
- Loading branch information
1 parent
fbe9f74
commit 3d34cf0
Showing
8 changed files
with
195 additions
and
214 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
119 changes: 119 additions & 0 deletions
119
OneSignalSDK/onesignal/core/src/test/java/com/onesignal/common/ModelingTests.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,119 @@ | ||
package com.onesignal.common | ||
|
||
import com.onesignal.common.events.EventProducer | ||
import com.onesignal.common.modeling.IModelChangedHandler | ||
import com.onesignal.common.modeling.IModelStoreChangeHandler | ||
import com.onesignal.common.modeling.ModelChangedArgs | ||
import com.onesignal.mocks.MockHelper | ||
import com.onesignal.mocks.MockPreferencesService | ||
import com.onesignal.user.internal.subscriptions.SubscriptionModel | ||
import com.onesignal.user.internal.subscriptions.SubscriptionModelStore | ||
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 ModelingTests : 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() | ||
|
||
// Set 1s timeout for t2 to complete the task | ||
t2.join(1000) | ||
|
||
// verify if the thread has been successfully terminated | ||
TestCase.assertEquals(Thread.State.TERMINATED, t2.state) | ||
} | ||
|
||
test("Deadlock related to ModelSstore add() or remove()") { | ||
// Given | ||
val modelStore = SubscriptionModelStore(MockPreferencesService()) | ||
val event = EventProducer<SubscriptionModel>() | ||
val oldSubscriptionModel = SubscriptionModel() | ||
val newSubscriptionModel = SubscriptionModel() | ||
oldSubscriptionModel.id = "oldModel" | ||
newSubscriptionModel.id = "newModel" | ||
modelStore.add(oldSubscriptionModel) | ||
|
||
val t1 = | ||
Thread { | ||
// acquire "ModelStore.models", then trigger the onChanged event | ||
System.out.println("1") | ||
modelStore.add(newSubscriptionModel) | ||
} | ||
|
||
val t2 = | ||
Thread { | ||
System.out.println("2") | ||
// acquire "model.data", then wait for "ModelStore.models" | ||
newSubscriptionModel.toJSON() | ||
} | ||
|
||
modelStore.subscribe( | ||
object : IModelStoreChangeHandler<SubscriptionModel> { | ||
override fun onModelAdded( | ||
model: SubscriptionModel, | ||
tag: String, | ||
) { | ||
// waiting for "model.data" | ||
model.initializeFromModel("", MockHelper.configModelStore().model) | ||
} | ||
|
||
override fun onModelUpdated( | ||
args: ModelChangedArgs, | ||
tag: String, | ||
) { | ||
// left empty in purpose | ||
} | ||
|
||
override fun onModelRemoved( | ||
model: SubscriptionModel, | ||
tag: String, | ||
) { | ||
// left empty in purpose | ||
} | ||
}, | ||
) | ||
|
||
t1.start() | ||
t2.start() | ||
|
||
// Set 1s timeout for t2 to complete the task | ||
t2.join(1000) | ||
|
||
// verify if the thread has been successfully terminated | ||
TestCase.assertEquals(Thread.State.TERMINATED, t2.state) | ||
} | ||
}) |
Oops, something went wrong.