-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add redis transporter * add redis to action test
- Loading branch information
1 parent
4ef1e23
commit 97f0d46
Showing
5 changed files
with
131 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
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
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
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,59 @@ | ||
package gosumer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type Redis struct { | ||
Host string | ||
Port int | ||
User string | ||
Password string | ||
DB uint8 | ||
Channel string | ||
} | ||
|
||
var rdb *redis.Client | ||
|
||
func (red Redis) connect() error { | ||
var err error | ||
rdb = redis.NewClient(&redis.Options{ | ||
Addr: fmt.Sprintf("%s:%d", red.Host, red.Port), | ||
Username: red.User, | ||
Password: red.Password, | ||
DB: int(red.DB), | ||
}) | ||
ctx := context.Background() | ||
err = rdb.Ping(ctx).Err() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (red Redis) listen(fn process, message any) error { | ||
err := red.connect() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
ctx := context.Background() | ||
sub := rdb.Subscribe(ctx, red.Channel) | ||
defer sub.Close() | ||
for { | ||
msg, err := sub.ReceiveMessage(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var e chan error | ||
go fn(msg, e) | ||
} | ||
} |
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,58 @@ | ||
package gosumer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRedisConnect(t *testing.T) { | ||
transport := Redis{ | ||
Host: "localhost", | ||
Port: 6379, | ||
Channel: "channel_name", | ||
} | ||
|
||
err := transport.connect() | ||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
} | ||
|
||
func TestRedisListen(t *testing.T) { | ||
transport := Redis{ | ||
Host: "localhost", | ||
Port: 6379, | ||
User: "", | ||
Password: "", | ||
Channel: "channel_1", | ||
DB: 0, | ||
} | ||
|
||
go func() { | ||
err := transport.listen(processMessage, Message{}) | ||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
}() | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
}) | ||
time.Sleep(1 * time.Second) | ||
|
||
ctx := context.Background() | ||
body := `{"id": 1, "name": "John Doe"}` | ||
res := rdb.Publish(ctx, "channel_1", body) | ||
|
||
if res.Err() != nil { | ||
t.Errorf("Expected no error, got %v", res.Err()) | ||
} | ||
time.Sleep(1 * time.Second) | ||
|
||
assert.True(t, processMessageCalled, "Expected processMessage to be called") | ||
processMessageCalled = false | ||
} |