You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
290 lines
8.6 KiB
290 lines
8.6 KiB
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.lowcodeplatform.net/fabric/lib"
|
|
"git.lowcodeplatform.net/packages/logger"
|
|
)
|
|
|
|
var config struct {
|
|
UrlApi string `envconfig:"URL_API" default:""`
|
|
ApiCacheTTL time.Duration `envconfig:"URL_API" default:""`
|
|
|
|
// API-client CircuitBreaker
|
|
CbMaxRequests uint32 `envconfig:"CB_MAX_REQUESTS" default:"3" description:"максимальное количество запросов, которые могут пройти, когда автоматический выключатель находится в полуразомкнутом состоянии"`
|
|
CbTimeout time.Duration `envconfig:"CB_TIMEOUT" default:"5s" description:"период разомкнутого состояния, после которого выключатель переходит в полуразомкнутое состояние"`
|
|
CbInterval time.Duration `envconfig:"CB_INTERVAL" default:"5s" description:"циклический период замкнутого состояния автоматического выключателя для сброса внутренних счетчиков"`
|
|
|
|
// LOGBOX
|
|
LogboxEndpoint string `envconfig:"LOGBOX_ENDPOINT" default:"http://127.0.0.1:8999"`
|
|
LogboxAccessKeyId string `envconfig:"LOGBOX_ACCESS_KEY_ID" default:""`
|
|
LogboxSecretKey string `envconfig:"LOGBOX_SECRET_KEY" default:""`
|
|
LogboxRequestTimeout time.Duration `envconnfig:"LOGBOX_REQUEST_TIMEOUT" default:"300ms"`
|
|
|
|
// LOGBOX-client CircuitBreaker
|
|
CbMaxRequestsLogbox uint32 `envconfig:"CB_MAX_REQUESTS_LOGBOX" default:"3" description:"максимальное количество запросов, которые могут пройти, когда автоматический выключатель находится в полуразомкнутом состоянии"`
|
|
CbTimeoutLogbox time.Duration `envconfig:"CB_TIMEOUT_LOGBOX" default:"5s" description:"период разомкнутого состояния, после которого выключатель переходит в полуразомкнутое состояние"`
|
|
CbIntervalLogbox time.Duration `envconfig:"CB_INTERVAL_LOGBOX" default:"5s" description:"циклический период замкнутого состояния автоматического выключателя для сброса внутренних счетчиков"`
|
|
}
|
|
|
|
func Test_ObjGetWithCache(t *testing.T) {
|
|
var err error
|
|
ctx := context.Background()
|
|
uids := "ddfe549f-4a13-92fb-23a7-ee030f60174d"
|
|
|
|
cfg := config
|
|
cfg.UrlApi = "https://lms.wb.ru/lms/api"
|
|
cfg.CbMaxRequests = 3
|
|
cfg.CbTimeout = 5 * time.Second
|
|
cfg.CbInterval = 5 * time.Second
|
|
cfg.ApiCacheTTL = 5 * time.Second
|
|
cfg.LogboxEndpoint = "http://127.0.0.1:8999"
|
|
cfg.CbMaxRequestsLogbox = 3
|
|
cfg.CbTimeoutLogbox = 5 * time.Second
|
|
cfg.CbIntervalLogbox = 5 * time.Second
|
|
|
|
err = logger.SetupDefaultLogboxLogger("api/client", logger.LogboxConfig{
|
|
Endpoint: cfg.LogboxEndpoint,
|
|
AccessKeyID: cfg.LogboxAccessKeyId,
|
|
SecretKey: cfg.LogboxSecretKey,
|
|
RequestTimeout: cfg.LogboxRequestTimeout,
|
|
CbMaxRequests: cfg.CbMaxRequestsLogbox,
|
|
CbTimeout: cfg.CbTimeoutLogbox,
|
|
CbInterval: cfg.CbIntervalLogbox,
|
|
}, map[string]string{
|
|
logger.ServiceIDKey: lib.Hash(lib.UUID()),
|
|
logger.ConfigIDKey: "api",
|
|
logger.ServiceTypeKey: "api",
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println("error init logger")
|
|
}
|
|
|
|
apiClient := New(
|
|
ctx,
|
|
cfg.UrlApi,
|
|
true,
|
|
cfg.ApiCacheTTL,
|
|
cfg.CbMaxRequests,
|
|
cfg.CbTimeout,
|
|
cfg.CbInterval,
|
|
"jkkdkdk",
|
|
)
|
|
|
|
fmt.Println("time get from api - start")
|
|
|
|
t1 := time.Now()
|
|
res, err := apiClient.ObjGetWithCache(ctx, uids)
|
|
|
|
fmt.Println("time get from api", time.Since(t1), len(res.Data))
|
|
|
|
t2 := time.Now()
|
|
res2, err := apiClient.ObjGetWithCache(ctx, uids)
|
|
|
|
fmt.Println("time get from cache", time.Since(t2), len(res2.Data))
|
|
|
|
return
|
|
}
|
|
|
|
func Test_QueryWithCache(t *testing.T) {
|
|
var err error
|
|
ctx := context.Background()
|
|
query, method, bodyJSON := "sys_public_pages", "GET", ""
|
|
|
|
cfg := config
|
|
cfg.UrlApi = "https://lms.wb.ru/lms/api"
|
|
cfg.CbMaxRequests = 3
|
|
cfg.CbTimeout = 5 * time.Second
|
|
cfg.CbInterval = 5 * time.Second
|
|
cfg.ApiCacheTTL = 10 * time.Second
|
|
cfg.LogboxEndpoint = "http://127.0.0.1:8999"
|
|
cfg.CbMaxRequestsLogbox = 3
|
|
cfg.CbTimeoutLogbox = 5 * time.Second
|
|
cfg.CbIntervalLogbox = 5 * time.Second
|
|
|
|
err = logger.SetupDefaultLogboxLogger("api/client", logger.LogboxConfig{
|
|
Endpoint: cfg.LogboxEndpoint,
|
|
AccessKeyID: cfg.LogboxAccessKeyId,
|
|
SecretKey: cfg.LogboxSecretKey,
|
|
RequestTimeout: cfg.LogboxRequestTimeout,
|
|
CbMaxRequests: cfg.CbMaxRequestsLogbox,
|
|
CbTimeout: cfg.CbTimeoutLogbox,
|
|
CbInterval: cfg.CbIntervalLogbox,
|
|
}, map[string]string{
|
|
logger.ServiceIDKey: lib.Hash(lib.UUID()),
|
|
logger.ConfigIDKey: "api",
|
|
logger.ServiceTypeKey: "api",
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println("error init logger")
|
|
}
|
|
|
|
apiClient := New(
|
|
ctx,
|
|
cfg.UrlApi,
|
|
true,
|
|
cfg.ApiCacheTTL,
|
|
cfg.CbMaxRequests,
|
|
cfg.CbTimeout,
|
|
cfg.CbInterval,
|
|
"jsdfkdsvk",
|
|
)
|
|
|
|
t1 := time.Now()
|
|
res, err := apiClient.QueryWithCache(ctx, query, method, bodyJSON)
|
|
|
|
fmt.Println("time QueryWithCache from api", time.Since(t1), len(res))
|
|
|
|
t2 := time.Now()
|
|
res2, err := apiClient.QueryWithCache(ctx, query, method, bodyJSON)
|
|
|
|
fmt.Println("time QueryWithCache from cache", time.Since(t2), len(res2))
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
t3 := time.Now()
|
|
res, err = apiClient.QueryWithCache(ctx, query, method, bodyJSON)
|
|
|
|
fmt.Println("time QueryWithCache from api", time.Since(t3), len(res))
|
|
|
|
t4 := time.Now()
|
|
res2, err = apiClient.QueryWithCache(ctx, query, method, bodyJSON)
|
|
|
|
fmt.Println("time QueryWithCache from cache", time.Since(t4), len(res2))
|
|
|
|
return
|
|
}
|
|
|
|
func TestApi_Search(t *testing.T) {
|
|
var err error
|
|
ctx := context.Background()
|
|
query, method,
|
|
bodyJSON := "", "POST", `{
|
|
"name": "",
|
|
"tpls": "2022-12-25T12-17-25Z-96a1d4",
|
|
"type": "",
|
|
"after_uid": "",
|
|
"limit": 0,
|
|
"after_rev_include_border": "",
|
|
"offset": 0,
|
|
"filter_field": "",
|
|
"filter_value": "",
|
|
"filter_src": "",
|
|
"short": "true"
|
|
}
|
|
`
|
|
|
|
cfg := config
|
|
cfg.UrlApi = "http://127.0.0.1:8027"
|
|
cfg.CbMaxRequests = 3
|
|
cfg.CbTimeout = 5 * time.Second
|
|
cfg.CbInterval = 5 * time.Second
|
|
cfg.ApiCacheTTL = 10 * time.Second
|
|
cfg.LogboxEndpoint = "http://127.0.0.1:8999"
|
|
|
|
cfg.CbMaxRequestsLogbox = 3
|
|
cfg.CbTimeoutLogbox = 5 * time.Second
|
|
cfg.CbIntervalLogbox = 5 * time.Second
|
|
|
|
err = logger.SetupDefaultLogboxLogger("api/client", logger.LogboxConfig{
|
|
Endpoint: cfg.LogboxEndpoint,
|
|
AccessKeyID: cfg.LogboxAccessKeyId,
|
|
SecretKey: cfg.LogboxSecretKey,
|
|
RequestTimeout: cfg.LogboxRequestTimeout,
|
|
CbMaxRequests: cfg.CbMaxRequestsLogbox,
|
|
CbTimeout: cfg.CbTimeoutLogbox,
|
|
CbInterval: cfg.CbIntervalLogbox,
|
|
}, map[string]string{
|
|
logger.ServiceIDKey: lib.Hash(lib.UUID()),
|
|
logger.ConfigIDKey: "api",
|
|
logger.ServiceTypeKey: "api",
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println("error init logger")
|
|
}
|
|
|
|
apiClient := New(
|
|
ctx,
|
|
cfg.UrlApi,
|
|
true,
|
|
cfg.ApiCacheTTL,
|
|
cfg.CbMaxRequests,
|
|
cfg.CbTimeout,
|
|
cfg.CbInterval,
|
|
"LKHlhb899Y09olUi",
|
|
)
|
|
|
|
t1 := time.Now()
|
|
|
|
resp, err := apiClient.Search(ctx, query, method, bodyJSON)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Println("time Search from api", time.Since(t1), len(resp))
|
|
|
|
return
|
|
}
|
|
|
|
func Test_ObjAdd(t *testing.T) {
|
|
var err error
|
|
ctx := context.Background()
|
|
uids := "2019-12-25T14-27-21Z-ab85d7"
|
|
element := "access_admin"
|
|
to := "7929d995-3206-d127-fa4a-1e6950eaaa22"
|
|
|
|
cfg := config
|
|
cfg.UrlApi = "https://lms.wb.ru/lms/api"
|
|
cfg.CbMaxRequests = 3
|
|
cfg.CbTimeout = 5 * time.Second
|
|
cfg.CbInterval = 5 * time.Second
|
|
cfg.ApiCacheTTL = 5 * time.Second
|
|
cfg.LogboxEndpoint = "127.0.0.1:8999"
|
|
cfg.CbMaxRequestsLogbox = 3
|
|
cfg.CbTimeoutLogbox = 5 * time.Second
|
|
cfg.CbIntervalLogbox = 5 * time.Second
|
|
|
|
err = logger.SetupDefaultLogboxLogger("api/client", logger.LogboxConfig{
|
|
Endpoint: cfg.LogboxEndpoint,
|
|
AccessKeyID: cfg.LogboxAccessKeyId,
|
|
SecretKey: cfg.LogboxSecretKey,
|
|
RequestTimeout: cfg.LogboxRequestTimeout,
|
|
CbMaxRequests: cfg.CbMaxRequestsLogbox,
|
|
CbTimeout: cfg.CbTimeoutLogbox,
|
|
CbInterval: cfg.CbIntervalLogbox,
|
|
}, map[string]string{
|
|
logger.ServiceIDKey: lib.Hash(lib.UUID()),
|
|
logger.ConfigIDKey: "api",
|
|
logger.ServiceTypeKey: "api",
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println("error init logger")
|
|
}
|
|
|
|
apiClient := New(
|
|
ctx,
|
|
cfg.UrlApi,
|
|
true,
|
|
cfg.ApiCacheTTL,
|
|
cfg.CbMaxRequests,
|
|
cfg.CbTimeout,
|
|
cfg.CbInterval,
|
|
"LKHlhb899Y09olUi",
|
|
)
|
|
|
|
fmt.Println("1. добавление связи")
|
|
res, err := apiClient.LinkAdd(ctx, uids, element, to)
|
|
|
|
fmt.Println(res, err)
|
|
|
|
return
|
|
}
|
|
|