Files
soft_quay/core/downloader/application_observer_test.go
T

103 lines
2.8 KiB
Go

package downloader
import (
"context"
"reflect"
"testing"
"softbox.local/core/application"
)
func TestApplicationObserverUsesConcretePayloads(t *testing.T) {
publisher := &recordingApplicationPublisher{}
observer := ApplicationObserver{Publisher: publisher}
tests := []struct {
event Event
wantType application.EventType
wantPayload any
}{
{
event: Event{
Type: EventStarted, RequestID: "request-1", AppID: "app-1",
Attempt: 1, Done: 2, TotalKnown: true, Total: 10,
},
wantType: application.EventDownloadStarted,
wantPayload: application.DownloadStartedPayload{
Attempt: 1, Done: 2, TotalKnown: true, Total: 10,
},
},
{
event: Event{
Type: EventProgress, RequestID: "request-1", AppID: "app-1",
Attempt: 1, Done: 4, TotalKnown: true, Total: 10, SpeedBytesSec: 2,
},
wantType: application.EventDownloadProgress,
wantPayload: application.DownloadProgressPayload{
Attempt: 1, Done: 4, TotalKnown: true, Total: 10, SpeedBytesSec: 2,
},
},
{
event: Event{
Type: EventPaused, RequestID: "request-1", AppID: "app-1",
Attempt: 1, Done: 4,
},
wantType: application.EventDownloadPaused,
wantPayload: application.DownloadPausedPayload{Attempt: 1, Done: 4},
},
{
event: Event{
Type: EventCompleted, RequestID: "request-1", AppID: "app-1",
Attempt: 1, Done: 10, CompletedPath: "download",
},
wantType: application.EventDownloadCompleted,
wantPayload: application.DownloadCompletedPayload{
Attempt: 1, Done: 10, Path: "download",
},
},
{
event: Event{
Type: EventFailed, RequestID: "request-1", AppID: "app-1",
Attempt: 1, Done: 4, ErrorCode: "http_status",
},
wantType: application.EventDownloadFailed,
wantPayload: application.DownloadFailedPayload{
Attempt: 1, Done: 4, ErrorCode: "http_status",
},
},
{
event: Event{
Type: EventCanceled, RequestID: "request-1", AppID: "app-1",
Attempt: 1,
},
wantType: application.EventDownloadCanceled,
wantPayload: application.DownloadCanceledPayload{Attempt: 1},
},
}
for _, test := range tests {
if err := observer.PublishDownload(context.Background(), test.event); err != nil {
t.Fatalf("PublishDownload(%s) error = %v", test.event.Type, err)
}
got := publisher.events[len(publisher.events)-1]
if got.Type != test.wantType ||
got.RequestID != test.event.RequestID ||
got.AppID != test.event.AppID {
t.Fatalf("event = %#v", got)
}
if !reflect.DeepEqual(got.Payload, test.wantPayload) {
t.Fatalf("payload = %#v, want %#v", got.Payload, test.wantPayload)
}
}
}
type recordingApplicationPublisher struct {
events []application.Event
}
func (publisher *recordingApplicationPublisher) Publish(
_ context.Context,
event application.Event,
) error {
publisher.events = append(publisher.events, event)
return nil
}