Skip to content

Commit

Permalink
fixup! Subscriptions: Add batching to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Jul 12, 2024
1 parent 4fe7234 commit 4da30d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions exchanges/subscription/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
errInvalidAssetExpandPairs = errors.New("subscription template containing PairSeparator with must contain either specific Asset or AssetSeparator")
errAssetRecords = errors.New("subscription template did not generate the expected number of asset records")
errPairRecords = errors.New("subscription template did not generate the expected number of pair records")
errBatchSize = errors.New("invalid BatchSize")
errTooManyBatchSize = errors.New("too many BatchSize directives")
errAssetTemplateWithoutAll = errors.New("sub.Asset must be set to All if AssetSeparator is used in Channel template")
errNoTemplateContent = errors.New("subscription template did not generate content")
errInvalidTemplate = errors.New("GetSubscriptionTemplate did not return a template")
Expand Down Expand Up @@ -158,7 +158,7 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It

batchSize := len(pairs) // Default to all pairs in one batch
if b := strings.Split(assetChannels, subCtx.BatchSize); len(b) > 2 {
return nil, fmt.Errorf("%w for %s: %w", errPairRecords, a, errBatchSize)
return nil, fmt.Errorf("%w for %s", errTooManyBatchSize, a)
} else if len(b) == 2 { // If there's a batch size indicator we batch by that
assetChannels = b[0]
if batchSize, err = strconv.Atoi(strings.TrimSpace(b[1])); err != nil {
Expand Down
16 changes: 13 additions & 3 deletions exchanges/subscription/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func TestExpandTemplates(t *testing.T) {
)
equalLists(t, exp, got)

_, err = List{{Channel: "nil"}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errInvalidTemplate, "Should get correct error on nil template")

_, err = List{{Channel: "feature1", Asset: asset.Spot, Pairs: currency.Pairs{currency.NewPairWithDelimiter("NOPE", "POPE", "🐰")}}}.ExpandTemplates(e)
assert.ErrorIs(t, err, currency.ErrPairNotContainedInAvailablePairs, "Should error correctly when pair not available")

e.tpl = "errors.tmpl"

_, err = List{{Channel: "error1", Asset: asset.Spot}}.ExpandTemplates(e)
Expand All @@ -95,6 +101,13 @@ func TestExpandTemplates(t *testing.T) {
_, err = List{{Channel: "error5", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errPairRecords, "Should error correctly when invalid number of pair entries")

_, err = List{{Channel: "error6", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errTooManyBatchSize, "Should error correctly when too many BatchSize directives")

_, err = List{{Channel: "error7", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, common.ErrTypeAssertFailure, "Should error correctly when batch size isn't an int")

e.tpl = "parse-error.tmpl"
e.tpl = "parse-error.tmpl"
_, err = l.ExpandTemplates(e)
assert.ErrorContains(t, err, "function \"explode\" not defined", "Should error correctly on unparsable template")
Expand All @@ -113,7 +126,4 @@ func TestExpandTemplates(t *testing.T) {
require.Len(t, got, 1, "Must get back the one sub")
assert.Equal(t, "already happy", l[0].QualifiedChannel, "Should get back the one sub")
assert.NotSame(t, got, l, "Should get back a different actual list")

_, err = List{{Channel: "nil"}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errInvalidTemplate, "Should get correct error on nil template")
}
15 changes: 15 additions & 0 deletions exchanges/subscription/testdata/errors.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
{{- .PairSeparator -}}
{{- .PairSeparator -}}
{{- .PairSeparator -}}
{{- else if eq .S.Channel "error6" }}
{{/* Too many BatchSize commands */}}
{{- range $asset, $pairs := $.AssetPairs }}
{{- $pairs.Join -}}
{{- $.BatchSize -}}1
{{- $.BatchSize -}}2
{{- $.AssetSeparator -}}
{{- end -}}
{{- else if eq .S.Channel "error7" }}
{{/* BatchSize without number */}}
{{- range $asset, $pairs := $.AssetPairs }}
{{- $pairs.Join -}}
{{- $.BatchSize -}}
{{- $.AssetSeparator -}}
{{- end -}}
{{- else if eq .S.Channel "empty-content" }}
{{/* Empty response for the pair */}}
{{- range $asset, $pairs := $.AssetPairs }}
Expand Down

0 comments on commit 4da30d0

Please sign in to comment.