-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(queue): add ping function for redis (#991)
* add ping functin for redis * Update queue/redis/ping_test.go Co-authored-by: Jacob Floyd <[email protected]> --------- Co-authored-by: TimHuynh <[email protected]> Co-authored-by: Jacob Floyd <[email protected]>
- Loading branch information
1 parent
3ee9ccf
commit 6708cea
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters