Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1037 from square/jwilson.0605.concurrent_mock_tracer
Fix races in ClientServerTraceTest
- Loading branch information
Showing
5 changed files
with
73 additions
and
64 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
misk-testing/src/main/kotlin/misk/testing/ConcurrentMockTracer.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,27 @@ | ||
package misk.testing | ||
|
||
import io.opentracing.mock.MockSpan | ||
import io.opentracing.mock.MockTracer | ||
import java.util.concurrent.LinkedBlockingDeque | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
/** | ||
* Extends [MockTracer] for use in concurrent environments, such as a web server and test client. | ||
* Prefer this wherever you'd otherwise use [MockTracer]. | ||
*/ | ||
@Singleton | ||
class ConcurrentMockTracer @Inject constructor() : MockTracer() { | ||
private val queue = LinkedBlockingDeque<MockSpan>() | ||
|
||
/** Awaits a span, removes it, and returns it. */ | ||
fun take(): MockSpan { | ||
return queue.poll(500, TimeUnit.MILLISECONDS) ?: throw IllegalArgumentException("no spans!") | ||
} | ||
|
||
override fun onSpanFinished(mockSpan: MockSpan) { | ||
super.onSpanFinished(mockSpan) | ||
queue.put(mockSpan) | ||
} | ||
} |
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