Minor: Fix NPE for botUser during race Condition, Timestamp Comparison (#23660)

* Fix NPE for botUser during race Condition

* Fix Timestamp processing for date assertions
This commit is contained in:
Ram Narayan Balaji 2025-10-03 14:36:11 +05:30 committed by GitHub
parent 453c57df9f
commit 2cf7466911
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 3 deletions

View File

@ -52,7 +52,17 @@ public class BotRepository extends EntityRepository<Bot> {
@Override
public void prepare(Bot entity, boolean update) {
User user = Entity.getEntity(entity.getBotUser(), "", Include.ALL);
EntityReference botUserRef = entity.getBotUser();
if (botUserRef == null) {
// Race condition detected: Retry by fetching the relationship directly from the database
botUserRef = getBotUser(entity);
}
if (botUserRef == null) {
// This should never happen
throw new IllegalStateException(
String.format("Bot entity [%s] is missing required botUser reference", entity.getId()));
}
User user = Entity.getEntity(botUserRef, "", Include.ALL);
entity.withBotUser(user.getEntityReference());
}

View File

@ -4497,11 +4497,18 @@ public abstract class EntityResourceTest<T extends EntityInterface, K extends Cr
.withConfigType(SettingsType.ASSET_CERTIFICATION_SETTINGS)
.withConfigValue(certificationSettings));
long timestampBeforePatch = System.currentTimeMillis();
T patchedEntity = patchEntity(entity.getId(), json, entity, ADMIN_AUTH_HEADERS);
long timestampAfterPatch = System.currentTimeMillis();
assertEquals(
patchedEntity.getCertification().getTagLabel().getTagFQN(), certificationLabel.getTagFQN());
assertEquals(
patchedEntity.getCertification().getAppliedDate(), System.currentTimeMillis(), 10 * 1000);
// Verify the applied date is within the time window of the patch operation
assertTrue(
patchedEntity.getCertification().getAppliedDate() >= timestampBeforePatch - 1000,
"Applied date should be at or after the patch operation started (with 1s tolerance)");
assertTrue(
patchedEntity.getCertification().getAppliedDate() <= timestampAfterPatch + 1000,
"Applied date should be at or before the patch operation completed (with 1s tolerance)");
assertEquals(
(double)
(patchedEntity.getCertification().getExpiryDate()