123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package application
- import (
- "context"
- "io"
- "log"
- "testing"
- "time"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- "git.ali33.ru/fcg-xvii/rest"
- )
- // Пустая реализация IStream
- type TestStream struct {
- ctx context.Context
- cancel context.CancelFunc
- }
- func NewTestStream() *TestStream {
- ctx, cancel := context.WithCancel(context.Background())
- return &TestStream{
- ctx: ctx,
- cancel: cancel,
- }
- }
- func (t *TestStream) SetAuth(auth json.Map) {}
- func (t *TestStream) Auth() json.Map { return nil }
- func (t *TestStream) SetClientData(key string, val any) {}
- func (t *TestStream) ClientData(key string) (any, bool) { return nil, false }
- func (t *TestStream) Context() context.Context {
- return t.ctx
- }
- func (t *TestStream) SendMessage(req rest.IRequestOut) (<-chan *rest.RequestStream, error) {
- log.Printf("TestStream %p send message.\n", t)
- return nil, nil
- }
- func (t *TestStream) Close() {
- t.cancel()
- log.Printf("TestStream %p closed.\n", t)
- }
- // Пустая реализация IRequestOut
- type TestRequestOut struct {
- TestRequest
- }
- func NewTestRequestOut() *TestRequestOut {
- return &TestRequestOut{
- TestRequest: TestRequest{},
- }
- }
- func (r *TestRequestOut) Write(w io.Writer) rest.IErrorArgs {
- // Пустая реализация записи
- return nil
- }
- // Пустая реализация IRequest
- type TestRequest struct{}
- func (r *TestRequest) RType() rest.RequestType {
- return 0 // Возвращаем значение по умолчанию
- }
- func (r *TestRequest) RCommand() string {
- return "" // Возвращаем пустую строку
- }
- func (r *TestRequest) RData() json.Map {
- return make(json.Map) // Возвращаем пустую карту
- }
- func (r *TestRequest) RFiles() rest.RequestFiles {
- return nil // Возвращаем nil
- }
- func (r *TestRequest) RFile(name string) (rest.IReadCloserLen, bool) {
- return nil, false // Возвращаем nil и false
- }
- func (r *TestRequest) RClose() {
- // Пустая реализация
- }
- ////////////////////////////////////////////////////////
- func TestStreamClose(t *testing.T) {
- stream := NewTestStream()
- store := NewStreamStore(stream, context.Background())
- go func() {
- <-store.Context().Done()
- log.Println("store closedddd...")
- store.Close()
- store.Close()
- }()
- stream.Close()
- time.Sleep(time.Second * 5)
- }
- func TestStreamStoreClose(t *testing.T) {
- stream := NewTestStream()
- store := NewStreamStore(stream, context.Background())
- store.Add(NewTestStream())
- store.Add(NewTestStream())
- store.Add(NewTestStream())
- store.Add(NewTestStream())
- store.Close()
- time.Sleep(time.Minute * 5)
- }
- func TestGroupClose(t *testing.T) {
- group := NewStreamGroup(context.Background())
- stream := NewTestStream()
- group.Store("1", stream)
- group.Close()
- time.Sleep(time.Minute * 5)
- }
- func TestGroupStreamClose(t *testing.T) {
- group := NewStreamGroup(context.Background())
- add := func() {
- stream := NewTestStream()
- group.Store("1", stream)
- go func() {
- time.Sleep(time.Second * 3)
- log.Println("remove", stream)
- stream.Close()
- }()
- log.Println("add", stream)
- }
- add()
- add()
- time.Sleep(time.Second * 10)
- }
- func TestGroupBroadcast(t *testing.T) {
- group := NewStreamGroup(context.Background())
- group.Store("1", NewTestStream())
- group.Store("12", NewTestStream())
- group.EventBroadcast(NewTestRequestOut())
- time.Sleep(time.Second * 10)
- }
|