Skip to content

Commit

Permalink
feat: Implement Ping and PingContext functionality in http driver
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Westra <[email protected]>
  • Loading branch information
webstradev committed Dec 16, 2023
1 parent 05c9f06 commit 2903dd5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libsql/internal/http/hranaV2/hranaV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ type hranaV2Conn struct {
streamClosed bool
}

func (h *hranaV2Conn) Ping() error {
return h.PingContext(context.Background())
}

func (h *hranaV2Conn) PingContext(ctx context.Context) error {
_, err := h.executeStmt(ctx, "SELECT 1", nil, false)
return err
}

func (h *hranaV2Conn) Prepare(query string) (driver.Stmt, error) {
return h.PrepareContext(context.Background(), query)
}
Expand Down
18 changes: 18 additions & 0 deletions tests/http/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@ func (t Tx) prepareInsertStmt() PreparedStmt {
return PreparedStmt{stmt, t.t}
}

func TestPing(t *testing.T) {
t.Parallel()
db := getDb(T{t})

// This ping should succeed because the database is up and running
db.t.FatalOnError(db.Ping())

t.Cleanup(func() {
db.Close()

// This ping should return an error because the database is already closed
err := db.Ping()
if err == nil {
db.t.Fatal("db.Ping succeeded when it should have failed")
}
})
}

func TestExecAndQuery(t *testing.T) {
t.Parallel()
db := getDb(T{t})
Expand Down

0 comments on commit 2903dd5

Please sign in to comment.