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

Dynamic Body not to be cached when a Func is used to created the body #826

Closed
paulmctigue opened this issue Oct 4, 2022 · 12 comments
Closed
Assignees
Labels

Comments

@paulmctigue
Copy link

paulmctigue commented Oct 4, 2022

I'm using wirenet.net as a standalone process. The response func code below loads an existing file from disk, mutates the string and return it to the user. In my case it mutates all the dates relative to the todays date.

Because it a dynamics response I don't want the response to be cached. Currently (version 1.5.6), I think, the behaviour is caching the response every time its invoked increasing the memory on each request.

server.Given(Request.Create()
	.WithPath($"/someurl/")
	.UsingPost())
	.RespondWith(Response.Create()
		.WithBody(req =>
		{
			var xml = File.ReadAllText($"./myxmlfile.xml");
		
			xml = MutateString(xml);
			return xml;
		})
	.WithStatusCode(200)
	.WithHeader("Content-Type", "application/xml"));
@paulmctigue paulmctigue changed the title Dynamic Body not to be cached when a Func is used to created the bod Dynamic Body not to be cached when a Func is used to created the body Oct 4, 2022
@StefH
Copy link
Collaborator

StefH commented Oct 11, 2022

I'm not sure if there is caching when you use a Func to define your response.

Caching can only be enabled for WithBodyFromFile.

Can you please double check?

@StefH
Copy link
Collaborator

StefH commented Oct 13, 2022

@paulmctigue
I did add a extra unit test: #828 which shows that it works correct.

@paulmctigue
Copy link
Author

paulmctigue commented Oct 14, 2022

@StefH It always returns the correct response string for the request. That bit is fine. However memory is increased for each new string that is returned. I'll double check it now

@StefH
Copy link
Collaborator

StefH commented Oct 14, 2022

Yes I can understand that the memory is increased because the return value from that Func is a string, and this is saved internally.

However I think that this is only kept in memory for logging the request. So cleaning the logging could help, or I can investigate if in case it's a Func, the response is not saved to the logging.

@paulmctigue
Copy link
Author

Yes its the log entries that are consuming the memory - this object WireMock.Util.BodyData.
I didn't see DeleteLogEntry method.

 foreach (var log in logs)
  {
      server.DeleteLogEntry(log.Guid);
  }

This solves the issue.

@paulmctigue
Copy link
Author

Maybe have a global setting to opt of this type of logging when setting up the server (in WireMockServerSettings).
Is the internal logging separate for the general logging because using WireMockNullLogger wouldn't help in this circumstance?

@StefH
Copy link
Collaborator

StefH commented Oct 14, 2022

Yes a good idea.

I'll see if this can be added, however, it will take some time because I first want merge some other PR's.

@StefH
Copy link
Collaborator

StefH commented Oct 15, 2022

@paulmctigue
You can now opt-out using DoNotSaveDynamicResponseInLogEntry = true

Preview version : 1.5.7-ci-16532

https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH StefH self-assigned this Oct 15, 2022
@paulmctigue
Copy link
Author

@StefH

Tested this and the memory kept on increasing.

<PackageReference Include="WireMock.Net" Version="1.5.7-ci-16532" />

settings.DoNotSaveDynamicResponseInLogEntry = true;
server = WireMockServer.Start(settings);

Are there log entries for request as well as responses?

@StefH
Copy link
Collaborator

StefH commented Oct 17, 2022

A log entry defines the request AND the response.

So the memory will increase, but not as much as before (in case you had big xml-strings to return).

If you do not want log entries at all, you can set the MaxRequestLogCount to 0 in the settings.

@StefH
Copy link
Collaborator

StefH commented Oct 18, 2022

@paulmctigue
Is the explanation sufficient?

@StefH
Copy link
Collaborator

StefH commented Oct 21, 2022

merged

@StefH StefH closed this as completed Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants