From 6708cea79580d84437f93ea9755056ed330586b0 Mon Sep 17 00:00:00 2001 From: Tim Huynh Date: Tue, 31 Oct 2023 14:56:02 -0500 Subject: [PATCH] feat(queue): add ping function for redis (#991) * add ping functin for redis * Update queue/redis/ping_test.go Co-authored-by: Jacob Floyd --------- Co-authored-by: TimHuynh Co-authored-by: Jacob Floyd --- queue/redis/ping.go | 20 +++++++++++++ queue/redis/ping_test.go | 61 ++++++++++++++++++++++++++++++++++++++++ queue/service.go | 4 +++ 3 files changed, 85 insertions(+) create mode 100644 queue/redis/ping.go create mode 100644 queue/redis/ping_test.go diff --git a/queue/redis/ping.go b/queue/redis/ping.go new file mode 100644 index 000000000..57d97a901 --- /dev/null +++ b/queue/redis/ping.go @@ -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 +} diff --git a/queue/redis/ping_test.go b/queue/redis/ping_test.go new file mode 100644 index 000000000..29f84ee26 --- /dev/null +++ b/queue/redis/ping_test.go @@ -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) + } +} diff --git a/queue/service.go b/queue/service.go index fe39524b9..fd36a64bc 100644 --- a/queue/service.go +++ b/queue/service.go @@ -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)