package io.ray.test;

import com.google.common.collect.ImmutableList;
import io.ray.api.ActorHandle;
import io.ray.api.Ray;
import io.ray.api.id.JobId;
import io.ray.runtime.util.SystemUtil;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

@Test(groups = {"cluster"})
public class RayJavaLoggingTest extends BaseTest {

  private static final Logger LOG = LoggerFactory.getLogger(RayJavaLoggingTest.class);

  private static class HeavyLoggingActor {
    public int getPid() {
      return SystemUtil.pid();
    }

    public boolean log() {
      for (int i = 0; i < 100000; ++i) {
        LOG.info("hello world, this is a log.");
      }
      return true;
    }
  }

  @Test
  public void testJavaLoggingRotate() {
    ActorHandle<HeavyLoggingActor> loggingActor =
        Ray.actor(HeavyLoggingActor::new)
            .setJvmOptions(
                ImmutableList.of(
                    "-Dray.logging.max-file-size=1KB", "-Dray.logging.max-backup-files=3"))
            .remote();
    Assert.assertTrue(loggingActor.task(HeavyLoggingActor::log).remote().get());
    final int pid = loggingActor.task(HeavyLoggingActor::getPid).remote().get();
    final JobId jobId = Ray.getRuntimeContext().getCurrentJobId();
    String currLogDir = "/tmp/ray/session_latest/logs";
    for (int i = 1; i <= 3; ++i) {
      File rotatedFile =
          new File(String.format("%s/java-worker-%s-%d.%d.log", currLogDir, jobId, pid, i));
      Assert.assertTrue(rotatedFile.exists());
      long fileSize = rotatedFile.length();
      Assert.assertTrue(fileSize > 1024 && fileSize < 1024 * 2);
    }
  }
}

@@ -1230,24 +1230,328 @@
 te().get();%0A
+    // Note(qwang): Due to log4j2 is async, once the %60log()%60 actor method returned, we%0A    // still have no confident to make sure all log messages are printed to the log files,%0A    // especially on slow CI machine.%0A    boolean rotated =%0A        TestUtils.waitForCondition(%0A            () -%3E %7B%0A          
     final Jo
@@ -1601,24 +1601,34 @@
 entJobId();%0A
+          
     String c
@@ -1671,16 +1671,26 @@
 /logs%22;%0A
+          
     for 
@@ -1712,24 +1712,34 @@
 = 3; ++i) %7B%0A
+          
       File r
@@ -1761,16 +1761,26 @@
         
+       
+   
 new File
@@ -1780,16 +1780,41 @@
 ew File(
+%0A                        
 String.f
@@ -1885,34 +1885,31 @@
 ;%0A      
-Assert.assertTrue(
+          if (!
 rotatedF
@@ -1921,18 +1921,79 @@
 xists())
-;%0A
+ %7B%0A                  return false;%0A                %7D%0A          
       lo
@@ -2034,34 +2034,53 @@
 ;%0A      
-Assert.assertTrue(
+          boolean fileSizeExpected = 
 fileSize
@@ -2109,23 +2109,221 @@
 1024 * 2
-);%0A    %7D
+;%0A                if (!fileSizeExpected) %7B%0A                  return false;%0A                %7D%0A              %7D%0A              return true;%0A            %7D,%0A            10 * 1000);%0A    Assert.assertTrue(rotated);
 %0A  %7D%0A%7D%0A

Fix java logging rotated test on slow CI machine. (#29929)