Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs committed Jan 20, 2025
1 parent c76caab commit cb41ddd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 47 deletions.
18 changes: 10 additions & 8 deletions pkg/core/capability/manual/interaction/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func New(manualIntegrations []IInteractionIntegrationNotifier) *InteractionContr
// - Change waitInteractionIntegrationResponse to be waitResponse
// - Put result := <- interactionintegrationchannel into a separate function
// - Just use the one instance of manual capability channel. Do not use interactionintegrationchannel
// - Create typed error and pass back to API function for Storage interface fcns

// ############################################################################
// ICapabilityInteraction implementation
Expand Down Expand Up @@ -104,6 +105,13 @@ func (manualController *InteractionController) Queue(command manual.CommandInfo,
}

func (manualController *InteractionController) handleManualCommandResponse(command manual.CommandInfo, manualComms manual.ManualCapabilityCommunication) {
log.Trace(
fmt.Sprintf(
"goroutine handling command response %s, %s has started", command.Metadata.ExecutionId.String(), command.Metadata.StepId))
defer log.Trace(
fmt.Sprintf(
"goroutine handling command response %s, %s has ended", command.Metadata.ExecutionId.String(), command.Metadata.StepId))

select {
case <-manualComms.TimeoutContext.Done():
if manualComms.TimeoutContext.Err().Error() == ctxModel.ErrorContextTimeout {
Expand Down Expand Up @@ -159,12 +167,7 @@ func (manualController *InteractionController) PostContinue(response manual.Inte
//Then put outArgs back into manualCapabilityChannel
// Copy result and conversion back to interactionResponse format
log.Trace("pushing assigned variables in manual capability channel")
pendingEntry.Channel <- manual.InteractionResponse{
Metadata: response.Metadata,
ResponseError: nil,
ResponseStatus: response.ResponseStatus,
OutArgsVariables: response.OutArgsVariables,
}
pendingEntry.Channel <- response

if len(warnings) > 0 {
for _, warning := range warnings {
Expand Down Expand Up @@ -270,8 +273,7 @@ func (manualController *InteractionController) validateMatchingOutArgs(pendingEn
for varName, variable := range responseOutArgs {
// first check that out args provided match the variables
if _, ok := pendingEntry.CommandInfo.OutArgsVariables[varName]; !ok {
warns = append(warns, fmt.Sprintf("provided out arg %s does not match any intended out arg", varName))
err = errors.New("provided out args do not match command-related variables")
err = errors.New(fmt.Sprintf("provided out arg %s does not match any intended out arg", varName))
}
// then warn if any value outside "value" has changed
if pending, ok := pendingEntry.CommandInfo.OutArgsVariables[varName]; ok {
Expand Down
55 changes: 19 additions & 36 deletions pkg/core/capability/manual/interaction/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,16 @@ func TestCopyOutArgsToVars(t *testing.T) {
func TestPostContinueWarningsRaised(t *testing.T) {

interaction := New([]IInteractionIntegrationNotifier{})
timeout := 500 * time.Millisecond
timeout := 5 * time.Second
testCtx, testCancel := context.WithTimeout(context.Background(), timeout)

defer testCancel()

hook := NewTestLogHook()
log.Logger.AddHook(hook)

testCapComms := manualModel.ManualCapabilityCommunication{
Channel: make(chan manualModel.InteractionResponse),
TimeoutContext: testCtx,
}
defer close(testCapComms.Channel)

err := interaction.Queue(testInteractionCommand, testCapComms)
if err != nil {
Expand All @@ -305,27 +302,38 @@ func TestPostContinueWarningsRaised(t *testing.T) {
OutArgsVariables: cacao.Variables{"var2": outArg},
}

err = interaction.PostContinue(outArgsUpdate)
// Start a goroutine to read from the channel to avoid blocking
go func() {
for response := range testCapComms.Channel {
log.Trace("Received response:", response)
}
}()

err = interaction.PostContinue(outArgsUpdate)
var expectedErr error = nil

assert.Equal(t, err, expectedErr)

// Simulating Manual Capability closing the channel and the context
close(testCapComms.Channel)
testCancel()
time.Sleep(800 * time.Millisecond)

expectedLogEntry1 := "provided out arg var2 has different value for 'Constant' property of intended out arg. This different value is ignored."
expectedLogEntry2 := "provided out arg var2 has a different value for 'Description' property of intended out arg. This different value is ignored."
expectedLogEntry3 := "provided out arg var2 has a different value for 'External' property of intended out arg. This different value is ignored."
expectedLogEntry4 := "provided out arg var2 has a different value for 'Type' property of intended out arg. This different value is ignored."
expectedLogEntry2 := "provided out arg var2 has different value for 'Description' property of intended out arg. This different value is ignored."
expectedLogEntry3 := "provided out arg var2 has different value for 'External' property of intended out arg. This different value is ignored."
expectedLogEntry4 := "provided out arg var2 has different value for 'Type' property of intended out arg. This different value is ignored."
expectedLogs := []string{expectedLogEntry1, expectedLogEntry2, expectedLogEntry3, expectedLogEntry4}

all := true
for _, expectedMessage := range expectedLogs {
containsAll := true
for _, entry := range hook.Entries {
if strings.Contains(expectedMessage, entry.Message) {
if strings.Contains(entry.Message, expectedMessage) {
containsAll = true
break
}
if !strings.Contains(expectedMessage, entry.Message) {
if !strings.Contains(entry.Message, expectedMessage) {
containsAll = false
}
}
Expand Down Expand Up @@ -378,34 +386,9 @@ func TestPostContinueFailOnNonexistingVariable(t *testing.T) {

err = interaction.PostContinue(outArgsUpdate)

expectedErr := errors.New("provided out args do not match command-related variables")

expectedLogEntry1 := "provided out args do not match command-related variables"
expectedLogs := []string{expectedLogEntry1}

all := true
for _, expectedMessage := range expectedLogs {
containsAll := true
for _, entry := range hook.Entries {
if strings.Contains(expectedMessage, entry.Message) {
containsAll = true
break
}
if !strings.Contains(expectedMessage, entry.Message) {
containsAll = false
}
}
if !containsAll {
t.Logf("log message: '%s' not found in logged messages", expectedMessage)
all = false
break
}
}
expectedErr := errors.New(fmt.Sprintf("provided out arg %s does not match any intended out arg", outArg.Name))

assert.Equal(t, err, expectedErr)

assert.NotEqual(t, len(hook.Entries), 0)
assert.Equal(t, all, true)
}

func TestRegisterRetrieveNewExecutionNewPendingInteraction(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/api/routes/manual_api/manual_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGetPendingCommandsCalled(t *testing.T) {
recorder := httptest.NewRecorder()
api_routes.ManualRoutes(app, manualApiHandler)

mock_interaction_storage.On("GetPendingCommands").Return([]manual.CommandInfo{}, 200, nil)
mock_interaction_storage.On("GetPendingCommands").Return([]manual.CommandInfo{}, nil)

request, err := http.NewRequest("GET", "/manual/", nil)
if err != nil {
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestGetPendingCommandCalled(t *testing.T) {
}
emptyCommandInfoList := manual.CommandInfo{}

mock_interaction_storage.On("GetPendingCommand", executionMetadata).Return(emptyCommandInfoList, 200, nil)
mock_interaction_storage.On("GetPendingCommand", executionMetadata).Return(emptyCommandInfoList, nil)

request, err := http.NewRequest("GET", path, nil)
if err != nil {
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestPostContinueCalled(t *testing.T) {
t.Fatalf("Error marshalling JSON: %v", err)
}

mock_interaction_storage.On("PostContinue", testManualResponse).Return(200, nil)
mock_interaction_storage.On("PostContinue", testManualResponse).Return(nil)

request, err := http.NewRequest("POST", path, bytes.NewBuffer(jsonData))
if err != nil {
Expand Down

0 comments on commit cb41ddd

Please sign in to comment.