-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data): Make MaxEventSize a service configuration setting (#3891)
25000KB (25MB) by default close #3237 Signed-off-by: Ginny Guan <[email protected]>
- Loading branch information
1 parent
1cc235d
commit de3e46c
Showing
8 changed files
with
173 additions
and
35 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 was deleted.
Oops, something went wrong.
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,22 @@ | ||
// | ||
// Copyright (C) 2021-2022 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors" | ||
) | ||
|
||
func CheckPayloadSize(payload []byte, sizeLimit int64) errors.EdgeX { | ||
// Treat 0 as unlimit size | ||
if sizeLimit < 0 { | ||
return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintf("sizeLimit cannot be lower than 0, current sizeLimit: %d", sizeLimit), nil) | ||
} else if sizeLimit != 0 && int64(len(payload)) > sizeLimit { | ||
return errors.NewCommonEdgeX(errors.KindLimitExceeded, fmt.Sprintf("request size exceed %d KB", sizeLimit), nil) | ||
} | ||
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,39 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCheckPayloadSize(t *testing.T) { | ||
smallpayload := make([]byte, 10) | ||
largePayload := make([]byte, 25000000) | ||
tests := []struct { | ||
name string | ||
payload []byte | ||
sizeLimit int64 | ||
errorExpected bool | ||
expectedErrKind errors.ErrKind | ||
}{ | ||
{"Valid small size", smallpayload, int64(len(smallpayload)), false, ""}, | ||
{"Valid large size", largePayload, int64(len(largePayload)), false, ""}, | ||
{"Invalid small size", smallpayload, int64(len(smallpayload) - 1), true, errors.KindLimitExceeded}, | ||
{"Invalid large size", largePayload, int64(len(largePayload) - 1), true, errors.KindLimitExceeded}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
err := CheckPayloadSize(testCase.payload, testCase.sizeLimit) | ||
if testCase.errorExpected { | ||
require.Error(t, err) | ||
assert.NotEmpty(t, err.Error(), "Error message is empty") | ||
assert.Equal(t, testCase.expectedErrKind, errors.Kind(err), "Error kind not as expected") | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |