Skip to content

Commit

Permalink
mock time in LocalDate
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilou242 committed Feb 8, 2025
1 parent a77eda7 commit 717e6db
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 StarTree Inc
*
* Licensed under the StarTree Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at http://www.startree.ai/legal/startree-community-license
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OF ANY KIND,
* either express or implied.
* See the License for the specific language governing permissions and limitations under
* the License.
*/
package ai.startree.thirdeye.aspect;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LocalDateAspect {

@Pointcut("call(public static java.time.LocalDate java.time.LocalDate.now(..))")
void localDateNow() {
}

@Around("localDateNow()")
public Object aroundLocalDateNow(ProceedingJoinPoint pjp) throws Throwable {
if (TimeProvider.instance().isTimedMocked()) {
// assume localDate now is always called in a UTC context
return LocalDate.ofInstant(Instant.ofEpochMilli(TimeProvider.instance().currentTimeMillis()),
ZoneId.of("UTC"));
}
return pjp.proceed();
}
}
2 changes: 2 additions & 0 deletions thirdeye-persistence/src/test/resources/META-INF/aop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<aspect name="ai.startree.thirdeye.aspect.DateMockAspect"/>
<aspect name="ai.startree.thirdeye.aspect.CronTriggerImplAspect"/>
<aspect name="ai.startree.thirdeye.aspect.QuartzRandomizedIdleWaitTimeMockAspect"/>
<aspect name="ai.startree.thirdeye.aspect.ExecutorsAspect"/>
<aspect name="ai.startree.thirdeye.aspect.LocalDateAspect"/>
<weaver options="-showWeaveInfo">
</weaver>
</aspects>
Expand Down

0 comments on commit 717e6db

Please sign in to comment.