Deribit: Add subscription configuration (#1636)

* Kline: Fix Raw Short, Marshal and Unmarshal

* Deribit: Rename GenerateDefaultSubs

* Deribit: Remove custom GetDefaultConfig

Moved to exchange base by #1472

* Deribit: Straight Rename of eps to endpoints

Since I had to ask what this abbreviation meant, I think we should
abandon it

* Deribit: Add Subscription configuration

* Deribit: Fix race on Setup with optionsRegex

Calling Setup twice would race on the assignment to this package var.

There was an option to just move the assignment to the package var declaration, but this
change improves the performance and allocations:
```
BenchmarkOptionPairToString-8            1000000              1239 ns/op             485 B/op         10 allocs/op
BenchmarkOptionPairToString2-8           3473804               656.2 ns/op           348 B/op          7 allocs/op
```

I've also removed the t.Run because even success the -v output from
tests would be very noisy, and I don't think we were getting any benefit
from it at all:
```
=== RUN   TestOptionPairToString
=== PAUSE TestOptionPairToString
=== CONT  TestOptionPairToString
=== RUN   TestOptionPairToString/BTC-30MAY24-61000-C
=== PAUSE TestOptionPairToString/BTC-30MAY24-61000-C
=== RUN   TestOptionPairToString/ETH-1JUN24-3200-P
=== PAUSE TestOptionPairToString/ETH-1JUN24-3200-P
=== RUN   TestOptionPairToString/SOL_USDC-31MAY24-162-P
=== PAUSE TestOptionPairToString/SOL_USDC-31MAY24-162-P
=== RUN   TestOptionPairToString/MATIC_USDC-6APR24-0d98-P
=== PAUSE TestOptionPairToString/MATIC_USDC-6APR24-0d98-P
=== CONT  TestOptionPairToString/BTC-30MAY24-61000-C
=== CONT  TestOptionPairToString/SOL_USDC-31MAY24-162-P
=== CONT  TestOptionPairToString/ETH-1JUN24-3200-P
=== CONT  TestOptionPairToString/MATIC_USDC-6APR24-0d98-P
--- PASS: TestOptionPairToString (0.00s)
    --- PASS: TestOptionPairToString/BTC-30MAY24-61000-C (0.00s)
    --- PASS: TestOptionPairToString/ETH-1JUN24-3200-P (0.00s)
    --- PASS: TestOptionPairToString/SOL_USDC-31MAY24-162-P (0.00s)
    --- PASS: TestOptionPairToString/MATIC_USDC-6APR24-0d98-P (0.00s)
```

( And that got worse with me adding more tests )
This commit is contained in:
Gareth Kirwan
2025-01-02 21:55:58 +00:00
committed by GitHub
parent b209b0e1a1
commit 4fcee8489e
9 changed files with 308 additions and 528 deletions

View File

@@ -135,6 +135,9 @@ func (i Interval) Duration() time.Duration {
// Short returns short string version of interval
func (i Interval) Short() string {
if i == Raw {
return "raw"
}
s := i.String()
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
@@ -149,6 +152,10 @@ func (i Interval) Short() string {
// It does not validate the duration is aligned, only that it is a parsable duration
func (i *Interval) UnmarshalJSON(text []byte) error {
text = bytes.Trim(text, `"`)
if string(text) == "raw" {
*i = Raw
return nil
}
if len(bytes.TrimLeft(text, `0123456789`)) > 0 { // contains non-numerics, ParseDuration can handle errors
d, err := time.ParseDuration(string(text))
if err != nil {

View File

@@ -143,9 +143,9 @@ func TestKlineDuration(t *testing.T) {
func TestKlineShort(t *testing.T) {
t.Parallel()
if OneDay.Short() != "24h" {
t.Fatalf("unexpected result: %v", OneDay.Short())
}
assert.Equal(t, "24h", OneDay.Short(), "One day should show as 24h")
assert.Equal(t, "1h", OneHour.Short(), "One hour should truncate 0m0s suffix")
assert.Equal(t, "raw", Raw.Short(), "Raw should return raw")
}
func TestDurationToWord(t *testing.T) {
@@ -1403,16 +1403,15 @@ func TestGetIntervalResultLimit(t *testing.T) {
}
func TestUnmarshalJSON(t *testing.T) {
i := new(Interval)
err := i.UnmarshalJSON([]byte(`"3m"`))
assert.NoError(t, err, "UnmarshalJSON should not error")
assert.Equal(t, time.Minute*3, i.Duration(), "Interval should have correct value")
err = i.UnmarshalJSON([]byte(`"15s"`))
assert.NoError(t, err, "UnmarshalJSON should not error")
assert.Equal(t, time.Second*15, i.Duration(), "Interval should have correct value")
err = i.UnmarshalJSON([]byte(`720000000000`))
assert.NoError(t, err, "UnmarshalJSON should not error")
assert.Equal(t, time.Minute*12, i.Duration(), "Interval should have correct value")
err = i.UnmarshalJSON([]byte(`"6hedgehogs"`))
t.Parallel()
var i Interval
for _, tt := range []struct {
in string
exp Interval
}{{`"3m"`, ThreeMin}, {`"15s"`, FifteenSecond}, {`720000000000`, OneMin * 12}, {`"-1ns"`, Raw}, {`"raw"`, Raw}} {
err := i.UnmarshalJSON([]byte(tt.in))
assert.NoErrorf(t, err, "UnmarshalJSON should not error on %q", tt.in)
}
err := i.UnmarshalJSON([]byte(`"6hedgehogs"`))
assert.ErrorContains(t, err, "unknown unit", "UnmarshalJSON should error")
}