-
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.
- Loading branch information
TimHuynh
committed
Oct 23, 2023
1 parent
b51d5a2
commit a47dcff
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 shoudl 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