/*
 * Copyright 2021 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 invoke

import (
	"context"
	"testing"

	"github.com/cloudwego/kitex/internal/test"
)

func Test_ivkConnExtension_NewWriteByteBuffer(t *testing.T) {
	msg := NewMessage(nil, nil)
	msg.GetRequestReaderByteBuffer()
	ext := newIvkConnExtension()
	test.Assert(t, ext.NewWriteByteBuffer(context.Background(), msg, nil) != nil)
}

func Test_ivkConnExtension_NewReadByteBuffer(t *testing.T) {
	msg := NewMessage(nil, nil)
	msg.GetRequestReaderByteBuffer()
	ext := newIvkConnExtension()
	test.Assert(t, ext.NewReadByteBuffer(context.Background(), msg, nil) == nil)
	msg.SetRequestBytes([]byte{})
	test.Assert(t, ext.NewReadByteBuffer(context.Background(), msg, nil) != nil)
}

func Test_ivkConnExtension_ReleaseBuffer(t *testing.T) {
	msg := NewMessage(nil, nil)
	msg.GetRequestReaderByteBuffer()
	ext := newIvkConnExtension()
	bufWriter := ext.NewWriteByteBuffer(context.Background(), msg, nil)
	bufWriter.WriteString("any")
	bufWriter.Flush()
	ext.ReleaseBuffer(bufWriter, nil)
	b, err := msg.GetResponseBytes()
	if err != nil {
		t.Fatal(err)
	}
	test.Assert(t, string(b) == "any")
}
