/*
 * Copyright 2024 CloudWeGo 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 grpc

import (
	"context"
	"errors"
	"math"
	"strings"
	"testing"

	"github.com/cloudwego/kitex/internal/test"
	"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/codes"
)

func TestGracefulShutdown(t *testing.T) {
	onGoAwayCh := make(chan struct{})
	srv, cli := setUpWithOnGoAway(t, 10000, &ServerConfig{MaxStreams: math.MaxUint32}, gracefulShutdown, ConnectOptions{}, func(reason GoAwayReason) {
		close(onGoAwayCh)
	})
	defer cli.Close(errSelfCloseForTest)

	stream, err := cli.NewStream(context.Background(), &CallHdr{})
	test.Assert(t, err == nil, err)
	<-srv.srvReady
	finishCh := make(chan struct{})
	go srv.gracefulShutdown(finishCh)
	err = cli.Write(stream, nil, []byte("hello"), &Options{})
	test.Assert(t, err == nil, err)
	msg := make([]byte, 5)
	num, err := stream.Read(msg)
	test.Assert(t, err == nil, err)
	test.Assert(t, num == 5, num)
	// waiting onGoAway triggered
	<-onGoAwayCh
	// this transport could not create Stream anymore
	_, err = cli.NewStream(context.Background(), &CallHdr{})
	test.Assert(t, errors.Is(err, errStreamDrain), err)
	// wait for the server transport to be closed
	<-finishCh
	_, err = stream.Read(msg)
	test.Assert(t, err != nil, err)
	st := stream.Status()
	test.Assert(t, strings.Contains(st.Message(), gracefulShutdownMsg), st.Message())
	test.Assert(t, st.Code() == codes.Unavailable, st.Code())
}
