/*
 * Copyright 2026 The gRPC Authors
 *
 * Licensed 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 io.grpc.servlet;

import static org.junit.Assert.assertEquals;

import io.grpc.servlet.AsyncServletOutputStreamWriter.Log;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class AsyncServletOutputStreamWriterTest {

  @Test
  public void backToBackWrites_remainReady() throws IOException {
    AtomicInteger writes = new AtomicInteger();
    // A writer where isReady always returns true
    AsyncServletOutputStreamWriter writer = new AsyncServletOutputStreamWriter(
        (bytes, len) -> () -> writes.incrementAndGet(),
        () -> {}, // flushAction
        () -> {}, // completeAction
        () -> true, // isReady
        new Log() {}
    );

    // 1. Initial onWritePossible call to set internal state 'readyAndDrained' to true.
    writer.onWritePossible();

    // 2. First write: Should execute directly.
    // In the new state machine, this MUST set 'readyAndDrained' to false
    // to satisfy Tomcat's requirement for a callback between writes.
    writer.writeBytes(new byte[1], 1);
    assertEquals("First write should execute directly", 1, writes.get());

    // 3. Second write: Should be BUFFERED.
    // Even though isReady is true, 'readyAndDrained' is false, so we wait.
    writer.writeBytes(new byte[1], 1);
    assertEquals("Second write should be buffered, not executed yet", 1, writes.get());

    // 4. Container calls onWritePossible.
    // This should drain the queue (executing the 2nd write) and set 'readyAndDrained' to true.
    writer.onWritePossible();
    assertEquals("Second write should execute after onWritePossible", 2, writes.get());
    
    // 5. Third write: Should execute directly again.
    writer.writeBytes(new byte[1], 1);
    assertEquals("Third write should execute directly", 3, writes.get());
  }

  @Test
  public void writeThenFlush_remainReady() throws IOException {
    AtomicInteger writes = new AtomicInteger();
    AtomicInteger flushes = new AtomicInteger();
    AsyncServletOutputStreamWriter writer = new AsyncServletOutputStreamWriter(
        (bytes, len) -> () -> writes.incrementAndGet(),
        () -> flushes.incrementAndGet(),
        () -> {},
        () -> true,
        new Log() {}
    );

    writer.onWritePossible();

    // writeBytes sets readyAndDrained = false
    writer.writeBytes(new byte[1], 1);
    
    // flush should now be buffered because readyAndDrained is false
    writer.flush();
    assertEquals(1, writes.get());
    assertEquals(0, flushes.get());

    // Drain
    writer.onWritePossible();
    assertEquals(1, flushes.get());
  }
}
