Skip to content

Commit ae2efae

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

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

cassandra_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,3 +3410,46 @@ 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+
3432+
//decrease Conn.timeout
3433+
session.executor.pool.mu.Lock()
3434+
for _, conPool := range session.executor.pool.hostConnPools {
3435+
conPool.mu.Lock()
3436+
for _, conn := range conPool.conns {
3437+
conn.timeout = 50
3438+
}
3439+
conPool.mu.Unlock()
3440+
}
3441+
session.executor.pool.mu.Unlock()
3442+
err = session.Query("INSERT INTO gocql_test.named_query(id, value) VALUES(2, 'value')").Exec()
3443+
if err == nil || err != ErrTimeoutNoResponse {
3444+
t.Fatalf("expected: ErrTimeoutNoResponse, got: %v", err)
3445+
}
3446+
3447+
// override timeout with context
3448+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
3449+
defer cancel()
3450+
err = session.Query("TRUNCATE TABLE gocql_test.named_query").WithContext(ctx).Exec()
3451+
if err != nil {
3452+
t.Fatal(err)
3453+
}
3454+
3455+
}

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)