forked from TrueCloudLab/frostfs-api-go
[#253] container: Implement marshalers on UsedSpaceAnnouncement structure
Implement Unmarshal method on UsedSpaceAnnouncement v2 message. Implement Marshal/Unmarshal methods on cross-version UsedSpaceAnnouncement type. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
9e01f29dda
commit
93e1580684
4 changed files with 85 additions and 10 deletions
|
@ -55,3 +55,23 @@ func (a *UsedSpaceAnnouncement) SetUsedSpace(value uint64) {
|
|||
func (a *UsedSpaceAnnouncement) ToV2() *container.UsedSpaceAnnouncement {
|
||||
return (*container.UsedSpaceAnnouncement)(a)
|
||||
}
|
||||
|
||||
// Marshal marshals UsedSpaceAnnouncement into a protobuf binary form.
|
||||
//
|
||||
// Buffer is allocated when the argument is empty.
|
||||
// Otherwise, the first buffer is used.
|
||||
func (a *UsedSpaceAnnouncement) Marshal(b ...[]byte) ([]byte, error) {
|
||||
var buf []byte
|
||||
if len(b) > 0 {
|
||||
buf = b[0]
|
||||
}
|
||||
|
||||
return a.ToV2().
|
||||
StableMarshal(buf)
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals protobuf binary representation of UsedSpaceAnnouncement.
|
||||
func (a *UsedSpaceAnnouncement) Unmarshal(data []byte) error {
|
||||
return a.ToV2().
|
||||
Unmarshal(data)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package container_test
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||
|
@ -47,3 +48,24 @@ func TestAnnouncement(t *testing.T) {
|
|||
require.Equal(t, container.NewIDFromV2(newCID), newA.ContainerID())
|
||||
})
|
||||
}
|
||||
|
||||
func TestUsedSpaceEncoding(t *testing.T) {
|
||||
a := container.NewAnnouncement()
|
||||
a.SetUsedSpace(13)
|
||||
a.SetEpoch(666)
|
||||
|
||||
id := container.NewID()
|
||||
id.SetSHA256([sha256.Size]byte{1, 2, 3})
|
||||
|
||||
a.SetContainerID(id)
|
||||
|
||||
t.Run("binary", func(t *testing.T) {
|
||||
data, err := a.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
a2 := container.NewAnnouncement()
|
||||
require.NoError(t, a2.Unmarshal(data))
|
||||
|
||||
require.Equal(t, a, a2)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -589,6 +589,17 @@ func (a *UsedSpaceAnnouncement) StableSize() (size int) {
|
|||
return size
|
||||
}
|
||||
|
||||
func (a *UsedSpaceAnnouncement) Unmarshal(data []byte) error {
|
||||
m := new(container.AnnounceUsedSpaceRequest_Body_Announcement)
|
||||
if err := proto.Unmarshal(data, m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*a = *UsedSpaceAnnouncementFromGRPCMessage(m)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AnnounceUsedSpaceRequestBody) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if r == nil {
|
||||
return []byte{}, nil
|
||||
|
|
|
@ -234,6 +234,22 @@ func TestGetEACLResponseBody_StableMarshal(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestUsedSpaceAnnouncement_StableMarshal(t *testing.T) {
|
||||
from := generateAnnouncement()
|
||||
transport := new(grpc.AnnounceUsedSpaceRequest_Body_Announcement)
|
||||
|
||||
t.Run("non empty", func(t *testing.T) {
|
||||
wire, err := from.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = goproto.Unmarshal(wire, transport)
|
||||
require.NoError(t, err)
|
||||
|
||||
to := container.UsedSpaceAnnouncementFromGRPCMessage(transport)
|
||||
require.Equal(t, from, to)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAnnounceUsedSpaceRequestBody_StableMarshal(t *testing.T) {
|
||||
requestFrom := generateAnnounceRequestBody(10)
|
||||
transport := new(grpc.AnnounceUsedSpaceRequest_Body)
|
||||
|
@ -443,21 +459,27 @@ func generateGetEACLResponseBody(n int, k, v string) *container.GetExtendedACLRe
|
|||
return resp
|
||||
}
|
||||
|
||||
func generateAnnouncement() *container.UsedSpaceAnnouncement {
|
||||
buf := make([]byte, sha256.Size)
|
||||
rand.Read(buf)
|
||||
|
||||
cid := new(refs.ContainerID)
|
||||
cid.SetValue(buf)
|
||||
|
||||
a := new(container.UsedSpaceAnnouncement)
|
||||
a.SetEpoch(rand.Uint64())
|
||||
a.SetContainerID(cid)
|
||||
a.SetUsedSpace(rand.Uint64())
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func generateAnnounceRequestBody(n int) *container.AnnounceUsedSpaceRequestBody {
|
||||
resp := new(container.AnnounceUsedSpaceRequestBody)
|
||||
buf := make([]byte, sha256.Size)
|
||||
|
||||
announcements := make([]*container.UsedSpaceAnnouncement, 0, n)
|
||||
for i := 0; i < n; i++ {
|
||||
rand.Read(buf)
|
||||
|
||||
cid := new(refs.ContainerID)
|
||||
cid.SetValue(buf)
|
||||
|
||||
a := new(container.UsedSpaceAnnouncement)
|
||||
a.SetEpoch(rand.Uint64())
|
||||
a.SetContainerID(cid)
|
||||
a.SetUsedSpace(rand.Uint64())
|
||||
announcements = append(announcements, generateAnnouncement())
|
||||
}
|
||||
|
||||
resp.SetAnnouncements(announcements)
|
||||
|
|
Loading…
Reference in a new issue