File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
akka-persistence-query/src
main/scala/akka/persistence/query
test/scala/akka/persistence/query Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -16,8 +16,6 @@ object QueryCorrelationId {
1616 /**
1717 * Expected to be used "around" calls to plugin query method, will clear the correlation id from thread local
1818 * to make sure there is no leak between logic executed on shared threads.
19- *
20- * @param correlationId
2119 */
2220 def withCorrelationId [T ](correlationId : String )(block : () => T ): T = {
2321 threadLocal.set(correlationId)
@@ -28,6 +26,17 @@ object QueryCorrelationId {
2826 }
2927 }
3028
29+ /**
30+ * Expected to be used "around" calls to plugin query method to pass along a prevously extracted optional correlation id,
31+ * will clear the correlation id from thread local to make sure there is no leak between logic executed on shared threads.
32+ */
33+ def withCorrelationId [T ](correlationId : Option [String ])(block : () => T ): T = {
34+ correlationId match {
35+ case None => block()
36+ case Some (actualId) => withCorrelationId(actualId)(block)
37+ }
38+ }
39+
3140 /**
3241 * @return Expected to be called directly after receiving a query call, before starting any asynchronous tasks,
3342 * returns and clears out the correlation id to make sure there is no leak between tasks. Further passing
Original file line number Diff line number Diff line change @@ -29,6 +29,26 @@ class QueryCorrelationIdSpec extends AnyWordSpecLike with Matchers {
2929 QueryCorrelationId .get() shouldBe None
3030 }
3131
32+ " pass along and clear correlation id if present" in {
33+ val uuid = UUID .randomUUID().toString
34+ val observed =
35+ QueryCorrelationId .withCorrelationId(Some (uuid)) { () =>
36+ pretendQueryMethod()
37+ }
38+ observed shouldEqual Some (uuid)
39+
40+ // cleared after returning
41+ QueryCorrelationId .get() shouldBe None
42+ }
43+
44+ " just invoke the block if correlation id not present" in {
45+ val observed =
46+ QueryCorrelationId .withCorrelationId(None ) { () =>
47+ pretendQueryMethod()
48+ }
49+ observed shouldEqual None
50+ }
51+
3252 " clear correlation id when call fails" in {
3353 val uuid = UUID .randomUUID().toString
3454 intercept[TestException ] {
You can’t perform that action at this time.
0 commit comments