Skip to content

Commit

Permalink
Fixed bug discussed in #52 (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Chauvin <[email protected]>

Signed-off-by: Jeff Chauvin <[email protected]>
  • Loading branch information
Christopher-Wolf-ibm authored Sep 26, 2022
1 parent 321265c commit fb3ab47
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
20 changes: 17 additions & 3 deletions fluent/client/ws_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SOFTWARE.
package client

import (
"bytes"
"crypto/tls"
"errors"
"net/http"
Expand Down Expand Up @@ -265,10 +266,14 @@ func (c *WSClient) Reconnect() (err error) {

// Send sends a single msgp.Encodable across the wire.
func (c *WSClient) Send(e protocol.ChunkEncoder) error {
var (
err error
rawMessageData bytes.Buffer
)
// Check for an async connection error and return it here.
// In most cases, the client will not care about reading from
// the connection, so checking for the error here is sufficient.
if err := c.getErr(); err != nil {
if err = c.getErr(); err != nil {
return err // TODO: wrap this
}

Expand All @@ -278,8 +283,17 @@ func (c *WSClient) Send(e protocol.ChunkEncoder) error {
return errors.New("no active session")
}

// msgp.Encode makes use of object pool to decrease allocations
return msgp.Encode(session.Connection, e)
err = msgp.Encode(&rawMessageData, e)
if err != nil {
return err
}

bytesData := rawMessageData.Bytes()
// Write function does not accurately return the number of bytes written
// so it would be ineffective to compare
_, err = c.session.Connection.Write(bytesData)

return err
}

// SendRaw sends an array of bytes across the wire.
Expand Down
40 changes: 40 additions & 0 deletions fluent/client/ws_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"bytes"
"crypto/tls"
"errors"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -46,6 +47,7 @@ import (
"github.com/gorilla/websocket"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/tinylib/msgp/msgp"
)

var _ = Describe("IAMAuthInfo", func() {
Expand Down Expand Up @@ -303,6 +305,44 @@ var _ = Describe("WSClient", func() {
Expect(bytes.Equal(bits, writtenbits)).To(BeTrue())
})

It("Sends the message", func() {
msgBytes, _ := msg.MarshalMsg(nil)
Expect(client.Send(&msg)).ToNot(HaveOccurred())

writtenBytes := conn.WriteArgsForCall(0)
Expect(bytes.Equal(msgBytes, writtenBytes)).To(BeTrue())
})

When("The message is large", func() {
const charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

var (
expectedBytes int
messageSize = 65536
)

JustBeforeEach(func() {
seededRand := rand.New(
rand.NewSource(time.Now().UnixNano()))
m := make([]byte, messageSize)
for i := range m {
m[i] = charset[seededRand.Intn(len(charset))]
}
msg.Record = m

var b bytes.Buffer
Expect(msgp.Encode(&b, &msg)).ToNot(HaveOccurred())
expectedBytes = len(b.Bytes())
})

It("Sends the correct number of bits", func() {
Expect(client.Send(&msg)).ToNot(HaveOccurred())
Expect(conn.WriteCallCount()).To(Equal(1))
writtenBytes := len(conn.WriteArgsForCall(0))
Expect(writtenBytes).To(Equal(expectedBytes))
})
})

When("the connection is disconnected", func() {
JustBeforeEach(func() {
err := client.Disconnect()
Expand Down

0 comments on commit fb3ab47

Please sign in to comment.