Skip to content

Commit 9f35e38

Browse files
committed
Fix USING TIMEOUT time generation
Scylla does not support fractions. We need to make sure that time is formatted the following way: XmYsZms
1 parent ab73391 commit 9f35e38

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

Diff for: qb/using.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (u *using) writeCql(cql *bytes.Buffer) (names []string) {
9494

9595
if u.timeout != 0 {
9696
writePreamble(cql)
97-
fmt.Fprintf(cql, "TIMEOUT %s ", u.timeout)
97+
fmt.Fprintf(cql, "TIMEOUT %s ", formatDuration(u.timeout))
9898
} else if u.timeoutName != "" {
9999
writePreamble(cql)
100100
cql.WriteString("TIMEOUT ? ")

Diff for: qb/using_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ func TestUsing(t *testing.T) {
5757
B: new(using).Timeout(time.Second),
5858
S: "USING TIMEOUT 1s ",
5959
},
60+
// Timeout faction
61+
{
62+
B: new(using).Timeout(time.Second + 100*time.Millisecond),
63+
S: "USING TIMEOUT 1s100ms ",
64+
},
6065
// TimeoutNamed
6166
{
6267
B: new(using).TimeoutNamed("to"),

Diff for: qb/utils.go

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package qb
66

77
import (
88
"bytes"
9+
"fmt"
10+
"strings"
11+
"time"
912
)
1013

1114
// placeholders returns a string with count ? placeholders joined with commas.
@@ -31,3 +34,26 @@ func (cols columns) writeCql(cql *bytes.Buffer) {
3134
}
3235
}
3336
}
37+
38+
func formatDuration(d time.Duration) string {
39+
// Round the duration to the nearest millisecond
40+
// Extract hours, minutes, seconds, and milliseconds
41+
minutes := d / time.Minute
42+
d %= time.Minute
43+
seconds := d / time.Second
44+
d %= time.Second
45+
milliseconds := d / time.Millisecond
46+
47+
// Format the duration string
48+
var res []string
49+
if minutes > 0 {
50+
res = append(res, fmt.Sprintf("%dm", minutes))
51+
}
52+
if seconds > 0 {
53+
res = append(res, fmt.Sprintf("%ds", seconds))
54+
}
55+
if milliseconds > 0 {
56+
res = append(res, fmt.Sprintf("%dms", milliseconds))
57+
}
58+
return strings.Join(res, "")
59+
}

Diff for: qb/utils_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package qb
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestFormatDuration(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input time.Duration
12+
expected string
13+
}{
14+
{
15+
name: "Zero duration",
16+
input: 0,
17+
expected: "",
18+
},
19+
{
20+
input: 500 * time.Millisecond,
21+
expected: "500ms",
22+
},
23+
{
24+
input: 10 * time.Second,
25+
expected: "10s",
26+
},
27+
{
28+
input: 3 * time.Minute,
29+
expected: "3m",
30+
},
31+
{
32+
input: (2 * time.Minute) + (30 * time.Second),
33+
expected: "2m30s",
34+
},
35+
{
36+
input: (15 * time.Second) + (250 * time.Millisecond),
37+
expected: "15s250ms",
38+
},
39+
{
40+
input: (1 * time.Minute) + (45 * time.Second) + (123 * time.Millisecond),
41+
expected: "1m45s123ms",
42+
},
43+
{
44+
input: (5 * time.Minute) + (1 * time.Second) + (999 * time.Millisecond),
45+
expected: "5m1s999ms",
46+
},
47+
{
48+
input: (2 * time.Second) + (1500 * time.Millisecond), // 3 seconds, 500ms
49+
expected: "3s500ms",
50+
},
51+
}
52+
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
actual := formatDuration(tt.input)
56+
if actual != tt.expected {
57+
t.Errorf("got %q, want %q", actual, tt.expected)
58+
}
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)