-
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 #37 from Gsantomaggio/credits
Implement Credit
- Loading branch information
Showing
8 changed files
with
270 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,80 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
) | ||
|
||
type CreditRequest struct { | ||
subscriptionId uint8 | ||
// number of chunks that can be sent | ||
credit uint16 | ||
} | ||
|
||
func NewCreditRequest(subscriptionId uint8, credit uint16) *CreditRequest { | ||
return &CreditRequest{subscriptionId: subscriptionId, credit: credit} | ||
} | ||
|
||
func (c *CreditRequest) UnmarshalBinary(data []byte) error { | ||
return readMany(bytes.NewReader(data), &c.subscriptionId, &c.credit) | ||
} | ||
|
||
func (c *CreditRequest) SubscriptionId() uint8 { | ||
return c.subscriptionId | ||
} | ||
|
||
func (c *CreditRequest) Credit() uint16 { | ||
return c.credit | ||
} | ||
|
||
func (c *CreditRequest) Write(w *bufio.Writer) (int, error) { | ||
return writeMany(w, c.subscriptionId, c.credit) | ||
} | ||
|
||
func (c *CreditRequest) Key() uint16 { | ||
return CommandCredit | ||
} | ||
|
||
func (c *CreditRequest) SizeNeeded() int { | ||
return streamProtocolHeaderSizeBytes + streamProtocolKeySizeUint8 + streamProtocolKeySizeUint16 | ||
} | ||
|
||
func (c *CreditRequest) Version() int16 { | ||
return Version1 | ||
} | ||
|
||
type CreditResponse struct { | ||
responseCode uint16 | ||
subscriptionId uint8 | ||
} | ||
|
||
func (c *CreditResponse) ResponseCode() uint16 { | ||
return c.responseCode | ||
} | ||
|
||
func (c *CreditResponse) SubscriptionId() uint8 { | ||
return c.subscriptionId | ||
} | ||
|
||
func (c *CreditResponse) MarshalBinary() (data []byte, err error) { | ||
w := &bytes.Buffer{} | ||
n, err := writeMany(w, c.responseCode, c.subscriptionId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if n != 3 { | ||
return nil, errWriteShort | ||
} | ||
|
||
data = w.Bytes() | ||
return | ||
} | ||
|
||
func NewCreditResponse(responseCode uint16, subscriptionId uint8) *CreditResponse { | ||
return &CreditResponse{responseCode: responseCode, subscriptionId: subscriptionId} | ||
} | ||
|
||
func (c *CreditResponse) Read(r *bufio.Reader) error { | ||
return readMany(r, &c.responseCode, &c.subscriptionId) | ||
} |
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,54 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Internal/Credit", func() { | ||
Context("Request", func() { | ||
It("knows the size needed to encode itself", func() { | ||
c := &CreditRequest{ | ||
subscriptionId: 123, | ||
credit: 987, | ||
} | ||
Expect(c.SizeNeeded()).To(BeNumerically("==", streamProtocolHeaderSizeBytes+1+2)) | ||
}) | ||
|
||
It("encodes itself into a binary sequence", func() { | ||
c := &CreditRequest{ | ||
subscriptionId: 5, | ||
credit: 255, | ||
} | ||
buff := &bytes.Buffer{} | ||
wr := bufio.NewWriter(buff) | ||
Expect(c.Write(wr)).To(BeNumerically("==", 3)) | ||
Expect(wr.Flush()).To(Succeed()) | ||
|
||
expectedByteSequence := []byte{ | ||
0x05, // subscription ID | ||
0x00, 0xff, // credit | ||
} | ||
Expect(buff.Bytes()).To(Equal(expectedByteSequence)) | ||
}) | ||
}) | ||
|
||
Context("Response", func() { | ||
It("decodes itself into a response struct", func() { | ||
byteSequence := []byte{ | ||
0x00, 0x0f, // response code | ||
0x10, // sub ID | ||
} | ||
|
||
buff := bytes.NewBuffer(byteSequence) | ||
r := bufio.NewReader(buff) | ||
c := &CreditResponse{} | ||
|
||
Expect(c.Read(r)).Should(Succeed()) | ||
Expect(c.responseCode).To(BeNumerically("==", 15)) | ||
Expect(c.subscriptionId).To(BeNumerically("==", 16)) | ||
}) | ||
}) | ||
}) |
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