From d802345e889f0dd748b5bc1754b65516e31d8a8d Mon Sep 17 00:00:00 2001 From: Kegan Dougal <7190048+kegsay@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:48:38 +0000 Subject: [PATCH] Add generic state listener --- internal/api/client.go | 10 ++++---- internal/api/js.go | 2 +- internal/api/rust.go | 54 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/internal/api/client.go b/internal/api/client.go index 5d69d96..71df69f 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -45,8 +45,8 @@ type Client interface { MustBackpaginate(t *testing.T, roomID string, count int) // MustGetEvent will return the client's view of this event, or fail the test if the event cannot be found. MustGetEvent(t *testing.T, roomID, eventID string) Event - // MustBackupKeys will backup E2EE keys using the password provided, else fail the test. - MustBackupKeys(t *testing.T, password string) + // MustBackupKeys will backup E2EE keys, else fail the test. + MustBackupKeys(t *testing.T) // Log something to stdout and the underlying client log file Logf(t *testing.T, format string, args ...interface{}) // The user for this client @@ -98,10 +98,10 @@ func (c *LoggedClient) MustBackpaginate(t *testing.T, roomID string, count int) c.Client.MustBackpaginate(t, roomID, count) } -func (c *LoggedClient) MustBackupKeys(t *testing.T, password string) { +func (c *LoggedClient) MustBackupKeys(t *testing.T) { t.Helper() - c.Logf(t, "%s MustBackupKeys password=%s", c.logPrefix(), password) - c.Client.MustBackupKeys(t, password) + c.Logf(t, "%s MustBackupKeys", c.logPrefix()) + c.Client.MustBackupKeys(t) } func (c *LoggedClient) logPrefix() string { diff --git a/internal/api/js.go b/internal/api/js.go index c839c55..a7137d8 100644 --- a/internal/api/js.go +++ b/internal/api/js.go @@ -298,7 +298,7 @@ func (c *JSClient) MustBackpaginate(t *testing.T, roomID string, count int) { )) } -func (c *JSClient) MustBackupKeys(t *testing.T, password string) { +func (c *JSClient) MustBackupKeys(t *testing.T) { } diff --git a/internal/api/rust.go b/internal/api/rust.go index 7053ba1..36da2c9 100644 --- a/internal/api/rust.go +++ b/internal/api/rust.go @@ -92,12 +92,10 @@ func (c *RustClient) StartSyncing(t *testing.T) (stopSyncing func()) { t.Helper() syncService, err := c.FFIClient.SyncService().Finish() must.NotError(t, fmt.Sprintf("[%s]failed to make sync service", c.userID), err) - ch := make(chan matrix_sdk_ffi.RoomListLoadingState, 10) roomList, err := syncService.RoomListService().AllRooms() must.NotError(t, "failed to call SyncService.RoomListService.AllRooms", err) - result, err := roomList.LoadingState(&roomListLoadingStateListener{ - ch: ch, - }) + genericListener := newGenericStateListener[matrix_sdk_ffi.RoomListLoadingState]() + result, err := roomList.LoadingState(genericListener) must.NotError(t, "failed to call RoomList.LoadingState", err) go syncService.Start() c.allRooms = roomList @@ -108,7 +106,7 @@ func (c *RustClient) StartSyncing(t *testing.T) (stopSyncing func()) { select { case <-time.After(5 * time.Second): fatalf(t, "[%s](rust) timed out after 5s StartSyncing", c.userID) - case state := <-ch: + case state := <-genericListener.ch: fmt.Println(state) switch state.(type) { case matrix_sdk_ffi.RoomListLoadingStateLoaded: @@ -118,6 +116,7 @@ func (c *RustClient) StartSyncing(t *testing.T) (stopSyncing func()) { } } } + genericListener.Close() result.StateStream.Cancel() @@ -139,7 +138,27 @@ func (c *RustClient) IsRoomEncrypted(t *testing.T, roomID string) (bool, error) return r.IsEncrypted() } -func (c *RustClient) MustBackupKeys(t *testing.T, password string) { +func (c *RustClient) MustBackupKeys(t *testing.T) { + // no-op, ffi does this by default + /* + t.Helper() + must.NotError(t, "failed to EnableBackups", c.FFIClient.Encryption().EnableBackups()) + genericListener := newGenericStateListener[matrix_sdk_ffi.BackupUploadState]() + var listener matrix_sdk_ffi.BackupSteadyStateListener = genericListener + must.NotError(t, "failed to WaitForBackupUploadSteadyState", c.FFIClient.Encryption().WaitForBackupUploadSteadyState(&listener)) + for s := range genericListener.ch { + switch x := s.(type) { + case matrix_sdk_ffi.BackupUploadStateWaiting: + c.Logf(t, "MustBackupKeys: state=waiting") + case matrix_sdk_ffi.BackupUploadStateUploading: + c.Logf(t, "MustBackupKeys: state=uploading %d/%d", x.BackedUpCount, x.TotalCount) + case matrix_sdk_ffi.BackupUploadStateError: + fatalf(t, "MustBackupKeys: state=error") + case matrix_sdk_ffi.BackupUploadStateDone: + genericListener.Close() + return + } + } */ } func (c *RustClient) WaitUntilEventInRoom(t *testing.T, roomID string, checker func(Event) bool) Waiter { @@ -494,10 +513,25 @@ func eventTimelineItemToEvent(item *matrix_sdk_ffi.EventTimelineItem) *Event { return &complementEvent } -type roomListLoadingStateListener struct { - ch chan matrix_sdk_ffi.RoomListLoadingState +type genericStateListener[T any] struct { + ch chan T + isClosed bool +} + +func newGenericStateListener[T any]() *genericStateListener[T] { + return &genericStateListener[T]{ + ch: make(chan T), + } +} + +func (l *genericStateListener[T]) Close() { + l.isClosed = true + close(l.ch) } -func (s *roomListLoadingStateListener) OnUpdate(state matrix_sdk_ffi.RoomListLoadingState) { - s.ch <- state +func (l *genericStateListener[T]) OnUpdate(state T) { + if l.isClosed { + return + } + l.ch <- state }