114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
package rolesync
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
)
|
|
|
|
func TestEnqueueRoleOnlyAfterCommit(t *testing.T) {
|
|
if err := gdb.SetConfig(gdb.Config{"default": {{Type: "sqlite", Link: "sqlite::@file(:memory:)"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
t.Cleanup(func() { _ = g.DB().Close(ctx) })
|
|
for _, statement := range []string{
|
|
"CREATE TABLE sys_role (role_id INTEGER PRIMARY KEY, tenant_id TEXT, dept_id INTEGER, is_pass INTEGER, status TEXT DEFAULT '0', data_scope TEXT DEFAULT '4', created_dept INTEGER DEFAULT 100, deleted_at DATETIME)",
|
|
"CREATE TABLE sys_config (tenant_id TEXT, config_key TEXT, config_value TEXT)",
|
|
"CREATE TABLE sys_dept (dept_id INTEGER PRIMARY KEY, tenant_id TEXT, status TEXT)",
|
|
"CREATE TABLE sys_role_menu (tenant_id TEXT, role_id INTEGER, menu_id INTEGER, data_scope TEXT)",
|
|
"INSERT INTO sys_dept VALUES(100,'000000','0')",
|
|
"INSERT INTO sys_role(role_id,tenant_id,dept_id,is_pass) VALUES (18,'000000',100,1),(19,'000000',100,0)",
|
|
} {
|
|
if _, err := g.DB().Exec(ctx, statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
original := publishRoleSync
|
|
t.Cleanup(func() { publishRoleSync = original })
|
|
var messages []*model.AgentRoleSyncMessage
|
|
publishRoleSync = func(_ context.Context, message *model.AgentRoleSyncMessage) error {
|
|
messages = append(messages, message)
|
|
return nil
|
|
}
|
|
if err := g.DB().Transaction(ctx, func(txCtx context.Context, _ gdb.TX) error {
|
|
return EnqueueRole(txCtx, 18, 7)
|
|
}); err == nil {
|
|
t.Fatal("must reject publishing before commit")
|
|
}
|
|
if len(messages) != 0 {
|
|
t.Fatal("published inside transaction")
|
|
}
|
|
if err := EnqueueRole(ctx, 18, 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(messages) != 1 || messages[0].TaskId == "" || messages[0].SourceRoleId != 18 {
|
|
t.Fatalf("unexpected message: %+v", messages)
|
|
}
|
|
if err := EnqueueRole(DeferRecording(ctx), 18, 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := EnqueueRole(ctx, 19, 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(messages) != 1 {
|
|
t.Fatal("non-inheritable or deferred role published")
|
|
}
|
|
publishRoleSync = func(context.Context, *model.AgentRoleSyncMessage) error { return errors.New("broker offline") }
|
|
if err := EnqueueRole(ctx, 18, 7); err == nil || !strings.Contains(err.Error(), "角色已保存") {
|
|
t.Fatalf("must report committed save and failed publication: %v", err)
|
|
}
|
|
publishRoleSync = func(_ context.Context, message *model.AgentRoleSyncMessage) error {
|
|
messages = append(messages, message)
|
|
return nil
|
|
}
|
|
if _, err := g.DB().Exec(ctx, "INSERT INTO sys_config VALUES('000000','agent.role.template.role_id','19')"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := EnqueueRole(ctx, 18, 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(messages) != 1 {
|
|
t.Fatal("legacy local role must not overwrite configured hierarchy")
|
|
}
|
|
if err := EnqueueRole(ctx, 19, 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(messages) != 2 || !messages[1].TemplateRole || messages[1].SourceRoleId != 19 {
|
|
t.Fatalf("configured template with is_pass=0 was not queued: %+v", messages)
|
|
}
|
|
t.Run("提交后请求取消不丢失下发且保留有限超时", func(t *testing.T) {
|
|
type requestKey struct{}
|
|
requestCtx, cancel := context.WithCancel(context.WithValue(ctx, requestKey{}, "actor"))
|
|
cancel()
|
|
published := false
|
|
publishRoleSync = func(publishCtx context.Context, message *model.AgentRoleSyncMessage) error {
|
|
if err := publishCtx.Err(); err != nil {
|
|
t.Fatalf("committed publication inherited cancellation: %v", err)
|
|
}
|
|
deadline, ok := publishCtx.Deadline()
|
|
if !ok || time.Until(deadline) <= 0 || time.Until(deadline) > 15*time.Second {
|
|
t.Fatalf("publication must have bounded timeout: %v %v", deadline, ok)
|
|
}
|
|
if publishCtx.Value(requestKey{}) != "actor" || message.ActorId != 7 {
|
|
t.Fatal("request values or actor lost")
|
|
}
|
|
published = true
|
|
return nil
|
|
}
|
|
if err := EnqueueRole(requestCtx, 19, 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !published {
|
|
t.Fatal("committed role was not published")
|
|
}
|
|
})
|
|
}
|