Repository: apache/kafka
File: server-common/src/test/java/org/apache/kafka/server/util/timer/TimingWheelTest.java (Java)

Code before:
 */
package org.apache.kafka.server.util.timer;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TimingWheelTest {

    private TimingWheel timingWheel;
    private AtomicInteger taskCounter;
    private DelayQueue<TimerTaskList> queue;
    private final long startMs = 1000L;
    private final long tickMs = 10L;
    private final int wheelSize = 5;

    @BeforeEach
    public void setUp() {
        taskCounter = new AtomicInteger(0);
        queue = new DelayQueue<>();
        timingWheel = new TimingWheel(tickMs, wheelSize, startMs, taskCounter, queue);
    }

    @Test
    public void testAddValidTask() {
        // Create task within current time interval
        long expirationMs = startMs + tickMs * 2; // 1020ms
        TimerTask task = new TestTimerTask(tickMs * 2);
        TimerTaskEntry entry = new TimerTaskEntry(task, expirationMs);

        assertTrue(timingWheel.add(entry), "Should successfully add valid task");
        assertFalse(queue.isEmpty());
        assertEquals(1, taskCounter.get());
    }

    @Test
    public void testAddExpiredTask() {
        long expirationMs = startMs - 1; // 999ms, less than current time
        TimerTask task = new TestTimerTask(-1);
        TimerTaskEntry entry = new TimerTaskEntry(task, expirationMs);

        assertFalse(timingWheel.add(entry), "Expired task should not be added");

Review comment (suggestion):
those variables could be initialized in constructor, right?
```java
    private final long startMs = 1000L;
    private final long tickMs = 10L;
    private final int wheelSize = 5;
    private final DelayQueue<TimerTaskList> queue = new DelayQueue<>();
    private final AtomicInteger taskCounter = new AtomicInteger(0);
    private final TimingWheel timingWheel = new TimingWheel(tickMs, wheelSize, startMs, taskCounter, queue);
```

Code after:
 */
package org.apache.kafka.server.util.timer;

import org.junit.jupiter.api.Test;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TimingWheelTest {

    @Test
    public void testAddValidTask() {
        AtomicInteger taskCounter = new AtomicInteger(0);
        DelayQueue<TimerTaskList> queue = new DelayQueue<>();
        long startMs = 1000L;
        long tickMs = 10L;
        TimingWheel timingWheel = new TimingWheel(tickMs, 5, startMs, taskCounter, queue);

        // Create task within current time interval
        long expirationMs = startMs + tickMs * 2; // 1020ms
        TimerTask task = new TestTimerTask(tickMs * 2);
        TimerTaskEntry entry = new TimerTaskEntry(task, expirationMs);

        assertTrue(timingWheel.add(entry), "Should successfully add valid task");
        assertFalse(queue.isEmpty());
        assertEquals(1, taskCounter.get());
    }

    @Test
    public void testAddExpiredTask() {
        long startMs = 1000L;
        TimingWheel timingWheel = new TimingWheel(
            10L, 
            5, 
            startMs, 
            new AtomicInteger(0), 
            new DelayQueue<>()
        );

        long expirationMs = startMs - 1; // 999ms, less than current time
        TimerTask task = new TestTimerTask(-1);
        TimerTaskEntry entry = new TimerTaskEntry(task, expirationMs);

        assertFalse(timingWheel.add(entry), "Expired task should not be added");