Issue-ID: NONRTRIC-702
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: I3fb3467165a159941ba121e37e4938b80d12e3a9
}
func getAsJSONArray(rawMsgs [][]byte) []byte {
}
func getAsJSONArray(rawMsgs [][]byte) []byte {
+ if len(rawMsgs) == 0 {
+ return []byte("")
+ }
+ strings := ""
for i := 0; i < len(rawMsgs); i++ {
for i := 0; i < len(rawMsgs); i++ {
- msg := string(rawMsgs[i])
- json = json + strings.ReplaceAll(msg, "\"", "\\\"")
- if i < len(rawMsgs)-1 {
- json = json + ","
- }
+ strings = strings + makeIntoString(rawMsgs[i])
+ strings = addSeparatorIfNeeded(strings, i, len(rawMsgs))
- return []byte(json + `]"`)
+ return []byte(wrapInJSONArray(strings))
+}
+
+func makeIntoString(rawMsg []byte) string {
+ return `"` + strings.ReplaceAll(string(rawMsg), "\"", "\\\"") + `"`
+}
+
+func addSeparatorIfNeeded(strings string, position, length int) string {
+ if position < length-1 {
+ strings = strings + ","
+ }
+ return strings
+}
+
+func wrapInJSONArray(strings string) string {
+ return "[" + strings + "]"
}
func (j *job) waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
}
func (j *job) waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
distributeClientMock := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.String() == "http://consumerHost/target" {
assertions.Equal(req.Method, "POST")
distributeClientMock := NewTestClient(func(req *http.Request) *http.Response {
if req.URL.String() == "http://consumerHost/target" {
assertions.Equal(req.Method, "POST")
- assertions.Equal(`"[{\"data\": 1},{\"data\": 2}]"`, getBodyAsString(req, t))
+ assertions.Equal(`["{\"data\": 1}","{\"data\": 2}","ABCDEFGH"]`, getBodyAsString(req, t))
assertions.Equal("application/json", req.Header.Get("Content-Type"))
wg.Done()
return &http.Response{
assertions.Equal("application/json", req.Header.Get("Content-Type"))
wg.Done()
return &http.Response{
go func() {
jobUnderTest.messagesChannel <- []byte(`{"data": 1}`)
jobUnderTest.messagesChannel <- []byte(`{"data": 2}`)
go func() {
jobUnderTest.messagesChannel <- []byte(`{"data": 1}`)
jobUnderTest.messagesChannel <- []byte(`{"data": 2}`)
+ jobUnderTest.messagesChannel <- []byte("ABCDEFGH")
}()
if waitTimeout(&wg, 2*time.Second) {
}()
if waitTimeout(&wg, 2*time.Second) {
MaxTimeMiliseconds: 200,
})
MaxTimeMiliseconds: 200,
})
- assertions.Equal([]byte("\"[0,1]\""), msgs)
+ assertions.Equal([]byte("[\"0\",\"1\"]"), msgs)
}
func TestJobReadBufferedWhenTimeout_shouldOnlyReturnMessagesSentBeforeTimeout(t *testing.T) {
assertions := require.New(t)
}
func TestJobReadBufferedWhenTimeout_shouldOnlyReturnMessagesSentBeforeTimeout(t *testing.T) {
assertions := require.New(t)
MaxTimeMiliseconds: 30,
})
MaxTimeMiliseconds: 30,
})
- assertions.Equal([]byte("\"[0,1]\""), msgs)
+ assertions.Equal([]byte("[\"0\",\"1\"]"), msgs)
}
func fillMessagesBuffer(mc chan []byte) {
}
func fillMessagesBuffer(mc chan []byte) {