Files
remark42/backend/app/notify/notify_mock.go
T

44 lines
810 B
Go

package notify
import (
"context"
"fmt"
"sync"
"time"
log "github.com/go-pkgz/lgr"
)
// MockDest is a destination mock
type MockDest struct {
data []Request
id int
closed bool
lock sync.Mutex
}
// Send mock
func (m *MockDest) Send(ctx context.Context, r Request) error {
m.lock.Lock()
defer m.lock.Unlock()
select {
case <-time.After(10 * time.Millisecond):
m.data = append(m.data, r)
log.Printf("sent %s -> %d", r.Comment.ID, m.id)
case <-ctx.Done():
log.Printf("ctx closed %d", m.id)
m.closed = true
}
return nil
}
// Get mock
func (m *MockDest) Get() []Request {
m.lock.Lock()
defer m.lock.Unlock()
res := make([]Request, len(m.data))
copy(res, m.data)
return res
}
func (m *MockDest) String() string { return fmt.Sprintf("mock id=%d, closed=%v", m.id, m.closed) }