Skip to content

Commit

Permalink
[TH2-5239] Corrected after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Sep 19, 2024
1 parent e24fd51 commit 5a0d7e9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
}
}
}
Expand All @@ -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
}
}
80 changes: 62 additions & 18 deletions src/test/kotlin/handlers/IParentEventCounterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
},
)
}

Expand Down Expand Up @@ -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}",
)
},
)
}

Expand Down

0 comments on commit 5a0d7e9

Please sign in to comment.