internal/chat: мультиканальный Router (UserID-сессии, routing, pending Ask/ответ) + Channel интерфейс
All checks were successful
build-test / build (push) Successful in 41s
All checks were successful
build-test / build (push) Successful in 41s
This commit is contained in:
65
internal/chat/fake_channel_test.go
Normal file
65
internal/chat/fake_channel_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user