-
Notifications
You must be signed in to change notification settings - Fork 20
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 #43 from Gsantomaggio/query_offset
Implement query offset command
- Loading branch information
Showing
9 changed files
with
271 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
) | ||
|
||
// QueryOffsetRequest is used to query the offset of a stream for a given consumer reference. | ||
// ref: https://github.com/rabbitmq/rabbitmq-server/blob/main/deps/rabbitmq_stream/docs/PROTOCOL.adoc#queryoffset | ||
type QueryOffsetRequest struct { | ||
correlationId uint32 | ||
consumerReference string // max 256 characters | ||
stream string | ||
} | ||
|
||
func (c *QueryOffsetRequest) Stream() string { | ||
return c.stream | ||
} | ||
|
||
func (c *QueryOffsetRequest) ConsumerReference() string { | ||
return c.consumerReference | ||
} | ||
|
||
func (c *QueryOffsetRequest) UnmarshalBinary(data []byte) error { | ||
buff := bytes.NewReader(data) | ||
rd := bufio.NewReader(buff) | ||
return readMany(rd, &c.correlationId, &c.consumerReference, &c.stream) | ||
} | ||
|
||
func NewQueryOffsetRequest(consumerReference string, stream string) *QueryOffsetRequest { | ||
return &QueryOffsetRequest{consumerReference: consumerReference, stream: stream} | ||
} | ||
|
||
func (c *QueryOffsetRequest) Write(writer *bufio.Writer) (int, error) { | ||
return writeMany(writer, c.correlationId, c.consumerReference, c.stream) | ||
} | ||
|
||
func (c *QueryOffsetRequest) Key() uint16 { | ||
return CommandQueryOffset | ||
} | ||
|
||
func (c *QueryOffsetRequest) SizeNeeded() int { | ||
return streamProtocolHeader + | ||
streamProtocolStringLenSizeBytes + len(c.consumerReference) + // consumer reference | ||
streamProtocolStringLenSizeBytes + len(c.stream) // stream | ||
} | ||
|
||
func (c *QueryOffsetRequest) SetCorrelationId(id uint32) { | ||
c.correlationId = id | ||
} | ||
|
||
func (c *QueryOffsetRequest) CorrelationId() uint32 { | ||
return c.correlationId | ||
} | ||
|
||
func (c *QueryOffsetRequest) Version() int16 { | ||
return Version1 | ||
} | ||
|
||
type QueryOffsetResponse struct { | ||
correlationId uint32 | ||
responseCode uint16 | ||
offset uint64 | ||
} | ||
|
||
func (c *QueryOffsetResponse) UnmarshalBinary(data []byte) error { | ||
buff := bytes.NewReader(data) | ||
rd := bufio.NewReader(buff) | ||
return readMany(rd, &c.correlationId, &c.responseCode, &c.offset) | ||
} | ||
|
||
func (c *QueryOffsetResponse) Read(reader *bufio.Reader) error { | ||
return readMany(reader, &c.correlationId, &c.responseCode, &c.offset) | ||
} | ||
|
||
func (c *QueryOffsetResponse) CorrelationId() uint32 { | ||
return c.correlationId | ||
} | ||
|
||
func (c *QueryOffsetResponse) ResponseCode() uint16 { | ||
return c.responseCode | ||
} | ||
|
||
func (c *QueryOffsetResponse) Offset() uint64 { | ||
return c.offset | ||
} | ||
|
||
func (c *QueryOffsetResponse) MarshalBinary() ([]byte, error) { | ||
var buff bytes.Buffer | ||
wr := bufio.NewWriter(&buff) | ||
_, err := writeMany(wr, c.correlationId, c.responseCode, c.offset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err = wr.Flush(); err != nil { | ||
return nil, err | ||
} | ||
return buff.Bytes(), nil | ||
} | ||
|
||
func NewQueryOffsetResponseWith(correlationId uint32, responseCode uint16, offset uint64) *QueryOffsetResponse { | ||
return &QueryOffsetResponse{correlationId: correlationId, responseCode: responseCode, offset: offset} | ||
} | ||
|
||
func NewQueryOffsetResponse() *QueryOffsetResponse { | ||
return &QueryOffsetResponse{} | ||
} |
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,71 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("QueryOffset", func() { | ||
Describe("QueryOffsetRequest", func() { | ||
|
||
It("returns the size required to encode the frame", func() { | ||
queryOffsetRequest := NewQueryOffsetRequest("my_c", "my_stream") | ||
|
||
expectedSize := 2 + 2 + 4 + // key ID + version + correlation ID | ||
2 + 4 + // uint16 for the consumerReference string + uint32 consumerReference string length | ||
2 + 9 // uint16 for the stream string + uint32 stream string length | ||
Expect(queryOffsetRequest.SizeNeeded()).To(Equal(expectedSize)) | ||
}) | ||
|
||
It("has the expected fields", func() { | ||
queryOffsetRequest := NewQueryOffsetRequest("my_c", "my_stream") | ||
queryOffsetRequest.SetCorrelationId(123) | ||
Expect(queryOffsetRequest.Key()).To(BeNumerically("==", 0x000b)) | ||
Expect(queryOffsetRequest.Stream()).To(Equal("my_stream")) | ||
Expect(queryOffsetRequest.ConsumerReference()).To(Equal("my_c")) | ||
Expect(queryOffsetRequest.CorrelationId()).To(BeNumerically("==", 123)) | ||
|
||
}) | ||
|
||
It("binary encodes the struct into a binary sequence", func() { | ||
queryOffsetRequest := NewQueryOffsetRequest("my_c", "my_stream") | ||
queryOffsetRequest.SetCorrelationId(123) | ||
buff := new(bytes.Buffer) | ||
wr := bufio.NewWriter(buff) | ||
|
||
bytesWritten, err := queryOffsetRequest.Write(wr) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(wr.Flush()).To(Succeed()) | ||
|
||
Expect(bytesWritten).To(BeNumerically("==", queryOffsetRequest.SizeNeeded()-streamProtocolHeaderSizeBytes)) | ||
|
||
expectedByteSequence := []byte{0x00, 0x00, 0x00, 123, // correlation id | ||
0x00, 0x04, byte('m'), byte('y'), byte('_'), byte('c'), // consumerReference len + consumerReference string | ||
0x00, 0x09, byte('m'), byte('y'), byte('_'), byte('s'), byte('t'), byte('r'), byte('e'), byte('a'), byte('m'), // stream len + stream string | ||
} | ||
Expect(buff.Bytes()).To(Equal(expectedByteSequence)) | ||
}) | ||
|
||
}) | ||
|
||
Describe("QueryOffsetResponse", func() { | ||
It("decodes a binary sequence into a struct", func() { | ||
someResponse := NewQueryOffsetResponse() | ||
binaryQueryOffset := []byte{ | ||
0x00, 0x00, 0x00, 0xFF, // correlation id | ||
0x00, 0x0a, // response code | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, // offset | ||
} | ||
|
||
buff := bytes.NewBuffer(binaryQueryOffset) | ||
reader := bufio.NewReader(buff) | ||
Expect(someResponse.Read(reader)).To(Succeed()) | ||
|
||
Expect(someResponse.CorrelationId()).To(BeNumerically("==", 0xFF)) | ||
Expect(someResponse.ResponseCode()).To(BeNumerically("==", 0x0a)) | ||
Expect(someResponse.Offset()).To(BeNumerically("==", 7)) | ||
}) | ||
}) | ||
}) |
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