Skip to content

Commit

Permalink
Merge pull request #205 from tinh-tinh/test/ren/add-unit-test-redis
Browse files Browse the repository at this point in the history
test: add unit test for redis module
  • Loading branch information
Ren0503 authored Jan 10, 2025
2 parents 95c912d + e9f3fe0 commit 6ba98ae
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 6 deletions.
16 changes: 10 additions & 6 deletions microservices/redis/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func NewClient(opt Options) microservices.ClientProxy {
config: microservices.NewConfig(opt.Config),
}

return connect
}
if err := connect.Conn.Ping(connect.Context).Err(); err != nil {
panic(err)
}

func (c *Connect) Close() {
c.Conn.Close()
return connect
}

func (c *Connect) Headers() microservices.Header {
Expand Down Expand Up @@ -95,6 +95,10 @@ func New(module core.ModuleParam, opts ...Options) microservices.Service {
}
}

if err := connect.Conn.Ping(connect.Context).Err(); err != nil {
panic(err)
}

return connect
}

Expand Down Expand Up @@ -122,8 +126,8 @@ func (c *Connect) Create(module core.Module) {
}

func (c *Connect) Listen() {
store := c.Module.Ref(microservices.STORE).(*microservices.Store)
if store == nil {
store, ok := c.Module.Ref(microservices.STORE).(*microservices.Store)
if !ok {
panic("store not found")
}

Expand Down
115 changes: 115 additions & 0 deletions microservices/redis/connect_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis_test

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -157,6 +158,11 @@ func DeliveryApp() microservices.Service {
return nil
})

handler.OnEvent("order.created", func(ctx microservices.Ctx) error {
fmt.Println("Delivery when have order:", ctx.Payload(&Order{}))
return nil
})

return handler
}

Expand Down Expand Up @@ -259,3 +265,112 @@ func Benchmark_Practice(b *testing.B) {
}
})
}

func Test_Client_Error(t *testing.T) {
appModule := func() core.Module {
module := core.NewModule(core.NewModuleOptions{
Imports: []core.Modules{
microservices.RegisterClient(redis.NewClient(redis.Options{
Options: &redis_store.Options{
Addr: "localhost:637",
},
})),
},
})

return module
}
require.Panics(t, func() {
app := core.CreateFactory(appModule)
app.SetGlobalPrefix("api")
})

serverModule := func() core.Module {
module := core.NewModule(core.NewModuleOptions{
Imports: []core.Modules{microservices.Register()},
})
return module
}

server := redis.New(serverModule, redis.Options{
Options: &redis_store.Options{
Addr: "localhost:6379",
},
})
go server.Listen()

time.Sleep(100 * time.Millisecond)

clientController := func(module core.Module) core.Controller {
ctrl := module.NewController("test")

client := microservices.Inject(module)
ctrl.Get("", func(ctx core.Ctx) error {
go client.Send("abc", 1000)
return ctx.JSON(core.Map{"data": "ok"})
})

return ctrl
}

clientModule := func() core.Module {
module := core.NewModule(core.NewModuleOptions{
Imports: []core.Modules{
microservices.RegisterClient(redis.NewClient(redis.Options{
Options: &redis_store.Options{
Addr: "localhost:6379",
},
Config: microservices.Config{
Serializer: func(v interface{}) ([]byte, error) {
return nil, errors.New("error")
},
},
})),
},
Controllers: []core.Controllers{clientController},
})

return module
}

app := core.CreateFactory(clientModule)
app.SetGlobalPrefix("api")

testServer := httptest.NewServer(app.PrepareBeforeListen())
defer testServer.Close()

testClient := testServer.Client()
resp, err := testClient.Get(testServer.URL + "/api/test")
require.Nil(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
}

func Test_Server_Error(t *testing.T) {
require.Panics(t, func() {
serverModule := func() core.Module {
module := core.NewModule(core.NewModuleOptions{
Imports: []core.Modules{microservices.Register()},
})
return module
}
server := redis.New(serverModule, redis.Options{
Options: &redis_store.Options{
Addr: "localhost:637",
},
})
server.Listen()
})

require.Panics(t, func() {
serverModule := func() core.Module {
module := core.NewModule(core.NewModuleOptions{})
return module
}
server := redis.New(serverModule, redis.Options{
Options: &redis_store.Options{
Addr: "localhost:6379",
},
})
server.Listen()
})
}

0 comments on commit 6ba98ae

Please sign in to comment.