diff --git a/src/main/kotlin/com/exactpro/th2/rptdataprovider/handlers/IParentEventCounter.kt b/src/main/kotlin/com/exactpro/th2/rptdataprovider/handlers/IParentEventCounter.kt index 72ac6d1a..34f32461 100644 --- a/src/main/kotlin/com/exactpro/th2/rptdataprovider/handlers/IParentEventCounter.kt +++ b/src/main/kotlin/com/exactpro/th2/rptdataprovider/handlers/IParentEventCounter.kt @@ -28,7 +28,7 @@ internal interface IParentEventCounter { */ fun checkCountAndGet(event: BaseEventEntity): Boolean - private class NoLimitedParentEventCounter : IParentEventCounter { + private object NoLimitedParentEventCounter : IParentEventCounter { override fun checkCountAndGet(event: BaseEventEntity): Boolean = true } @@ -50,7 +50,12 @@ internal interface IParentEventCounter { parentEventCounter.putIfAbsent(event.id.eventId.id, MAX_EVENT_COUNTER) MAX_EVENT_COUNTER } else { - if (value.incrementAndGet() > limitForParent) MAX_EVENT_COUNTER else value + if (value.incrementAndGet() > limitForParent) { + parentEventCounter.putIfAbsent(event.id.eventId.id, MAX_EVENT_COUNTER) + MAX_EVENT_COUNTER + } else { + value + } } } } @@ -60,10 +65,9 @@ internal interface IParentEventCounter { } companion object { - private val NO_LIMIT_COUNTER = NoLimitedParentEventCounter() private val MAX_EVENT_COUNTER = AtomicLong(Long.MAX_VALUE) fun create(limitForParent: Long? = null): IParentEventCounter = - limitForParent?.let { LimitedParentEventCounter(it) } ?: NO_LIMIT_COUNTER + limitForParent?.let { LimitedParentEventCounter(it) } ?: NoLimitedParentEventCounter } } \ No newline at end of file diff --git a/src/test/kotlin/handlers/IParentEventCounterTest.kt b/src/test/kotlin/handlers/IParentEventCounterTest.kt index 155f4e39..8361cec0 100644 --- a/src/test/kotlin/handlers/IParentEventCounterTest.kt +++ b/src/test/kotlin/handlers/IParentEventCounterTest.kt @@ -143,17 +143,39 @@ class IParentEventCounterTest { ) } - assertFalse( - eventCounter.checkCountAndGet( - createEventEntity( - ProviderEventId( - batchId = null, - eventId = StoredTestEventId(BOOK_ID, SCOPE, Instant.now(), NEXT_UUID), + val nextEventId = StoredTestEventId(BOOK_ID, SCOPE, Instant.now(), NEXT_UUID) + assertAll( + { + assertFalse( + eventCounter.checkCountAndGet( + createEventEntity( + ProviderEventId( + batchId = null, + eventId = nextEventId, + ), + parentEventId, + ), + ), + "single event id, attempt ${limitForParent + 1}", + ) + }, + { + assertFalse( + eventCounter.checkCountAndGet( + createEventEntity( + ProviderEventId( + batchId = null, + eventId = StoredTestEventId(BOOK_ID, SCOPE, Instant.now(), NEXT_UUID), + ), + ProviderEventId( + batchId = null, + eventId = nextEventId, + ) + ), ), - parentEventId, + "child of single event id, attempt ${limitForParent + 1}", ) - ), - "single event id, attempt ${limitForParent + 1}", + }, ) } @@ -183,17 +205,39 @@ class IParentEventCounterTest { ) } - assertFalse( - eventCounter.checkCountAndGet( - createEventEntity( - ProviderEventId( - batchId = batchId, - eventId = StoredTestEventId(BOOK_ID, SCOPE, Instant.now(), NEXT_UUID), + val nextEventId = StoredTestEventId(BOOK_ID, SCOPE, Instant.now(), NEXT_UUID) + assertAll( + { + assertFalse( + eventCounter.checkCountAndGet( + createEventEntity( + ProviderEventId( + batchId = batchId, + eventId = nextEventId, + ), + parentEventId, + ), ), - parentEventId, + "single event id, attempt ${limitForParent + 1}", ) - ), - "single event id, attempt ${limitForParent + 1}", + }, + { + assertFalse( + eventCounter.checkCountAndGet( + createEventEntity( + ProviderEventId( + batchId = batchId, + eventId = StoredTestEventId(BOOK_ID, SCOPE, Instant.now(), NEXT_UUID), + ), + ProviderEventId( + batchId = batchId, + eventId = nextEventId + ) + ), + ), + "child of single event id, attempt ${limitForParent + 1}", + ) + }, ) }