Skip to content

Commit

Permalink
add ping functin for redis
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHuynh committed Oct 23, 2023
1 parent b51d5a2 commit a47dcff
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
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 shoudl 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

0 comments on commit a47dcff

Please sign in to comment.