Skip to content

Commit

Permalink
Merge pull request #140 from tinh-tinh/test/ren/add-unit-test-v1.4.0
Browse files Browse the repository at this point in the history
test: add performance test in case tenant app
  • Loading branch information
Ren0503 authored Dec 4, 2024
2 parents fb8c133 + 1103c34 commit bf9f9b7
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions core/tenant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package core_test

import (
"encoding/json"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"strconv"
"sync"
"testing"

"github.com/stretchr/testify/require"
"github.com/tinh-tinh/tinhtinh/core"
)

func BenchmarkTenant(b *testing.B) {
const (
TENANCY core.Provide = "TENANCY"
MAPPER core.Provide = "MAPPER"
)

type mapper map[string]interface{}
var mutex = sync.RWMutex{}

forRoot := func(module *core.DynamicModule) *core.DynamicModule {
tenantModule := module.New(core.NewModuleOptions{})

tenantModule.NewProvider(core.ProviderOptions{
Name: MAPPER,
Value: make(mapper),
})
tenantModule.Export(MAPPER)

tenantModule.NewProvider(core.ProviderOptions{
Scope: core.Request,
Name: TENANCY,
Factory: func(param ...interface{}) interface{} {
req := param[0].(*http.Request)
tenantID := req.Header.Get("x-tenant")
if tenantID == "" {
tenantID = "master"
}
mutex.RLock()
mapper, ok := param[1].(mapper)
mutex.RUnlock()
if !ok {
return nil
}

if mapper[tenantID] == nil {
mutex.Lock()
mapper[tenantID] = tenantID
mutex.Unlock()
}
return mapper[tenantID]
},
Inject: []core.Provide{core.REQUEST, MAPPER},
})
tenantModule.Export(TENANCY)

return tenantModule
}

userController := func(module *core.DynamicModule) *core.DynamicController {
ctrl := module.NewController("user")

ctrl.Get("", func(ctx core.Ctx) error {
return ctx.JSON(core.Map{
"data": ctx.Ref(TENANCY),
})
})

return ctrl
}

userModule := func(module *core.DynamicModule) *core.DynamicModule {
user := module.New(core.NewModuleOptions{
Controllers: []core.Controller{userController},
})

return user
}

appModule := func() *core.DynamicModule {
app := core.NewModule(core.NewModuleOptions{
Imports: []core.Module{forRoot, userModule},
})

return app
}

app := core.CreateFactory(appModule)
app.SetGlobalPrefix("/api")

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

b.RunParallel(func(p *testing.PB) {
for p.Next() {
req, err := http.NewRequest("GET", testServer.URL+"/api/user", nil)
require.Nil(b, err)
abc := rand.Intn(100)
id := strconv.Itoa(abc)
req.Header.Set("x-tenant", "test"+id)
resp, err := testClient.Do(req)
require.Nil(b, err)
require.Equal(b, http.StatusOK, resp.StatusCode)

data, err := io.ReadAll(resp.Body)
require.Nil(b, err)

var res Response
err = json.Unmarshal(data, &res)
require.Nil(b, err)
require.Equal(b, "test"+id, res.Data)
}
})
}

0 comments on commit bf9f9b7

Please sign in to comment.