-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Gsantomaggio/raw_send
Raw send
- Loading branch information
Showing
25 changed files
with
588 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package internal | ||
|
||
import "bufio" | ||
|
||
type PublishRequest struct { | ||
publisherId uint8 | ||
messageCount uint32 | ||
messages []byte | ||
} | ||
|
||
func (p *PublishRequest) Write(writer *bufio.Writer) (int, error) { | ||
return writeMany(writer, p.publisherId, p.messageCount, p.messages) | ||
} | ||
|
||
func (p *PublishRequest) Key() uint16 { | ||
return CommandPublish | ||
} | ||
|
||
func (p *PublishRequest) SizeNeeded() int { | ||
return streamProtocolKeySizeBytes + | ||
streamProtocolVersionSizeBytes + | ||
streamProtocolKeySizeUint8 + // publisherId | ||
streamProtocolKeySizeUint32 + // messageCount | ||
len(p.messages) // messages | ||
} | ||
|
||
func (p *PublishRequest) Version() int16 { | ||
return Version1 | ||
} | ||
|
||
func NewPublishRequest(publisherId uint8, messageCount uint32, messages []byte) *PublishRequest { | ||
return &PublishRequest{publisherId: publisherId, messageCount: messageCount, messages: messages} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package internal | ||
|
||
import "bufio" | ||
|
||
// PublishConfirmResponse is a response that contains a publisher ID and a list of publishing IDs. | ||
// Publish commands return this type of response. It is used to confirm that the publishing was successful. | ||
// It is asynchronous and the response could contain a list of publishing IDs. | ||
type PublishConfirmResponse struct { | ||
publisherID uint8 // publisher id | ||
publishingIds []uint64 | ||
} | ||
|
||
func (p *PublishConfirmResponse) Read(reader *bufio.Reader) error { | ||
var publishingIdCount uint32 | ||
err := readMany(reader, &p.publisherID, &publishingIdCount) | ||
if err != nil { | ||
return err | ||
} | ||
p.publishingIds = make([]uint64, publishingIdCount) | ||
for i := uint32(0); i < publishingIdCount; i++ { | ||
err = readMany(reader, &p.publishingIds[i]) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Publish Confirm Response", func() { | ||
Context("Response", func() { | ||
It("decodes a binary sequence", func() { | ||
buff := new(bytes.Buffer) | ||
wr := bufio.NewWriter(buff) | ||
_, err := writeMany(wr, uint8(12), uint32(3), uint64(12345), uint64(12346), uint64(12347)) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(wr.Flush()).To(Succeed()) | ||
|
||
response := PublishConfirmResponse{} | ||
Expect(response.Read(bufio.NewReader(buff))).To(Succeed()) | ||
|
||
Expect(response.publisherID).To(BeNumerically("==", 12)) | ||
Expect(len(response.publishingIds)).To(BeNumerically("==", 3)) | ||
Expect(response.publishingIds).To(HaveLen(3)) | ||
Expect(response.publishingIds[0]).To(BeNumerically("==", 12345)) | ||
Expect(response.publishingIds[1]).To(BeNumerically("==", 12346)) | ||
Expect(response.publishingIds[2]).To(BeNumerically("==", 12347)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"github.com/gsantomaggio/rabbitmq-stream-go-client/pkg/common" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Create", func() { | ||
Context("Request", func() { | ||
It("returns the expected attributes", func() { | ||
seq := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64} // publishingId sequence number 100 | ||
seq = append(seq, []byte{0x00, 0x00, 0x00, 0x03}...) //message size | ||
seq = append(seq, []byte("foo")...) // message body | ||
|
||
createRequest := NewPublishRequest(17, 1, seq) | ||
Expect(createRequest.Key()).To(BeNumerically("==", 0x0002)) | ||
Expect(createRequest.Version()).To(BeNumerically("==", 1)) | ||
|
||
expectedSize := streamProtocolKeySizeBytes + | ||
streamProtocolVersionSizeBytes + | ||
streamProtocolKeySizeUint8 + // publisherId | ||
streamProtocolKeySizeUint32 + // message count | ||
+len(seq) // message body | ||
|
||
Expect(createRequest.SizeNeeded()).To(BeNumerically("==", expectedSize)) | ||
}) | ||
|
||
It("encodes itself into a binary sequence", func() { | ||
seq := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64} // publishingId sequence number 100 | ||
seq = append(seq, []byte{0x00, 0x00, 0x00, 0x03}...) //message size | ||
seq = append(seq, []byte("foo")...) // message body | ||
|
||
c := NewPublishRequest(17, 1, seq) | ||
buff := new(bytes.Buffer) | ||
wr := bufio.NewWriter(buff) | ||
Expect(c.Write(wr)).To(BeNumerically("==", c.SizeNeeded()-streamProtocolHeaderSizeBytes)) | ||
Expect(wr.Flush()).To(Succeed()) | ||
|
||
expectedByteSequence := []byte{ | ||
0x11, // publisher ID 17 | ||
0x00, 0x00, 0x00, 0x01, // message count 1 | ||
|
||
} | ||
expectedByteSequence = append(expectedByteSequence, seq...) | ||
Expect(buff.Bytes()).To(Equal(expectedByteSequence)) | ||
}) | ||
|
||
It("encodes fake message", func() { | ||
buffFakeMessages := new(bytes.Buffer) | ||
writerFakeMessages := bufio.NewWriter(buffFakeMessages) | ||
|
||
// we simulate a message aggregation | ||
// that can be done by the client | ||
// First loop to aggregate the messages | ||
var fakeMessages []common.StreamerMessage | ||
fakeMessages = append(fakeMessages, NewFakeMessage(17, []byte("foo"))) | ||
fakeMessages = append(fakeMessages, NewFakeMessage(21, []byte("wine"))) | ||
fakeMessages = append(fakeMessages, NewFakeMessage(23, []byte("beer"))) | ||
fakeMessages = append(fakeMessages, NewFakeMessage(29, []byte("water"))) | ||
|
||
// It is time to prepare the buffer to send to the server | ||
for _, fakeMessage := range fakeMessages { | ||
Expect(fakeMessage.WriteTo(writerFakeMessages)).To(BeNumerically("==", 8+4+len(fakeMessage.Body()))) | ||
} | ||
Expect(writerFakeMessages.Flush()).To(Succeed()) | ||
|
||
c := NewPublishRequest(200, uint32(len(fakeMessages)), buffFakeMessages.Bytes()) | ||
buff := new(bytes.Buffer) | ||
wr := bufio.NewWriter(buff) | ||
Expect(c.Write(wr)).To(BeNumerically("==", c.SizeNeeded()-streamProtocolHeaderSizeBytes)) | ||
Expect(wr.Flush()).To(Succeed()) | ||
|
||
}) | ||
|
||
}) | ||
|
||
Context("Response", func() { | ||
It("decodes a binary sequence", func() { | ||
buff := new(bytes.Buffer) | ||
wr := bufio.NewWriter(buff) | ||
_, err := writeMany(wr, uint32(12345), uint16(101)) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(wr.Flush()).To(Succeed()) | ||
|
||
response := SimpleResponse{} | ||
Expect(response.Read(bufio.NewReader(buff))).To(Succeed()) | ||
|
||
Expect(response.correlationId).To(BeNumerically("==", 12345)) | ||
Expect(response.responseCode).To(BeNumerically("==", 101)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.