Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(microservices): update unit test for v2 beta #170

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion microservices/nats/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewClient(opt microservices.ConnectOptions) microservices.ClientProxy {
}

func (c *Connect) Send(event string, data interface{}) error {
payload, err := c.serializer(data)
payload, err := c.Serializer(data)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nats_test

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -42,7 +43,7 @@ func OrderApp() *core.App {
handler := microservices.NewHandler(module, core.ProviderOptions{})

orderService := module.Ref(ORDER).(*OrderService)
handler.OnResponse("order.created", func(ctx microservices.Ctx) {
handler.OnEvent("order.created", func(ctx microservices.Ctx) {
data := ctx.Payload(&Order{}).(*Order)

orderService.mutex.Lock()
Expand Down Expand Up @@ -122,7 +123,7 @@ func ProductApp(addr string) *core.App {
return app
}

func Test_Practice(t *testing.T) {
func Test_Hybrid(t *testing.T) {
orderApp := OrderApp()
orderApp.ConnectMicroservice(nats.Open(microservices.ConnectOptions{
Addr: "localhost:3010",
Expand Down Expand Up @@ -163,6 +164,44 @@ func Test_Practice(t *testing.T) {
require.Equal(t, `{"data":{"order1":true}}`, string(data))
}

func Test_Standalone(t *testing.T) {
appService := func(module core.Module) core.Provider {
handler := microservices.NewHandler(module, core.ProviderOptions{})

handler.OnEvent("order.*", func(ctx microservices.Ctx) {
fmt.Println("User Event Data:", ctx.Payload(&Order{}))
})

return handler
}

appModule := func() core.Module {
module := core.NewModule(core.NewModuleOptions{
Providers: []core.Providers{
appService,
},
})
return module
}
app := nats.New(appModule, microservices.ConnectOptions{
Addr: "localhost:3010",
})

go app.Listen()

productApp := ProductApp("localhost:3010")
testProductServer := httptest.NewServer(productApp.PrepareBeforeListen())
defer testProductServer.Close()

testClientProduct := testProductServer.Client()

resp, err := testClientProduct.Post(testProductServer.URL+"/product-api/products", "application/json", nil)
time.Sleep(100 * time.Millisecond)

require.Nil(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
}

func Benchmark_Practice(b *testing.B) {
orderApp := OrderApp()
orderApp.ConnectMicroservice(nats.Open(microservices.ConnectOptions{
Expand Down
60 changes: 0 additions & 60 deletions microservices/redis/client.go

This file was deleted.

159 changes: 159 additions & 0 deletions microservices/redis/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package redis

import (
"context"
"encoding/json"
"strings"

redis_store "github.com/redis/go-redis/v9"
"github.com/tinh-tinh/tinhtinh/v2/core"
"github.com/tinh-tinh/tinhtinh/v2/microservices"
)

type Connect struct {
Context context.Context
Module core.Module
serializer core.Encode
deserializer core.Decode
Conn *redis_store.Client
}

// Client usage
func NewClient(opt microservices.ConnectOptions) microservices.ClientProxy {
conn := redis_store.NewClient(&redis_store.Options{
Addr: opt.Addr,
Password: "", // no password set
DB: 0, // use default DB
})

connect := &Connect{
Context: context.Background(),
Conn: conn,
serializer: json.Marshal,
deserializer: json.Unmarshal,
}
if opt.Deserializer != nil {
connect.deserializer = opt.Deserializer
}
if opt.Serializer != nil {
connect.serializer = opt.Serializer
}

return connect
}

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

func (c *Connect) Send(event string, data interface{}) error {
payload, err := c.Serializer(data)
if err != nil {
return err
}
err = c.Conn.Publish(c.Context, event, payload).Err()
if err != nil {
return err
}
return nil
}

func (client *Connect) Broadcast(data interface{}) error {
return client.Send("*", data)
}

// Server usage
func New(module core.ModuleParam, opts ...microservices.ConnectOptions) microservices.Service {
connect := &Connect{
Module: module(),
serializer: json.Marshal,
deserializer: json.Unmarshal,
}

if len(opts) > 0 {
if opts[0].Serializer != nil {
connect.serializer = opts[0].Serializer
}

if opts[0].Deserializer != nil {
connect.deserializer = opts[0].Deserializer
}
if opts[0].Addr != "" {
conn := redis_store.NewClient(&redis_store.Options{
Addr: opts[0].Addr,
Password: "", // no password set
DB: 0, // use default DB
})
connect.Conn = conn
}
}

return connect
}

func Open(opts ...microservices.ConnectOptions) core.Service {
connect := &Connect{
serializer: json.Marshal,
deserializer: json.Unmarshal,
Context: context.Background(),
}

if len(opts) > 0 {
if opts[0].Serializer != nil {
connect.serializer = opts[0].Serializer
}

if opts[0].Deserializer != nil {
connect.deserializer = opts[0].Deserializer
}

if opts[0].Addr != "" {
conn := redis_store.NewClient(&redis_store.Options{
Addr: opts[0].Addr,
Password: "", // no password set
DB: 0, // use default DB
})
connect.Conn = conn
}
}

return connect
}

func (c *Connect) Create(module core.Module) {
c.Module = module
}

func (c *Connect) Listen() {
for _, prd := range c.Module.GetDataProviders() {
var subscriber *redis_store.PubSub
if strings.HasSuffix(string(prd.GetName()), "*") {
subscriber = c.Conn.PSubscribe(c.Context, string(prd.GetName()))
} else {
subscriber = c.Conn.Subscribe(c.Context, string(prd.GetName()))
}
go c.Handler(subscriber, prd.GetFactory())
}
}

func (c *Connect) Handler(params ...interface{}) {
subscriber := params[0].(*redis_store.PubSub)
factory := params[1].(core.Factory)
for {
msg, err := subscriber.ReceiveMessage(c.Context)
if err != nil {
return
}

data := microservices.ParseCtx(msg.Payload, c)
factory(data)
}
}

func (c *Connect) Serializer(v interface{}) ([]byte, error) {
return c.serializer(v)
}

func (c *Connect) Deserializer(data []byte, v interface{}) error {
return c.deserializer(data, v)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package redis_test

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -42,16 +41,15 @@ func OrderApp() *core.App {
handlerService := func(module core.Module) core.Provider {
handler := microservices.NewHandler(module, core.ProviderOptions{})

// orderService := module.Ref(ORDER).(*OrderService)
orderService := module.Ref(ORDER).(*OrderService)
handler.OnResponse("order.created", func(ctx microservices.Ctx) {
data := ctx.Payload(&Order{}).(*Order)

// orderService.mutex.Lock()
// if orderService.orders[data.ID] == nil {
// orderService.orders[data.ID] = true
// }
// orderService.mutex.Unlock()
fmt.Println(data)
orderService.mutex.Lock()
if orderService.orders[data.ID] == nil {
orderService.orders[data.ID] = true
}
orderService.mutex.Unlock()
})

return handler
Expand Down
Loading
Loading