Search in sources :

Example 1 with AccelerateTimeService

use of com.sun.identity.shared.timeservice.AccelerateTimeService in project OpenAM by OpenRock.

the class DebugRotationTest method rotation.

private void rotation(long fakeInitTime) throws Exception {
    String DEBUG_CONFIG_FOR_TEST = "/debug_config_test/debugconfigRotation.properties";
    DebugConfigurationFromProperties debugConfigurationFromProperties = new DebugConfigurationFromProperties(DEBUG_CONFIG_FOR_TEST);
    initializeProperties();
    initializeProvider(DEBUG_CONFIG_FOR_TEST);
    //initialize a scenario
    SimpleDateFormat dateFormat = new SimpleDateFormat("-MM.dd.yyyy-HH.mm");
    String debugNameFile = "debugMerge";
    int rotationPeriod = debugConfigurationFromProperties.getRotationInterval();
    //Accelerate the test timeservice
    // Simulate 1 hours of logs
    int fakeDurationMs = 60 * 60 * 1000;
    //In order to have an effective and short in time test, we accelerate the time
    AccelerateTimeService accelerateClock = new AccelerateTimeService(fakeInitTime);
    debugFileProvider.setClock(accelerateClock);
    // check debugFiles.properties to see the mapping
    IDebug debugTest1MergeToDebugMerge = provider.getInstance("debugTest1MergeToDebugMerge");
    IDebug debugTest2MergeToDebugMerge = provider.getInstance("debugTest2MergeToDebugMerge");
    IDebug debugTest3MergeToDebugMerge = provider.getInstance("debugTest3MergeToDebugMerge");
    // We will print on logs from threads, for testing the synchronized
    List<PrintLogRunnable> printLogRunnableTests = new ArrayList<PrintLogRunnable>();
    PrintLogRunnable printLogRunnableTest1 = new PrintLogRunnable(debugTest1MergeToDebugMerge, fakeInitTime, fakeDurationMs, accelerateClock);
    printLogRunnableTests.add(printLogRunnableTest1);
    PrintLogRunnable printLogRunnableTest2 = new PrintLogRunnable(debugTest2MergeToDebugMerge, fakeInitTime, fakeDurationMs, accelerateClock);
    printLogRunnableTests.add(printLogRunnableTest2);
    PrintLogRunnable printLogRunnableTest3 = new PrintLogRunnable(debugTest3MergeToDebugMerge, fakeInitTime, fakeDurationMs, accelerateClock);
    printLogRunnableTests.add(printLogRunnableTest3);
    List<Thread> threads = new ArrayList<Thread>();
    for (PrintLogRunnable printLogRunnableTest : printLogRunnableTests) {
        threads.add(new Thread(printLogRunnableTest));
    }
    //The first writing initialize the log file. So we test that a swift of 1 minute doesn't
    //create a new file at the end
    debugTest1MergeToDebugMerge.message("Should appear in log", null);
    accelerateClock.incrementTime(1000 * 60 + 10);
    debugTest2MergeToDebugMerge.message("Should appear in log", null);
    accelerateClock.incrementTime(1000 * 60 + 10);
    debugTest3MergeToDebugMerge.message("Should appear in log", null);
    //Start threads
    for (Thread thread : threads) {
        thread.start();
    }
    //Wait threads
    for (Thread thread : threads) {
        thread.join();
    }
    //Check if any thread had a exception
    for (PrintLogRunnable printLogRunnableTest : printLogRunnableTests) {
        if (printLogRunnableTest.ex != null)
            throw printLogRunnableTest.ex;
    }
    //Check files creation
    Calendar fakeDate = Calendar.getInstance();
    fakeDate.setTimeInMillis(fakeInitTime);
    int currentPeriod = -1;
    while (fakeDate.getTimeInMillis() - fakeInitTime < fakeDurationMs) {
        if (isFileExist(debugNameFile + dateFormat.format(fakeDate.getTime()))) {
            if (currentPeriod != -1 && currentPeriod < rotationPeriod) {
                failAndPrintFolderStatusReport("A log rotation file is created before the log rotation ended. " + "currentPeriod= '" + currentPeriod + "'");
            }
            currentPeriod = 0;
        }
        currentPeriod++;
        fakeDate.add(Calendar.MINUTE, 1);
    }
}
Also used : DebugConfigurationFromProperties(com.sun.identity.shared.debug.file.impl.DebugConfigurationFromProperties) AccelerateTimeService(com.sun.identity.shared.timeservice.AccelerateTimeService) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

DebugConfigurationFromProperties (com.sun.identity.shared.debug.file.impl.DebugConfigurationFromProperties)1 AccelerateTimeService (com.sun.identity.shared.timeservice.AccelerateTimeService)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1