You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.9 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package client
import (
"testing"
"time"
"github.com/noahlann/nnet/pkg/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTCPClientNew(t *testing.T) {
config := &client.Config{
Addr: "tcp://127.0.0.1:0",
}
cli := NewTCPClient(config)
require.NotNil(t, cli, "Expected client to be non-nil")
}
func TestTCPClientNewWithNilConfig(t *testing.T) {
// NewTCPClient接受nil配置会使用默认配置
cli := NewTCPClient(nil)
require.NotNil(t, cli, "Expected client to be non-nil")
}
func TestTCPClientConnect(t *testing.T) {
t.Skip("Skipping connect test - requires actual server")
// 这个测试需要实际的服务器连接
// 可以放在集成测试中
}
func TestTCPClientIsConnected(t *testing.T) {
config := &client.Config{
Addr: "tcp://127.0.0.1:6995",
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
cli := NewTCPClient(config)
require.NotNil(t, cli, "Expected client to be non-nil")
// 测试初始连接状态
assert.False(t, cli.IsConnected(), "Expected client to not be connected initially")
}
func TestTCPClientWriteRead(t *testing.T) {
t.Skip("Skipping write/read test - requires actual server connection")
// 这个测试需要实际的服务器连接
// 可以放在集成测试中
}
func TestTCPClientClose(t *testing.T) {
config := &client.Config{
Addr: "tcp://127.0.0.1:6995",
}
cli := NewTCPClient(config)
require.NotNil(t, cli)
// 测试关闭未连接的客户端(应该不报错)
err := cli.Close()
require.NoError(t, err, "Expected no error when closing unconnected client")
// 验证客户端已关闭
assert.False(t, cli.IsConnected(), "Expected client to not be connected after close")
}
func TestTCPClientDisconnect(t *testing.T) {
config := &client.Config{
Addr: "tcp://127.0.0.1:6995",
}
cli := NewTCPClient(config)
require.NotNil(t, cli)
// 测试断开未连接的客户端(应该不报错)
err := cli.Disconnect()
require.NoError(t, err, "Expected no error when disconnecting unconnected client")
}
func TestTCPClientSendNotConnected(t *testing.T) {
config := &client.Config{
Addr: "tcp://127.0.0.1:6995",
}
cli := NewTCPClient(config)
require.NotNil(t, cli)
// 测试未连接时发送数据(应该返回错误)
err := cli.Send([]byte("test"))
assert.Error(t, err, "Expected error when sending without connection")
}
func TestTCPClientReceiveNotConnected(t *testing.T) {
config := &client.Config{
Addr: "tcp://127.0.0.1:6995",
}
cli := NewTCPClient(config)
require.NotNil(t, cli)
// 测试未连接时接收数据(应该返回错误)
_, err := cli.Receive()
assert.Error(t, err, "Expected error when receiving without connection")
}
func TestTCPClientReconnect(t *testing.T) {
t.Skip("Skipping reconnect test - requires actual server connection")
// 这个测试需要实际的服务器连接和重连逻辑
}