From a47dcff11688684b4cb6ea2dd0166055b29e5ecb Mon Sep 17 00:00:00 2001 From: TimHuynh Date: Mon, 23 Oct 2023 15:56:40 -0500 Subject: [PATCH 1/2] add ping functin for redis --- 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..98a48cb5c --- /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 shoudl 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) From 079625ef737ccec9ab5e6bf44e0fb087a7cd3913 Mon Sep 17 00:00:00 2001 From: Tim Huynh Date: Wed, 25 Oct 2023 10:10:29 -0500 Subject: [PATCH 2/2] Update queue/redis/ping_test.go Co-authored-by: Jacob Floyd --- queue/redis/ping_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/queue/redis/ping_test.go b/queue/redis/ping_test.go index 98a48cb5c..29f84ee26 100644 --- a/queue/redis/ping_test.go +++ b/queue/redis/ping_test.go @@ -56,6 +56,6 @@ func TestRedis_Ping_Bad(t *testing.T) { // run tests err = badRedis.Ping(context.Background()) if err == nil { - t.Errorf("Ping shoudl have returned err: %v", err) + t.Errorf("Ping should have returned err: %v", err) } }