Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(queue): add ping function for redis #991

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions queue/redis/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: Apache-2.0

package redis

import (
"context"
"fmt"
)

// Ping contacts the queue to test its connection.
func (c *client) Ping(ctx context.Context) error {
// send ping request to client
err := c.Redis.Ping(ctx).Err()
if err != nil {
c.Logger.Debugf("unable to ping Redis queue.")
return fmt.Errorf("unable to establish connection to Redis queue")
}

return nil
}
61 changes: 61 additions & 0 deletions queue/redis/ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: Apache-2.0

package redis

import (
"context"
"fmt"
"github.com/alicebob/miniredis/v2"
"testing"
"time"
)

func TestRedis_Ping_Good(t *testing.T) {
_redis, err := miniredis.Run()
if err != nil {
t.Errorf("unable to create miniredis instance: %v", err)
}

defer _redis.Close()

// setup redis mock
goodRedis, err := New(
WithAddress(fmt.Sprintf("redis://%s", _redis.Addr())),
WithChannels("foo"),
WithCluster(false),
WithTimeout(5*time.Second),
)
if err != nil {
t.Errorf("unable to create queue service: %v", err)
}

// run tests
err = goodRedis.Ping(context.Background())

if err != nil {
t.Errorf("Ping returned err: %v", err)
}
}

func TestRedis_Ping_Bad(t *testing.T) {
_redis, err := miniredis.Run()
if err != nil {
t.Errorf("unable to create miniredis instance: %v", err)
}

defer _redis.Close()

// setup redis mock
badRedis, _ := New(
WithAddress(fmt.Sprintf("redis://%s", _redis.Addr())),
WithChannels("foo"),
WithCluster(false),
WithTimeout(5*time.Second),
)
_redis.SetError("not aiv")
// run tests
err = badRedis.Ping(context.Background())
if err == nil {
t.Errorf("Ping should have returned err: %v", err)
}
}
4 changes: 4 additions & 0 deletions queue/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type Service interface {
// item to the specified route in the queue.
Push(context.Context, string, []byte) error

// Ping defines a function that checks the
// connection to the queue.
Ping(context.Context) error

// Route defines a function that decides which
// channel a build gets placed within the queue.
Route(*pipeline.Worker) (string, error)
Expand Down