Skip to content

Commit deb64e8

Browse files
committed
connection timeout override with custom was fixed
1 parent 65e2caf commit deb64e8

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
@@ -3362,3 +3362,42 @@ func TestQuery_NamedValues(t *testing.T) {
33623362
t.Fatal(err)
33633363
}
33643364
}
3365+
3366+
func TestTimeoutOverride(t *testing.T) {
3367+
session := createSession(t)
3368+
defer session.Close()
3369+
3370+
if session.cfg.ProtoVersion < 3 {
3371+
t.Skip("named Values are not supported in protocol < 3")
3372+
}
3373+
3374+
if err := createTable(session, "CREATE TABLE gocql_test.named_query(id int, value text, PRIMARY KEY (id))"); err != nil {
3375+
t.Fatal(err)
3376+
}
3377+
3378+
// normal case
3379+
err := session.Query("INSERT INTO gocql_test.named_query(id, value) VALUES(1, 'value')").Exec()
3380+
if err != nil {
3381+
t.Fatal(err)
3382+
}
3383+
//decrease Conn.timeout
3384+
for _, conPool := range session.executor.pool.hostConnPools {
3385+
for _, conn := range conPool.conns {
3386+
conn.timeout = 50
3387+
}
3388+
3389+
}
3390+
err = session.Query("INSERT INTO gocql_test.named_query(id, value) VALUES(2, 'value')").Exec()
3391+
if err == nil || err != ErrTimeoutNoResponse {
3392+
t.Fatalf("expected: ErrTimeoutNoResponse, got: %v", err)
3393+
}
3394+
3395+
// override timeout with context
3396+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
3397+
defer cancel()
3398+
err = session.Query("TRUNCATE TABLE gocql_test.named_query").WithContext(ctx).Exec()
3399+
if err != nil {
3400+
t.Fatal(err)
3401+
}
3402+
3403+
}

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)