Files
ratatoskr-go/internal/chat/fake_channel_test.go
2026-08-14 20:33:04 +05:00

66 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package chat
import (
"context"
"sync"
)
// fakeChannel — тестовый канал: захватывает исходящие и умеет эмулировать
// входящие события. Не запускает реальный цикл.
type fakeChannel struct {
mu sync.Mutex
handler Handler
sent []SendCall
addr Address
runErr error
}
type SendCall struct {
To Address
Msg Message
}
func newFakeChannel(addr Address) *fakeChannel {
return &fakeChannel{addr: addr}
}
func (f *fakeChannel) OnMessage(h Handler) {
f.mu.Lock()
defer f.mu.Unlock()
f.handler = h
}
func (f *fakeChannel) Run(ctx context.Context) error { return f.runErr }
func (f *fakeChannel) Send(_ context.Context, to Address, m Message) error {
f.mu.Lock()
defer f.mu.Unlock()
f.sent = append(f.sent, SendCall{To: to, Msg: m})
return nil
}
func (f *fakeChannel) Ask(_ context.Context, to Address, m Message) error {
f.mu.Lock()
defer f.mu.Unlock()
f.sent = append(f.sent, SendCall{To: to, Msg: m})
return nil
}
func (f *fakeChannel) Close() error { return nil }
// emit доставляет входящее через зарегистрированный handler.
func (f *fakeChannel) emit(uid UserID, addr Address, text string) {
f.mu.Lock()
h := f.handler
f.mu.Unlock()
if h != nil {
h(Incoming{UserID: uid, Address: addr, Msg: Message{Text: text}, Channel: f})
}
}
func (f *fakeChannel) sentCount() int {
f.mu.Lock()
defer f.mu.Unlock()
return len(f.sent)
}