diff --git a/wrapper/whitelistcb_test.go b/wrapper/whitelistcb_test.go new file mode 100644 index 0000000..86d01cd --- /dev/null +++ b/wrapper/whitelistcb_test.go @@ -0,0 +1,57 @@ +package wrapper + +import ( + "net" + "testing" + "unsafe" + + "github.com/stretchr/testify/assert" + "github.com/taosdata/driver-go/v3/wrapper/cgo" +) + +func TestWhitelistCallback_ErrorCode(t *testing.T) { + // Create a channel to receive the result + resultChan := make(chan *WhitelistResult, 1) + handle := cgo.NewHandle(resultChan) + // Simulate an error (code != 0) + go WhitelistCallback(handle.Pointer(), 1, nil, 0, nil) + + // Expect the result to have an error code + result := <-resultChan + assert.Equal(t, int32(1), result.ErrCode) + assert.Nil(t, result.IPNets) // No IPs should be returned +} + +func TestWhitelistCallback_Success(t *testing.T) { + // Prepare the test data: a list of byte slices representing IPs and masks + ipList := []byte{ + 192, 168, 1, 1, 24, // 192.168.1.1/24 + 0, 0, 0, + 10, 0, 0, 1, 16, // 10.0.0.1/16 + } + + // Create a channel to receive the result + resultChan := make(chan *WhitelistResult, 1) + + // Cast the byte slice to an unsafe pointer + pWhiteLists := unsafe.Pointer(&ipList[0]) + handle := cgo.NewHandle(resultChan) + // Simulate a successful callback (code == 0) + go WhitelistCallback(handle.Pointer(), 0, nil, 2, pWhiteLists) + + // Expect the result to have two IPNets + result := <-resultChan + assert.Equal(t, int32(0), result.ErrCode) + assert.Len(t, result.IPNets, 2) + + // Validate the first IPNet (192.168.1.1/24) + assert.Equal(t, net.IPv4(192, 168, 1, 1).To4(), result.IPNets[0].IP) + + ones, _ := result.IPNets[0].Mask.Size() + assert.Equal(t, 24, ones) + + // Validate the second IPNet (10.0.0.1/16) + assert.Equal(t, net.IPv4(10, 0, 0, 1).To4(), result.IPNets[1].IP) + ones, _ = result.IPNets[1].Mask.Size() + assert.Equal(t, 16, ones) +} diff --git a/ws/schemaless/schemaless_test.go b/ws/schemaless/schemaless_test.go index dc9caa6..63729ac 100644 --- a/ws/schemaless/schemaless_test.go +++ b/ws/schemaless/schemaless_test.go @@ -239,3 +239,33 @@ func TestSchemalessReconnect(t *testing.T) { err = s.Insert(data, InfluxDBLineProtocol, "ms", 0, 0) assert.NoError(t, err) } + +func TestWrongNewSchemaless(t *testing.T) { + s, err := NewSchemaless(NewConfig("://localhost:6041", 1, + SetUser("root"), + SetPassword("taosdata"), + )) + assert.Error(t, err) + assert.Nil(t, s) + + s, err = NewSchemaless(NewConfig("wrong://localhost:6041", 1, + SetUser("root"), + SetPassword("taosdata"), + )) + assert.Error(t, err) + assert.Nil(t, s) + + s, err = NewSchemaless(NewConfig("ws://localhost:6041", 1, + SetUser("root"), + SetPassword("wrongpassword"), + )) + assert.Error(t, err) + assert.Nil(t, s) + + s, err = NewSchemaless(NewConfig("ws://localhost:9999", 1, + SetUser("root"), + SetPassword("taosdata"), + )) + assert.Error(t, err) + assert.Nil(t, s) +} diff --git a/ws/stmt/stmt_test.go b/ws/stmt/stmt_test.go index 652766e..a9b04cf 100644 --- a/ws/stmt/stmt_test.go +++ b/ws/stmt/stmt_test.go @@ -764,6 +764,7 @@ func TestSTMTQuery(t *testing.T) { err = rows.Next(values) if err != nil { if err == io.EOF { + rows.Close() break } assert.NoError(t, err) @@ -936,6 +937,7 @@ func TestSTMTQuery(t *testing.T) { err = rows.Next(values) if err != nil { if err == io.EOF { + rows.Close() break } assert.NoError(t, err) diff --git a/ws/tmq/consumer.go b/ws/tmq/consumer.go index b7fbe4f..6ef55f7 100644 --- a/ws/tmq/consumer.go +++ b/ws/tmq/consumer.go @@ -65,7 +65,7 @@ type WSError struct { } func (e *WSError) Error() string { - return fmt.Sprintf("websocket close with error %s", e.err) + return fmt.Sprintf("websocket close with error %v", e.err) } // NewConsumer create a tmq consumer diff --git a/ws/tmq/consumer_test.go b/ws/tmq/consumer_test.go index 4ab7a98..2d1a401 100644 --- a/ws/tmq/consumer_test.go +++ b/ws/tmq/consumer_test.go @@ -731,6 +731,58 @@ func Test_configMapToConfigWrong(t *testing.T) { }, wantErr: "ws.message.writeWait cannot be less than 1 second", }, + { + name: "ws.autoReconnect", + args: args{ + m: &tmq.ConfigMap{ + "ws.url": "ws://127.0.0.1:6041", + "ws.autoReconnect": 123, + }, + }, + wantErr: "ws.autoReconnect expects type bool, not int", + }, + //ws.reconnectIntervalMs + { + name: "ws.reconnectIntervalMs", + args: args{ + m: &tmq.ConfigMap{ + "ws.url": "ws://127.0.0.1:6041", + "ws.reconnectIntervalMs": "not int", + }, + }, + wantErr: "ws.reconnectIntervalMs expects type int, not string", + }, + //ws.reconnectRetryCount + { + name: "ws.reconnectRetryCount", + args: args{ + m: &tmq.ConfigMap{ + "ws.url": "ws://127.0.0.1:6041", + "ws.reconnectRetryCount": "not int", + }, + }, + wantErr: "ws.reconnectRetryCount expects type int, not string", + }, + { + name: "session.timeout.ms", + args: args{ + m: &tmq.ConfigMap{ + "ws.url": "ws://127.0.0.1:6041", + "session.timeout.ms": 123, + }, + }, + wantErr: "session.timeout.ms expects type string, not int", + }, + { + name: "max.poll.interval.ms", + args: args{ + m: &tmq.ConfigMap{ + "ws.url": "ws://127.0.0.1:6041", + "max.poll.interval.ms": 123, + }, + }, + wantErr: "max.poll.interval.ms expects type string, not int", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -977,3 +1029,54 @@ func TestSubscribeReconnect(t *testing.T) { } assert.True(t, haveMessage) } + +func TestWSError_Error(t *testing.T) { + // Test scenario where an error is provided + expectedErr := errors.New("connection lost") + wsErr := &WSError{err: expectedErr} + + // Call the Error() method and check if the format is correct + actualError := wsErr.Error() + + // The expected error string format + expectedError := "websocket close with error connection lost" + + // Assert that the error string is formatted correctly + assert.Equal(t, expectedError, actualError, "Error string should match the expected format") + + // Test scenario where no error is provided (nil error) + wsErrNil := &WSError{} + actualErrorNil := wsErrNil.Error() + + // Expected format when error is nil (shouldn't panic) + expectedErrorNil := "websocket close with error " + + // Assert that the error string handles nil properly + assert.Equal(t, expectedErrorNil, actualErrorNil, "Error string should handle nil error correctly") +} + +func TestWrongConsumer(t *testing.T) { + consumer, err := NewConsumer(&tmq.ConfigMap{}) + assert.Error(t, err) + assert.Nil(t, consumer) + + consumer, err = NewConsumer(&tmq.ConfigMap{ + "ws.url": "ws://127.0.0.1:6041", + "auto.commit.interval.ms": "abc", + }) + assert.Error(t, err) + assert.Nil(t, consumer) + + consumer, err = NewConsumer(&tmq.ConfigMap{ + "ws.url": ":xxx", + }) + assert.Error(t, err) + assert.Nil(t, consumer) + + consumer, err = NewConsumer(&tmq.ConfigMap{ + "ws.url": "ws://127.0.0.1:9999", + }) + assert.Error(t, err) + assert.Nil(t, consumer) + +}