Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize redundant mock scripts evaluation #103

Open
2 tasks done
w-ess opened this issue Jul 5, 2022 · 3 comments
Open
2 tasks done

Optimize redundant mock scripts evaluation #103

w-ess opened this issue Jul 5, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@w-ess
Copy link

w-ess commented Jul 5, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing bugs to ensure a similar bug has not already been created

Description

Redundant script evaluation

Steps to reproduce

Given a mock with a single custom script in the body. Calling its respective method, the script will be evaluated twice.

Expected behavior

The script should be evaluated once.

Screenshots

image
image

Additional context

No response

@w-ess w-ess added the bug Something isn't working label Jul 5, 2022
@natenho
Copy link
Owner

natenho commented Jul 5, 2022

Hello @w-ess , could you provide more details? how are you noticing the duplication? I've just sent an e-mail so we can work on your case

@w-ess
Copy link
Author

w-ess commented Jul 8, 2022

Hi, example sent by e-mail

@natenho
Copy link
Owner

natenho commented Jul 12, 2022

The behavior described in this issue is actually by design and is caused by multiple template transformations occurring along the mocking pipeline, leading to re-evaluation of script snippets.

For instance, given the mock in the provided screenshot, the script <#= TesteObterLInk.ObterLinl() #> will be executed at 2 distinct moments:

  1. Before deciding which mock to return, it needs to transform each template to evaluate the "condition" field of each one:
    var template = await _templateTransformer.Transform(mock.RawTemplate, conditionMatcherScriptContext);

Given that the condition is dynamic, the mock must not be returned when it is evaluated to false. This is step could be optimized to partially evaluate the template, looking only for the condition field

  1. After deciding which mock to return, we need to effectively produce the output:
    var template = await templateTransformer.TransformAndSetVariables(mock.RawTemplate, scriptContext);

This stage leads to re-evaluation of mock scripts, this time with all context variables available (like route variables).

There is also a third possible moment where the script might be evaluated again, when using the callback template:

var template = await templateTransformer.Transform(mockacoContext.Mock.RawTemplate, scriptContext);

@w-ess I'll change the title of the issue and consider this optimization as an improvement instead of an actual bug.

@natenho natenho added enhancement New feature or request and removed bug Something isn't working labels Jul 12, 2022
@natenho natenho changed the title requisition duplication Optimize redundant mock scripts evaluation Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants