Files
proton-bridge/internal/services/userevents/subscriber_test.go
Leander Beernaert 82efa16d65 feat(GODT-2800): User Event Service
This patch adds the User Event Service which is meant to replace the
current event polling flow.

Each user interested in receiving events should register a new
subscriber using the `Service.Subscribe` function and then react on
the incoming events.

The current patch does not hook this up Bridge user as there are no
existing consumers, but it does provide extensive testing for the
expected behavior.
2023-07-21 11:47:43 +02:00

106 lines
2.7 KiB
Go

// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package userevents
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestChanneledSubscriber_CtxTimeoutDoesNotBlockFutureEvents(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
subscriber := newChanneledSubscriber[int]("test")
defer subscriber.close()
go func() {
defer wg.Done()
// Send one event, that succeeds.
require.NoError(t, subscriber.handle(context.Background(), []int{30}))
// Add an impossible deadline that fails immediately to simulate on event taking too long to send.
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Microsecond))
defer cancel()
err := subscriber.handle(ctx, []int{20})
require.Error(t, err)
require.True(t, errors.Is(err, context.DeadlineExceeded))
}()
// Receive first event. Notify success.
event, ok := <-subscriber.OnEventCh()
require.True(t, ok)
event.Consume(func(event []int) error {
require.Equal(t, []int{30}, event)
return nil
})
wg.Wait()
// Simulate reception of another event
wg.Add(1)
go func() {
defer wg.Done()
require.NoError(t, subscriber.handle(context.Background(), []int{40}))
}()
event, ok = <-subscriber.OnEventCh()
require.True(t, ok)
event.Consume(func(event []int) error {
require.Equal(t, []int{40}, event)
return nil
})
wg.Wait()
}
func TestChanneledSubscriber_ErrorReported(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
subscriber := newChanneledSubscriber[int]("test")
defer subscriber.close()
reportedErr := fmt.Errorf("request failed")
go func() {
defer wg.Done()
// Send one event, that succeeds.
err := subscriber.handle(context.Background(), []int{30})
require.Error(t, err)
require.Equal(t, reportedErr, err)
}()
// Receive first event. Notify success.
event, ok := <-subscriber.OnEventCh()
require.True(t, ok)
event.Consume(func(event []int) error {
require.Equal(t, []int{30}, event)
return reportedErr
})
wg.Wait()
}