git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/object/transformer/size_hint_test.go (about) 1 package transformer 2 3 import ( 4 "context" 5 "crypto/rand" 6 "math" 7 "testing" 8 9 cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test" 10 objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" 11 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user" 12 "github.com/nspcc-dev/neo-go/pkg/crypto/keys" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestTransformerSizeHintCorrectness(t *testing.T) { 17 const ( 18 maxSize = 100 19 payloadSize = maxSize*2 + maxSize/2 20 ) 21 22 pk, err := keys.NewPrivateKey() 23 require.NoError(t, err) 24 25 p := Params{ 26 Key: &pk.PrivateKey, 27 NetworkState: dummyEpochSource(123), 28 MaxSize: maxSize, 29 WithoutHomomorphicHash: true, 30 } 31 32 cnr := cidtest.ID() 33 hdr := newObject(cnr) 34 35 var owner user.ID 36 user.IDFromKey(&owner, pk.PrivateKey.PublicKey) 37 hdr.SetOwnerID(owner) 38 39 expected := make([]byte, payloadSize) 40 _, _ = rand.Read(expected) 41 42 t.Run("default", func(t *testing.T) { 43 p.SizeHint = 0 44 testPayloadEqual(t, p, hdr, expected) 45 }) 46 t.Run("size hint is perfect", func(t *testing.T) { 47 p.SizeHint = payloadSize 48 testPayloadEqual(t, p, hdr, expected) 49 }) 50 t.Run("size hint < payload size", func(t *testing.T) { 51 p.SizeHint = payloadSize / 2 52 testPayloadEqual(t, p, hdr, expected) 53 }) 54 t.Run("size hint > payload size", func(t *testing.T) { 55 p.SizeHint = math.MaxUint64 56 testPayloadEqual(t, p, hdr, expected) 57 }) 58 } 59 60 func testPayloadEqual(t *testing.T, p Params, hdr *objectSDK.Object, expected []byte) { 61 tt := new(testTarget) 62 63 p.NextTargetInit = func() ObjectWriter { return tt } 64 target := NewPayloadSizeLimiter(p) 65 66 writeObject(t, context.Background(), target, hdr, expected) 67 var actual []byte 68 for i := range tt.objects { 69 actual = append(actual, tt.objects[i].Payload()...) 70 } 71 require.Equal(t, expected, actual) 72 }