Skip to content

Commit

Permalink
added a way to crash the app: after trying to add exactly 10 National…
Browse files Browse the repository at this point in the history
… Park Foundation Explorascopes and confirming on the pop-up, the multiThreadCrashing function is called, which simulates a multi-threaded crash. The way to crash is copied from splunk-otel-android sample app.
  • Loading branch information
magda-woj committed Sep 11, 2024
1 parent 66a9df3 commit 1fe06d0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable

import androidx.compose.ui.tooling.preview.Preview


@Composable
fun ConfirmCrashPopup(
onConfirm: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import io.opentelemetry.android.demo.shop.ui.components.UpPressButton
import androidx.compose.ui.Alignment
import io.opentelemetry.android.demo.shop.clients.ProductCatalogClient
import io.opentelemetry.android.demo.shop.clients.RecommendationService
import io.opentelemetry.android.demo.shop.ui.components.ConfirmCrashPopup
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

@Composable
fun ProductDetails(
Expand Down Expand Up @@ -83,14 +86,7 @@ fun ProductDetails(
Spacer(modifier = Modifier.height(32.dp))
QuantityChooser(quantity = quantity, onQuantityChange = { quantity = it })
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = { cartViewModel.addProduct(product, quantity) },
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Text(text = "Add to Cart")
}
AddToCartButton(cartViewModel = cartViewModel, product = product, quantity = quantity)
Spacer(modifier = Modifier.height(32.dp))
RecommendedSection(recommendedProducts = recommendedProducts, onProductClick = onProductClick)
}
Expand All @@ -104,4 +100,68 @@ fun ProductDetails(
}
}

@Composable
fun AddToCartButton(
cartViewModel: CartViewModel,
product: Product,
quantity: Int
) {

var showPopup by remember { mutableStateOf(false) }

Button(
onClick = {
if (product.id == "OLJCESPC7Z" && quantity == 10) {
showPopup = true
} else {
cartViewModel.addProduct(product, quantity)
}
},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Text(text = "Add to Cart")
}

if (showPopup) {
ConfirmCrashPopup(
onConfirm = {
multiThreadCrashing()
},
onDismiss = {
showPopup = false
}
)
}
}

fun multiThreadCrashing(numThreads : Int = 4) {
val latch = CountDownLatch(1)

for (i in 0..numThreads) {
val thread = Thread {
try {
if (latch.await(10, TimeUnit.SECONDS)) {
throw IllegalStateException("Failure from thread ${Thread.currentThread().name}")
}
} catch (e: InterruptedException) {
throw RuntimeException(e)
}
}
thread.name = "crash-thread-$i"
thread.start()
}

try {
Thread.sleep(100)
} catch (e: InterruptedException) {
Thread.currentThread().interrupt()
return
}
latch.countDown()
}




0 comments on commit 1fe06d0

Please sign in to comment.