/*
 * 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 (
	"bytes"
	"net"
	"testing"

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

func Test_message_Addr(t *testing.T) {
	local := &net.TCPAddr{}
	remote := &net.TCPAddr{}
	msg := NewMessage(local, remote)
	test.Assert(t, msg.LocalAddr() == local)
	test.Assert(t, msg.RemoteAddr() == remote)
}

func Test_message_Request(t *testing.T) {
	msg := NewMessage(nil, nil)
	want := []byte("anything")
	err := msg.SetRequestBytes(want)
	if err != nil {
		t.Fatal(err)
	}
	rb := msg.GetRequestReaderByteBuffer()
	test.Assert(t, rb != nil)
	got := make([]byte, len(want))
	_, err = rb.ReadBinary(got)
	if err != nil {
		t.Fatal(err)
	}
	test.Assert(t, bytes.Equal(want, got))
}

func Test_message_Response(t *testing.T) {
	msg := NewMessage(nil, nil)
	want := []byte("anything")
	rb := msg.GetResponseWriterByteBuffer()
	rb.WriteBinary(want)
	rb.Flush()
	got, err := msg.GetResponseBytes()
	if err != nil {
		t.Fatal(err)
	}
	test.Assert(t, bytes.Equal(want, got))
}

func Test_message_Release(t *testing.T) {
	msg := NewMessage(nil, nil)
	any := []byte("anything")
	msg.SetRequestBytes(any)
	rb := msg.GetRequestReaderByteBuffer()
	msg.Release()
	_, err := rb.Write(any)
	test.Assert(t, err != nil)
	msg = NewMessage(nil, nil)
	rb = msg.GetResponseWriterByteBuffer()
	msg.Release()
	_, err = rb.Write(any)
	test.Assert(t, err != nil)
}
