Skip to content

Commit a8c9786

Browse files
Steve Kirkland-WaltonSteve Kirkland-Walton
Steve Kirkland-Walton
authored and
Steve Kirkland-Walton
committed
Merge branch 'next' into tms/step-use
2 parents 052b06c + d760dd6 commit a8c9786

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
submodules: recursive
5050
- uses: gradle/wrapper-validation-action@f9c9c575b8b21b6485636a91ffecd10e558c62f6 #v3.5.0
5151

52-
- uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 #v4.7.0
52+
- uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 #v4.7.1
5353
with:
5454
distribution: 'zulu'
5555
java-version: 17

bugsnag-android-performance/src/test/java/com/bugsnag/android/performance/ContextAwareScheduledThreadPoolExecutorTest.kt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import android.os.SystemClock
44
import com.bugsnag.android.performance.internal.SpanFactory
55
import com.bugsnag.android.performance.test.CollectingSpanProcessor
66
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertNotNull
78
import org.junit.Assert.assertSame
8-
import org.junit.Assert.assertTrue
99
import org.junit.Before
1010
import org.junit.Test
1111
import org.junit.runner.RunWith
1212
import org.robolectric.RobolectricTestRunner
1313
import org.robolectric.annotation.Config
1414
import org.robolectric.shadows.ShadowPausedSystemClock
15-
import java.lang.Thread.sleep
1615
import java.util.concurrent.Callable
17-
import java.util.concurrent.CountDownLatch
16+
import java.util.concurrent.SynchronousQueue
1817
import java.util.concurrent.TimeUnit
1918

2019
@RunWith(RobolectricTestRunner::class)
@@ -81,25 +80,28 @@ class ContextAwareScheduledThreadPoolExecutorTest {
8180
fun scheduleWithFixedDelay() {
8281
val scheduledExecutor = ContextAwareScheduledThreadPoolExecutor(1)
8382
var startTime = SystemClock.elapsedRealtime()
84-
val countDownLatch = CountDownLatch(2)
83+
// this is a weird use of SynchronousQueue - we use it to block until the appropriate spans are created
84+
// we could probably use a Phaser or Semaphore instead but this is simpler to understand
85+
// the span tasks put Units in the queue to signal that they have run, and we poll the queue to wait for them
86+
val sync = SynchronousQueue<Unit>()
8587
spanFactory.createCustomSpan("parent").use {
8688
scheduledExecutor.scheduleWithFixedDelay(
8789
{
8890
startTime += 100L
8991
SystemClock.setCurrentTimeMillis(startTime)
9092
spanFactory.createCustomSpan("child").end()
91-
countDownLatch.countDown()
93+
sync.put(Unit)
9294
},
9395
0L,
9496
100L,
9597
TimeUnit.MILLISECONDS,
9698
)
9799

98100
// end the parent span after one task execution
99-
sleep(75L)
101+
sync.poll(500, TimeUnit.MILLISECONDS)
100102
}
101103

102-
assertTrue(countDownLatch.await(500L, TimeUnit.MILLISECONDS))
104+
assertNotNull(sync.poll(500, TimeUnit.MILLISECONDS))
103105
scheduledExecutor.shutdownNow()
104106

105107
val collectedSpans = spanProcessor.toList()
@@ -112,25 +114,28 @@ class ContextAwareScheduledThreadPoolExecutorTest {
112114
fun scheduleAtFixedRate() {
113115
val scheduledExecutor = ContextAwareScheduledThreadPoolExecutor(1)
114116
var startTime = SystemClock.elapsedRealtime()
115-
val countDownLatch = CountDownLatch(2)
117+
// this is a weird use of SynchronousQueue - we use it to block until the appropriate spans are created
118+
// we could probably use a Phaser or Semaphore instead but this is simpler to understand
119+
// the span tasks put Units in the queue to signal that they have run, and we poll the queue to wait for them
120+
val sync = SynchronousQueue<Unit>()
116121
spanFactory.createCustomSpan("parent").use {
117122
scheduledExecutor.scheduleAtFixedRate(
118123
{
119124
startTime += 100L
120125
SystemClock.setCurrentTimeMillis(startTime)
121126
spanFactory.createCustomSpan("child").end()
122-
countDownLatch.countDown()
127+
sync.put(Unit)
123128
},
124129
0L,
125130
100L,
126131
TimeUnit.MILLISECONDS,
127132
)
128133

129134
// end the parent span after one task execution
130-
sleep(75L)
135+
sync.poll(500, TimeUnit.MILLISECONDS)
131136
}
132137

133-
assertTrue(countDownLatch.await(500L, TimeUnit.MILLISECONDS))
138+
assertNotNull(sync.poll(500, TimeUnit.MILLISECONDS))
134139
scheduledExecutor.shutdownNow()
135140

136141
val collectedSpans = spanProcessor.toList()

bugsnag-android-performance/src/test/java/com/bugsnag/android/performance/ContextAwareThreadPoolExecutorTest.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.bugsnag.android.performance
33
import com.bugsnag.android.performance.internal.SpanFactory
44
import com.bugsnag.android.performance.test.CollectingSpanProcessor
55
import org.junit.Assert.assertEquals
6+
import org.junit.Assert.assertTrue
67
import org.junit.Before
78
import org.junit.Test
89
import org.junit.runner.RunWith
@@ -47,15 +48,17 @@ class ContextAwareThreadPoolExecutorTest {
4748
fun closedContext() {
4849
val executor = ContextAwareThreadPoolExecutor(1, 1, 1L, TimeUnit.MILLISECONDS, LinkedBlockingQueue())
4950
spanFactory.createCustomSpan("parent").use {
50-
executor.submit {
51+
executor.execute {
5152
// allow the parent span to close before starting a span within the task
5253
sleep(10L)
5354
spanFactory.createCustomSpan("child").end(10L)
5455
}
5556
}
5657

58+
executor.shutdown()
59+
5760
// wait for the task to complete
58-
executor.awaitTermination(20L, TimeUnit.MILLISECONDS)
61+
assertTrue(executor.awaitTermination(500, TimeUnit.MILLISECONDS))
5962

6063
val collectedSpans = spanProcessor.toList()
6164
assertEquals(2, collectedSpans.size)

0 commit comments

Comments
 (0)