Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huskar-t committed Nov 5, 2024
1 parent ff31594 commit 575a092
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 1 deletion.
57 changes: 57 additions & 0 deletions wrapper/whitelistcb_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
30 changes: 30 additions & 0 deletions ws/schemaless/schemaless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions ws/stmt/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ws/tmq/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions ws/tmq/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 <nil>"

// 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)

}

0 comments on commit 575a092

Please sign in to comment.