forked from mtusi2026/mtusi2026lab7
114 lines
3.6 KiB
Go
114 lines
3.6 KiB
Go
package queue
|
|
|
|
import (
|
|
"backend/internal/domain/model"
|
|
"backend/internal/domain/port"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"github.com/redis/go-redis/v9"
|
|
"net"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type commandHook struct {
|
|
role string
|
|
mu sync.Mutex
|
|
commands []string
|
|
t *testing.T
|
|
}
|
|
|
|
func (h *commandHook) DialHook(redis.DialHook) redis.DialHook {
|
|
return func(context.Context, string, string) (net.Conn, error) {
|
|
return nil, errors.New("test forbids network")
|
|
}
|
|
}
|
|
func (h *commandHook) ProcessHook(redis.ProcessHook) redis.ProcessHook {
|
|
return func(_ context.Context, c redis.Cmder) error {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
name := c.Name()
|
|
if h.role == "publisher" && name != "xadd" || h.role == "consumer" && name != "xack" && name != "xdel" {
|
|
h.t.Errorf("%s principal used forbidden command %s", h.role, name)
|
|
}
|
|
h.commands = append(h.commands, name)
|
|
switch v := c.(type) {
|
|
case *redis.StringCmd:
|
|
v.SetVal("1-0")
|
|
case *redis.IntCmd:
|
|
v.SetVal(1)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
func (h *commandHook) ProcessPipelineHook(redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
|
return func(context.Context, []redis.Cmder) error { return errors.New("test forbids implicit pipeline") }
|
|
}
|
|
|
|
type publishStore struct {
|
|
port.PipelineStore
|
|
j model.Job
|
|
published bool
|
|
}
|
|
|
|
func (s *publishStore) PendingJobs(context.Context) ([]model.Job, error) {
|
|
return []model.Job{s.j}, nil
|
|
}
|
|
func (s *publishStore) Published(_ context.Context, id uuid.UUID) error {
|
|
s.published = id == s.j.ID
|
|
return nil
|
|
}
|
|
func TestPublisherAndConsumerCommandsUseSeparatePrincipals(t *testing.T) {
|
|
p := redis.NewClient(&redis.Options{Addr: "127.0.0.1:1"})
|
|
defer closeRedis(t, p)
|
|
c := redis.NewClient(&redis.Options{Addr: "127.0.0.1:1"})
|
|
defer closeRedis(t, c)
|
|
ph, ch := &commandHook{role: "publisher", t: t}, &commandHook{role: "consumer", t: t}
|
|
p.AddHook(ph)
|
|
c.AddHook(ch)
|
|
j := model.Job{ID: uuid.New(), AttachmentID: uuid.New(), PipelineVersion: 1, Attempt: 1, CreatedAt: time.Now()}
|
|
s := &publishStore{j: j}
|
|
b := New(p, c, s, "synthetic")
|
|
if e := b.Publish(context.Background()); e != nil || !s.published {
|
|
t.Fatal("outbox publish did not complete")
|
|
}
|
|
raw, _ := json.Marshal(struct {
|
|
Schema int `json:"schema_version"`
|
|
Type string `json:"type"`
|
|
model.Job
|
|
}{1, "attachment.process.v1", j})
|
|
processed := false
|
|
b.consume(context.Background(), redis.XMessage{ID: "1-0", Values: map[string]any{"job": string(raw)}}, func(context.Context, model.Job) error { processed = true; return nil }, func(error) { t.Fatal("consumer processing failed") })
|
|
if !processed || len(ph.commands) != 1 || len(ch.commands) != 2 {
|
|
t.Fatal("publication and acknowledgement were not routed independently")
|
|
}
|
|
}
|
|
func TestSeparateClientConfigurationFailsClosed(t *testing.T) {
|
|
for _, pair := range [][2]string{{"", "redis://consumer:p@127.0.0.1:1"}, {"redis://same:p@127.0.0.1:1", "redis://same:q@127.0.0.1:1"}, {"redis://:p@127.0.0.1:1", "redis://consumer:p@127.0.0.1:1"}} {
|
|
if p, c, e := Clients(pair[0], pair[1]); e == nil {
|
|
closeRedis(t, p)
|
|
closeRedis(t, c)
|
|
t.Fatal("missing or shared identity accepted")
|
|
}
|
|
}
|
|
p, c, e := Clients("redis://publisher:synthetic@127.0.0.1:1", "redis://consumer:synthetic@127.0.0.1:1")
|
|
if e != nil {
|
|
t.Fatal("separate identity configuration rejected")
|
|
}
|
|
defer closeRedis(t, p)
|
|
defer closeRedis(t, c)
|
|
if p.Options().Username != "publisher" || c.Options().Username != "consumer" {
|
|
t.Fatal("actual clients lost principal names")
|
|
}
|
|
}
|
|
|
|
func closeRedis(t *testing.T, c *redis.Client) {
|
|
t.Helper()
|
|
if e := c.Close(); e != nil {
|
|
t.Error("client cleanup failed")
|
|
}
|
|
}
|