mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-08-07 23:31:55 +00:00
2ceb1c3562
* * feat(agent): add support for binary algorithm execution * * feat(agent/algorithm): add Algorithm interface and binary implementation * * feat(agent/algorithm/binary): implement Run method for binary algorithm execution * * feat(agent/algorithm/logging): implement Stdout and Stderr writers for algorithm logging * * feat(agent/algorithm/logging_test): add tests for Stdout and Stderr writers * * feat(agent/events): add Service interface for sending events * * feat(agent/events/mocks): add mock implementation for Service interface * * refactor(agent/service): update runComputation method to use binary algorithm implementation Signed-off-by: SammyOina <sammyoina@gmail.com> * * fix(logging.go): handle error when sending event in Write method of Stderr struct * test(logging_test.go): add copyright header * fix(backend_info.go): add missing type declaration in function signature * fix(agent.go): rename progressbar variable to pb for clarity and consistency Signed-off-by: SammyOina <sammyoina@gmail.com> --------- Signed-off-by: SammyOina <sammyoina@gmail.com>
86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package algorithm
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
mglog "github.com/absmach/magistrala/logger"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/ultravioletrs/cocos/agent/events/mocks"
|
|
)
|
|
|
|
func TestStdoutWrite(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "Single line",
|
|
input: "Hello, World!",
|
|
expected: []string{"Hello, World!"},
|
|
},
|
|
{
|
|
name: "Multiple lines",
|
|
input: "Line 1\nLine 2\nLine 3",
|
|
expected: []string{"Line 1\nLine 2\nLine 3"},
|
|
},
|
|
{
|
|
name: "Long input",
|
|
input: strings.Repeat("a", bufSize+100),
|
|
expected: []string{strings.Repeat("a", bufSize), strings.Repeat("a", 100)},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
stdout := &Stdout{Logger: mglog.NewMock()}
|
|
n, err := stdout.Write([]byte(tt.input))
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, len(tt.input), n)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStderrWrite(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "Single line",
|
|
input: "Error: Something went wrong",
|
|
expected: []string{"Error: Something went wrong"},
|
|
},
|
|
{
|
|
name: "Multiple lines",
|
|
input: "Error 1\nError 2\nError 3",
|
|
expected: []string{"Error 1\nError 2\nError 3"},
|
|
},
|
|
{
|
|
name: "Long input",
|
|
input: strings.Repeat("e", bufSize+100),
|
|
expected: []string{strings.Repeat("e", bufSize), strings.Repeat("e", 100)},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mockEventService := mocks.NewService(t)
|
|
mockEventService.On("SendEvent", "algorithm-run", "failed", mock.Anything).Return(nil)
|
|
|
|
stderr := &Stderr{Logger: mglog.NewMock(), EventSvc: mockEventService}
|
|
n, err := stderr.Write([]byte(tt.input))
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, len(tt.input), n)
|
|
mockEventService.AssertExpectations(t)
|
|
})
|
|
}
|
|
}
|