Skip to content

Commit 951c58d

Browse files
committed
connection timeout override with custom was fixed
1 parent bf16ec3 commit 951c58d

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

cassandra_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,3 +3410,42 @@ func TestQuery_SetHostID(t *testing.T) {
34103410
t.Fatalf("Expected error to be: %v, but got %v", ErrNoConnections, err)
34113411
}
34123412
}
3413+
3414+
func TestTimeoutOverride(t *testing.T) {
3415+
session := createSession(t)
3416+
defer session.Close()
3417+
3418+
if session.cfg.ProtoVersion < 3 {
3419+
t.Skip("named Values are not supported in protocol < 3")
3420+
}
3421+
3422+
if err := createTable(session, "CREATE TABLE gocql_test.named_query(id int, value text, PRIMARY KEY (id))"); err != nil {
3423+
t.Fatal(err)
3424+
}
3425+
3426+
// normal case
3427+
err := session.Query("INSERT INTO gocql_test.named_query(id, value) VALUES(1, 'value')").Exec()
3428+
if err != nil {
3429+
t.Fatal(err)
3430+
}
3431+
//decrease Conn.timeout
3432+
for _, conPool := range session.executor.pool.hostConnPools {
3433+
for _, conn := range conPool.conns {
3434+
conn.timeout = 50
3435+
}
3436+
3437+
}
3438+
err = session.Query("INSERT INTO gocql_test.named_query(id, value) VALUES(2, 'value')").Exec()
3439+
if err == nil || err != ErrTimeoutNoResponse {
3440+
t.Fatalf("expected: ErrTimeoutNoResponse, got: %v", err)
3441+
}
3442+
3443+
// override timeout with context
3444+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
3445+
defer cancel()
3446+
err = session.Query("TRUNCATE TABLE gocql_test.named_query").WithContext(ctx).Exec()
3447+
if err != nil {
3448+
t.Fatal(err)
3449+
}
3450+
3451+
}

conn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,8 @@ func (c *Conn) exec(ctx context.Context, req frameBuilder, tracer Tracer) (*fram
10991099
}
11001100

11011101
var timeoutCh <-chan time.Time
1102-
if c.timeout > 0 {
1102+
_, isDeadline := ctx.Deadline()
1103+
if c.timeout > 0 && !isDeadline {
11031104
if call.timer == nil {
11041105
call.timer = time.NewTimer(0)
11051106
<-call.timer.C

0 commit comments

Comments
 (0)