Skip to content

fix(templates): apply delay from go_template_json dynamic responses#333

Merged
gwleclerc merged 1 commit into
mainfrom
fix/go-template-delay
Jul 19, 2026
Merged

fix(templates): apply delay from go_template_json dynamic responses#333
gwleclerc merged 1 commit into
mainfrom
fix/go-template-delay

Conversation

@gwleclerc

Copy link
Copy Markdown
Collaborator

Fixes #305.

Problem

A delay written as duration strings in a go_template_json dynamic response was silently ignored — the response returned immediately — while the same mock worked under lua:

dynamic_response:
  engine: go_template_json
  script: >
    { "body": {...}, "delay": { "min": "0", "max": "10ms" } }

Root cause

Delay.UnmarshalJSON only accepted numeric nanoseconds, not duration strings like "10ms". The Lua engine pre-parses durations (time.ParseDuration) and go_template_yaml happens to accept the string form, but go_template_json feeds the raw JSON straight to Delay.UnmarshalJSON, which errored on the string. goTemplateJsonEngine.Execute logs that error but doesn't return it, so the delay was dropped as {0, 0} and the mock answered with no delay.

Fix

Parse each delay bound as either a duration string (time.ParseDuration) or a number of nanoseconds, matching the Lua/yaml engines. Marshalling is unchanged (still numeric nanoseconds), so the on-disk mock format is unaffected.

Verified

  • New engine tests: object form {min,max} and scalar shorthand "5ms" parse correctly.
  • Golden serialization + JSON-schema tests unchanged; full server suite green.
  • Runtime: a go_template_json mock with delay {min:300ms, max:300ms} now responds in ~0.30s (was ~0s).

🤖 Generated with Claude Code

…305)

Delay.UnmarshalJSON only accepted numeric nanoseconds, so a delay written as a
duration string ("10ms") — the form used in the docs and accepted by the Lua and
go_template_yaml engines — failed to parse. go_template_json swallows that error,
so the delay was silently dropped and the response returned immediately.

Parse each bound as either a duration string (time.ParseDuration) or a number of
nanoseconds, like the other engines. Marshalling is unchanged (still numeric
nanoseconds), so the on-disk mock format is unaffected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@gwleclerc
gwleclerc merged commit 74061c0 into main Jul 19, 2026
6 checks passed
@gwleclerc
gwleclerc deleted the fix/go-template-delay branch July 19, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Delay not working when using go_template

1 participant