/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.pool2.impl;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.jupiter.api.Test;

/**
 * Tests {@link DefaultPooledObject}.
 */
public class TestDefaultPooledObject {

    @Test
    public void testGetActiveDuration() throws InterruptedException {
        final DefaultPooledObject<Object> dpo = new DefaultPooledObject<>(new Object());
        // Sleep MUST be "long enough" to test that we are not returning a negative time.
        Thread.sleep(200);
        assertFalse(dpo.getActiveDuration().isNegative());
        assertFalse(dpo.getActiveDuration().isZero());
        assertThat(dpo.getActiveDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis()));
        assertThat(dpo.getActiveDuration(), lessThanOrEqualTo(dpo.getActiveTime()));
    }

    /**
     * JIRA: POOL-279
     * @throws Exception May occur in some failure modes
     */
    @Test
    public void testGetIdleTimeMillis() throws Exception {
        final DefaultPooledObject<Object> dpo = new DefaultPooledObject<>(new Object());
        final AtomicBoolean negativeIdleTimeReturned = new AtomicBoolean(false);
        final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 3);
        final Runnable allocateAndDeallocateTask = () -> {
            for (int i1 = 0; i1 < 10000; i1++) {
                if (dpo.getIdleTime().isNegative()) {
                    negativeIdleTimeReturned.set(true);
                    break;
                }
            }
            dpo.allocate();
            for (int i2 = 0; i2 < 10000; i2++) {
                if (dpo.getIdleTime().isNegative()) {
                    negativeIdleTimeReturned.set(true);
                    break;
                }
            }
            dpo.deallocate();
        };
        final Runnable getIdleTimeTask = () -> {
            for (int i = 0; i < 10000; i++) {
                if (dpo.getIdleTime().isNegative()) {
                    negativeIdleTimeReturned.set(true);
                    break;
                }
            }
        };
        final double probabilityOfAllocationTask = 0.7;
        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 1; i <= 10000; i++) {
            final Runnable randomTask = Math.random() < probabilityOfAllocationTask ? allocateAndDeallocateTask
                    : getIdleTimeTask;
            futures.add(executor.submit(randomTask));
        }
        for (final Future<?> future : futures) {
            future.get();
        }
        assertFalse(negativeIdleTimeReturned.get(),
                "DefaultPooledObject.getIdleTimeMillis() returned a negative value");
    }
}
@@ -944,16 +944,77 @@
 qualTo;%0A
+import static org.junit.jupiter.api.Assertions.assertEquals;%0A
 import s
@@ -1065,16 +1065,16 @@
 tFalse;%0A
-
 %0Aimport 
@@ -1440,19 +1440,28 @@
 oid test
-Get
+InitialState
 ActiveDu
@@ -2002,24 +2002,1588 @@
 veTime()));%0A
+        //%0A        assertThat(dpo.getActiveDuration(), lessThanOrEqualTo(dpo.getIdleDuration()));%0A    %7D%0A%0A    @Test%0A    public void testInitialStateIdleDuration() throws InterruptedException %7B%0A        final DefaultPooledObject%3CObject%3E dpo = new DefaultPooledObject%3C%3E(new Object());%0A        // Sleep MUST be %22long enough%22 to test that we are not returning a negative time.%0A        Thread.sleep(200);%0A        assertFalse(dpo.getIdleDuration().isNegative());%0A        assertFalse(dpo.getIdleDuration().isZero());%0A        assertThat(dpo.getIdleDuration().toMillis(), lessThanOrEqualTo(dpo.getActiveTimeMillis()));%0A        assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getActiveTime()));%0A        //%0A        assertThat(dpo.getIdleDuration(), lessThanOrEqualTo(dpo.getActiveDuration()));%0A    %7D%0A%0A    @Test%0A    public void testInitialStateCreateInstant() throws InterruptedException %7B%0A        final DefaultPooledObject%3CObject%3E dpo = new DefaultPooledObject%3C%3E(new Object());%0A%0A        // Instant vs. long%0A        assertEquals(dpo.getCreateInstant().toEpochMilli(), dpo.getCreateTime());%0A%0A        // Instant vs. Instant%0A        assertEquals(dpo.getCreateInstant(), dpo.getLastBorrowInstant());%0A        assertEquals(dpo.getCreateInstant(), dpo.getLastReturnInstant());%0A        assertEquals(dpo.getCreateInstant(), dpo.getLastUsedInstant());%0A%0A        // long vs. long%0A        assertEquals(dpo.getCreateTime(), dpo.getLastBorrowTime());%0A        assertEquals(dpo.getCreateTime(), dpo.getLastReturnTime());%0A        assertEquals(dpo.getCreateTime(), dpo.getLastUsedTime());%0A
     %7D%0A%0A    /
@@ -3607,16 +3607,23 @@
 OOL-279%0A
+     *%0A
      * @
@@ -5145,36 +5145,16 @@
 cateTask
-%0A                   
  : getId
@@ -5306,32 +5306,32 @@
 et();%0A        %7D%0A
+
         assertFa
@@ -5365,32 +5365,16 @@
 d.get(),
-%0A               
  %22Defaul

Add tests.